Skip to content

Commit 244b20c

Browse files
committedNov 23, 2018
Add Soutution CPP[283 && 228]
1 parent 8f35d95 commit 244b20c

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed
 
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## 汇总区间
2+
3+
### 问题描述
4+
5+
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。
6+
7+
```
8+
示例 1:
9+
输入: [0,1,2,4,5,7]
10+
输出: ["0->2","4->5","7"]
11+
解释: 0,1,2 可组成一个连续的区间; 4,5 可组成一个连续的区间。
12+
13+
示例 2:
14+
输入: [0,2,3,4,6,8,9]
15+
输出: ["0","2->4","6","8->9"]
16+
解释: 2,3,4 可组成一个连续的区间; 8,9 可组成一个连续的区间。
17+
```
18+
------
19+
### 思路
20+
21+
1. 设置count计数有多少个连续数字
22+
2. 当不连续时,得出一个区间加入答案,更改下标`idx += (count+1)`,且count重新置0
23+
24+
25+
26+
```CPP
27+
class Solution {
28+
public:
29+
vector<string> summaryRanges(vector<int>& nums) {
30+
int len = nums.size();
31+
if(len == 0)return {};
32+
vector<string> ans;
33+
int count = 0;
34+
int idx = 0;
35+
while((idx + count) < len-1){
36+
if(nums[idx+count] == nums[idx+count+1]-1)count++;
37+
else{
38+
string str;
39+
if(count == 0){
40+
str = to_string(nums[idx]);
41+
}
42+
else{
43+
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
44+
}
45+
ans.push_back(str);
46+
idx += (count+1);
47+
count = 0;
48+
}
49+
}
50+
51+
//末尾处理
52+
string str;
53+
if(count > 0)
54+
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
55+
else
56+
str = to_string(nums[idx]);
57+
58+
ans.push_back(str);
59+
60+
return ans;
61+
62+
}
63+
};
64+
65+
```
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
vector<string> summaryRanges(vector<int>& nums) {
4+
int len = nums.size();
5+
if(len == 0)return {};
6+
vector<string> ans;
7+
int count = 0;
8+
int idx = 0;
9+
while((idx + count) < len-1){
10+
if(nums[idx+count] == nums[idx+count+1]-1)count++;
11+
else{
12+
string str;
13+
if(count == 0){
14+
str = to_string(nums[idx]);
15+
}
16+
else{
17+
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
18+
}
19+
ans.push_back(str);
20+
idx += (count+1);
21+
count = 0;
22+
}
23+
}
24+
25+
//末尾处理
26+
string str;
27+
if(count > 0)
28+
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
29+
else
30+
str = to_string(nums[idx]);
31+
32+
ans.push_back(str);
33+
34+
return ans;
35+
36+
}
37+
};

‎solution/0283.Move Zeroes/README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## 移动0
2+
3+
### 问题描述
4+
5+
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
6+
7+
```
8+
示例:
9+
输入: [0,1,0,3,12]
10+
输出: [1,3,12,0,0]
11+
```
12+
说明:
13+
14+
1. 必须在原数组上操作,不能拷贝额外的数组。
15+
2. 尽量减少操作次数。
16+
17+
### 思路
18+
19+
两种思路,分别是
20+
21+
1. 快慢指针,慢指针找0,快指针找慢指针之后的非0元素和慢指针交换,没有找到就直接结束
22+
2. 也可以通过对非0元素遍历来实现(更好)
23+
24+
```CPP
25+
class Solution {
26+
public:
27+
void moveZeroes(vector<int>& nums) {
28+
int len = nums.size();
29+
if(len == 0)return;
30+
31+
int slow = 0;
32+
int fast;
33+
34+
while(slow < len){
35+
if(nums[slow] == 0){
36+
fast = slow+1;
37+
while(fast < len){
38+
if(nums[fast] == 0)fast++;
39+
else break;
40+
}
41+
42+
if(fast == len)return;
43+
44+
swap(nums[slow],nums[fast]);
45+
}
46+
slow++;
47+
}
48+
49+
}
50+
};
51+
```
52+
53+
-----------------------
54+
```CPP
55+
class Solution {
56+
public:
57+
void moveZeroes(vector<int>& nums) {
58+
int len = nums.size();
59+
if(len == 0)return;
60+
61+
int idx = 0;
62+
for(int i = 0;i<len;i++){
63+
if(nums[i] != 0){
64+
nums[idx] = nums[i];
65+
idx++;
66+
}
67+
}
68+
69+
for(int i = idx;i<len;i++){
70+
nums[i] = 0;
71+
}
72+
}
73+
};
74+
```
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public:
3+
void moveZeroes(vector<int>& nums) {
4+
int len = nums.size();
5+
if(len == 0)return;
6+
7+
int slow = 0;
8+
int fast;
9+
10+
while(slow < len){
11+
if(nums[slow] == 0){
12+
fast = slow+1;
13+
while(fast < len){
14+
if(nums[fast] == 0)fast++;
15+
else break;
16+
}
17+
18+
if(fast == len)return;
19+
20+
swap(nums[slow],nums[fast]);
21+
}
22+
slow++;
23+
}
24+
25+
}
26+
};
27+
28+
//---------------------------------------------
29+
30+
class Solution {
31+
public:
32+
void moveZeroes(vector<int>& nums) {
33+
int len = nums.size();
34+
if(len == 0)return;
35+
36+
int idx = 0;
37+
for(int i = 0;i<len;i++){
38+
if(nums[i] != 0){
39+
nums[idx] = nums[i];
40+
idx++;
41+
}
42+
}
43+
44+
for(int i = idx;i<len;i++){
45+
nums[i] = 0;
46+
}
47+
}
48+
};

0 commit comments

Comments
 (0)