-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
}; | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
||
ans.push_back(str); | ||
|
||
return ans; | ||
|
||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里与上面的
if/else
,若改为三元表达式会更简洁~