Skip to content

Add Soutution CPP[228 && 283] #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions solution/0228.Summary Ranges/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## 汇总区间

### 问题描述

给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。

```
示例 1:
输入: [0,1,2,4,5,7]
输出: ["0->2","4->5","7"]
解释: 0,1,2 可组成一个连续的区间; 4,5 可组成一个连续的区间。

示例 2:
输入: [0,2,3,4,6,8,9]
输出: ["0","2->4","6","8->9"]
解释: 2,3,4 可组成一个连续的区间; 8,9 可组成一个连续的区间。
```
------
### 思路

1. 设置count计数有多少个连续数字
2. 当不连续时,得出一个区间加入答案,更改下标`idx += (count+1)`,且count重新置0



```CPP
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int len = nums.size();
if(len == 0)return {};
vector<string> ans;
int count = 0;
int idx = 0;
while((idx + count) < len-1){
if(nums[idx+count] == nums[idx+count+1]-1)count++;
else{
string str;
if(count == 0){
str = to_string(nums[idx]);
}
else{
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
}
ans.push_back(str);
idx += (count+1);
count = 0;
}
}

//末尾处理
string str;
if(count > 0)
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
else
str = to_string(nums[idx]);

ans.push_back(str);

return ans;

}
};

```
37 changes: 37 additions & 0 deletions solution/0228.Summary Ranges/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int len = nums.size();
if(len == 0)return {};
vector<string> ans;
int count = 0;
int idx = 0;
while((idx + count) < len-1){
if(nums[idx+count] == nums[idx+count+1]-1)count++;
else{
string str;
if(count == 0){
str = to_string(nums[idx]);
}
else{
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
}
ans.push_back(str);
idx += (count+1);
count = 0;
}
}

//末尾处理
string str;
if(count > 0)
str = to_string(nums[idx])+"->"+to_string(nums[idx+count]);
else
str = to_string(nums[idx]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里与上面的 if/else ,若改为三元表达式会更简洁~


ans.push_back(str);

return ans;

}
};
74 changes: 74 additions & 0 deletions solution/0283.Move Zeroes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## 移动0

### 问题描述

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

```
示例:
输入: [0,1,0,3,12]
输出: [1,3,12,0,0]
```
说明:

1. 必须在原数组上操作,不能拷贝额外的数组。
2. 尽量减少操作次数。

### 思路

两种思路,分别是

1. 快慢指针,慢指针找0,快指针找慢指针之后的非0元素和慢指针交换,没有找到就直接结束
2. 也可以通过对非0元素遍历来实现(更好)

```CPP
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size();
if(len == 0)return;

int slow = 0;
int fast;

while(slow < len){
if(nums[slow] == 0){
fast = slow+1;
while(fast < len){
if(nums[fast] == 0)fast++;
else break;
}

if(fast == len)return;

swap(nums[slow],nums[fast]);
}
slow++;
}

}
};
```

-----------------------
```CPP
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size();
if(len == 0)return;

int idx = 0;
for(int i = 0;i<len;i++){
if(nums[i] != 0){
nums[idx] = nums[i];
idx++;
}
}

for(int i = idx;i<len;i++){
nums[i] = 0;
}
}
};
```
48 changes: 48 additions & 0 deletions solution/0283.Move Zeroes/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size();
if(len == 0)return;

int slow = 0;
int fast;

while(slow < len){
if(nums[slow] == 0){
fast = slow+1;
while(fast < len){
if(nums[fast] == 0)fast++;
else break;
}

if(fast == len)return;

swap(nums[slow],nums[fast]);
}
slow++;
}

}
};

//---------------------------------------------

class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size();
if(len == 0)return;

int idx = 0;
for(int i = 0;i<len;i++){
if(nums[i] != 0){
nums[idx] = nums[i];
idx++;
}
}

for(int i = idx;i<len;i++){
nums[i] = 0;
}
}
};