Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5e1f18

Browse files
committed
some test for decltype
1 parent 23190cc commit f5e1f18

File tree

6 files changed

+110
-23
lines changed

6 files changed

+110
-23
lines changed

test/.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"ostream": "cpp"
4+
}
5+
}

test/4_8/test

-31.6 KB
Binary file not shown.

test/4_8/test.cpp

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,98 @@
33
#include<algorithm>
44
using namespace std;
55

6+
// class Solution {
7+
// public:
8+
9+
// int triangleNumber(vector<int>& nums) {
10+
// sort(nums.begin(), nums.end());
11+
// int left = 0, right = nums.size() - 1, cur = left + 1, ret = 0;
12+
// while(left <= right - 2){
13+
// int target = nums[left], cur = left + 1;
14+
// while(right >= left + 2){
15+
// while(cur < right){
16+
// if(nums[right] - nums[cur] < target){
17+
// ret += right - cur;
18+
// break;
19+
// }
20+
// else{
21+
// cur++;
22+
// }
23+
// }
24+
// cur = left + 1;
25+
// right--;
26+
// }
27+
// left++, right = nums.size() - 1;
28+
// }
29+
// return ret;
30+
// }
31+
// };
32+
33+
// class Solution {
34+
// public:
35+
// int lengthOfLongestSubstring(string s) {
36+
// int buff[200] = {0};
37+
// int max_length = 0, left = 0, right = 0, sz = s.size();
38+
// while(right < sz){
39+
// if(buff[s[right]] != 0){
40+
// max_length = max(max_length, right - left);
41+
// //有重复的数字,将left移到没有重复数字的地方位置
42+
// while(left < right){
43+
// if(s[left] == s[right]){
44+
// buff[left++]--;
45+
// break;
46+
// }else{
47+
// buff[left++]--;
48+
// }
49+
// }
50+
// }
51+
// buff[right++]++;
52+
// }
53+
// return max_length;
54+
// }
55+
// };
656
class Solution {
757
public:
58+
int KZero(const vector<int>& nums){
59+
int max_length = 0, left = 0, right = 0, num = 0, sz = nums.size();
60+
while(right < sz){
61+
if(nums[right] == 0){
62+
//更新left到第一个非0序列
63+
while(left < sz && nums[left] == 0){
64+
left++;
65+
}
66+
}
67+
right = left;
68+
if(right > sz) break;
69+
while(nums[right] == 1) right++;
70+
max_length = max(max_length, right - left);
71+
}
72+
return max_length;
73+
}
874

9-
int triangleNumber(vector<int>& nums) {
10-
sort(nums.begin(), nums.end());
11-
int left = 0, right = nums.size() - 1, cur = left + 1, ret = 0;
12-
while(left <= right - 2){
13-
int target = nums[left], cur = left + 1;
14-
while(right >= left + 2){
15-
while(cur < right){
16-
if(nums[right] - nums[cur] < target){
17-
ret += right - cur;
18-
break;
19-
}
20-
else{
21-
cur++;
22-
}
23-
}
24-
cur = left + 1;
25-
right--;
26-
}
27-
left++, right = nums.size() - 1;
75+
int longestOnes(vector<int>& nums, int k) {
76+
if(k == 0) return KZero(nums);
77+
//这道题,我们可以更改思路,因为翻转k个0并不好算,所以我们可以找连续k个0的个数,找到了就是结果
78+
int max_length = 0, left = 0, right = 0, num = 0, sz = nums.size();
79+
while(right < sz){
80+
if(num >= k){
81+
//证明[left, right)区间正好有k个0,此时要将left指向最近的0的下一个位置
82+
while(left < right && nums[left] != 0) { left++; }
83+
left++, num--;
84+
}
85+
while(left < right && nums[right] != 0) { right++; }
86+
right++, num++;
87+
while(right < sz && nums[right] != 0) { right++; }
88+
max_length = max(max_length, right-left);
89+
cout << max_length << "left is " << left << "right is " << right << endl;
2890
}
29-
return ret;
91+
return max_length;
3092
}
3193
};
3294

3395
int main(){
3496
Solution s;
35-
//vector<int> nums = {2,2,3,4};
36-
vector<int> nums = {24,3,82,22,35,84,19};
37-
cout << s.triangleNumber(nums) << endl;
97+
vector<int> nums = {0,0,1,1,1,0,0};
98+
cout << s.longestOnes(nums, 0) << endl;
3899
return 0;
39100
}

test/decltype/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test: test.cpp
2+
g++ test.cpp -o test
3+
4+
PHONY: clean
5+
clean:
6+
rm test

test/decltype/test

16.3 KB
Binary file not shown.

test/decltype/test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
int x = int();
6+
char m = char();
7+
double d = double();
8+
cout << m << endl;
9+
10+
cout << x << " " << m << " " << d << endl;
11+
return 0;
12+
}
13+
14+
15+

0 commit comments

Comments
 (0)