-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Add Solution 066&&073 CPP #75
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,49 @@ | ||
## 加一 | ||
|
||
### 问题描述 | ||
|
||
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 | ||
|
||
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 | ||
|
||
你可以假设除了整数 0 之外,这个整数不会以零开头。 | ||
|
||
``` | ||
示例 1: | ||
输入: [1,2,3] | ||
输出: [1,2,4] | ||
解释: 输入数组表示数字 123。 | ||
|
||
示例 2: | ||
输入: [4,3,2,1] | ||
输出: [4,3,2,2] | ||
解释: 输入数组表示数字 4321。 | ||
``` | ||
|
||
### 思路: | ||
1. 末尾加1,注意超过10的情况,要取余进1 | ||
2. 前后关系式应该是`digits[i-1] = digits[i-1] + digits[i] / 10;`,即前一个元素的值应该是本身加上后一个元素的进位 | ||
3. 若首元素>=10,则需要插入元素 | ||
|
||
```CPP | ||
class Solution { | ||
public: | ||
vector<int> plusOne(vector<int>& digits) { | ||
int len = digits.size(); | ||
if(len == 0)return digits; | ||
digits[len-1]++; | ||
int num = digits[len - 1]; | ||
for(int i = len - 1;i>=1;i--){ | ||
digits[i-1] = digits[i-1] + digits[i]/10; | ||
digits[i] %= 10; | ||
} | ||
|
||
if(digits[0] >= 10){ | ||
digits.insert(digits.begin(),digits[0]/10); | ||
digits[1] = digits[1] % 10; | ||
} | ||
return digits; | ||
} | ||
}; | ||
|
||
``` |
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,19 @@ | ||
class Solution { | ||
public: | ||
vector<int> plusOne(vector<int>& digits) { | ||
int len = digits.size(); | ||
if(len == 0)return digits; | ||
digits[len-1]++; | ||
int num = digits[len - 1]; | ||
for(int i = len - 1;i>=1;i--){ | ||
digits[i-1] = digits[i-1] + digits[i]/10; | ||
digits[i] %= 10; | ||
} | ||
|
||
if(digits[0] >= 10){ | ||
digits.insert(digits.begin(),digits[0]/10); | ||
digits[1] = digits[1] % 10; | ||
} | ||
return digits; | ||
} | ||
}; |
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,174 @@ | ||
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 | ||
|
||
``` | ||
示例 1: | ||
输入: | ||
[ | ||
[1,1,1], | ||
[1,0,1], | ||
[1,1,1] | ||
] | ||
输出: | ||
[ | ||
[1,0,1], | ||
[0,0,0], | ||
[1,0,1] | ||
] | ||
示例 2: | ||
|
||
输入: | ||
[ | ||
[0,1,2,0], | ||
[3,4,5,2], | ||
[1,3,1,5] | ||
] | ||
输出: | ||
[ | ||
[0,0,0,0], | ||
[0,4,5,0], | ||
[0,3,1,0] | ||
] | ||
``` | ||
|
||
### 进阶: | ||
一个直接的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。 | ||
|
||
一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。 | ||
|
||
你能想出一个常数空间的解决方案吗? | ||
|
||
### 思路1 | ||
|
||
1. 创建行列辅助数组,先遍历矩阵,定位元素是0的行列,分别加入行数组,列数组 | ||
2. 从行数组中取出元素,整行置零;同理列数组 | ||
|
||
空间复杂度`O(m+n)`,而且分别进行处理和列处理时有重复操作 | ||
|
||
### 思路2(优化思路1) | ||
|
||
1. 先检查首行首列有没有0,有的话设置bool标记 | ||
2. 从第二行第二列开始遍历,如果发现有0,设置`matrix[i][0] = 0和matrix[0][j] = 0`,即把首行首列对应行列值设置为0 | ||
3. 遍历首行首列,把值为0的行按行设置为0;列同理 | ||
4. 查看标记位,看是否需要把首行首列设置为0 | ||
|
||
这种思路没有用额外的空间,但是时间复杂度和思路1一样,都有待解决重复操作的问题 | ||
|
||
整体好于思路1,时间复杂度比思路1稳定 | ||
|
||
### Solution1 | ||
|
||
```CPP | ||
class Solution { | ||
public: | ||
void setZeroes(vector<vector<int>>& matrix) { | ||
if(matrix.empty())return; | ||
//行数组,列数组 | ||
int rowNum = matrix.size(); | ||
int columnNum = matrix[0].size(); | ||
vector<int> rowVec; | ||
vector<int> columnVec; | ||
|
||
|
||
for(int i = 0;i<rowNum;i++){ | ||
for(int j = 0;j<columnNum;j++){ | ||
if(matrix[i][j] == 0){ | ||
auto iter = find(rowVec.begin(),rowVec.end(),i); | ||
if(iter == rowVec.end())rowVec.push_back(i); | ||
|
||
iter = find(columnVec.begin(),columnVec.end(),j); | ||
if(iter == columnVec.end())columnVec.push_back(j); | ||
} | ||
} | ||
} | ||
|
||
rowNum = rowVec.size(); | ||
if(rowNum == 0)return; | ||
|
||
int row; | ||
int column; | ||
|
||
//行处理 | ||
for(int i = 0;i<rowNum;i++){ | ||
row = rowVec[i]; | ||
for(int j = 0;j<columnNum;j++){ | ||
matrix[row][j] = 0; | ||
} | ||
} | ||
|
||
//列处理 | ||
columnNum = columnVec.size(); | ||
rowNum = matrix.size(); | ||
for(int i = 0 ; i < columnNum;i++){ | ||
column = columnVec[i]; | ||
for(int j = 0;j<rowNum;j++){ | ||
matrix[j][column] = 0; | ||
} | ||
} | ||
|
||
} | ||
}; | ||
``` | ||
|
||
### Solution 2 | ||
|
||
```CPP | ||
class Solution { | ||
public: | ||
void setZeroes(vector<vector<int>>& matrix) { | ||
if(matrix.empty()) return; | ||
int m = matrix.size(); | ||
int n = matrix[0].size(); | ||
bool row = false , column = false; | ||
|
||
|
||
for(int i = 0; i < m; i++)//判断第1列的0; | ||
{ | ||
if(matrix[i][0] == 0) | ||
{ | ||
column = true; | ||
break; | ||
} | ||
} | ||
for(int i = 0; i < n; i ++)//判断第1行的0; | ||
{ | ||
if(matrix[0][i] == 0) | ||
{ | ||
row = true; | ||
break; | ||
} | ||
} | ||
|
||
for(int i = 1; i < m;i++) | ||
{ | ||
for(int j = 1; j < n;j++) | ||
{ | ||
if(matrix[i][j] == 0) | ||
{ | ||
matrix[0][j] = 0; | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
} | ||
for(int i = 1; i < m;i++) | ||
{ | ||
for(int j = 1; j < n;j++) | ||
{ | ||
if(matrix[i][0] == 0 || matrix[0][j] == 0) | ||
{ | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
if(row) | ||
for(int i = 0; i < n;i++) | ||
matrix[0][i] = 0; | ||
if(column) | ||
for(int i = 0; i < m;i++) | ||
matrix[i][0] = 0; | ||
|
||
return; | ||
|
||
|
||
} | ||
}; | ||
``` |
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,113 @@ | ||
//Solution1 | ||
class Solution { | ||
public: | ||
void setZeroes(vector<vector<int>>& matrix) { | ||
if(matrix.empty())return; | ||
//行数组,列数组 | ||
int rowNum = matrix.size(); | ||
int columnNum = matrix[0].size(); | ||
vector<int> rowVec; | ||
vector<int> columnVec; | ||
|
||
|
||
for(int i = 0;i<rowNum;i++){ | ||
for(int j = 0;j<columnNum;j++){ | ||
if(matrix[i][j] == 0){ | ||
auto iter = find(rowVec.begin(),rowVec.end(),i); | ||
if(iter == rowVec.end())rowVec.push_back(i); | ||
|
||
iter = find(columnVec.begin(),columnVec.end(),j); | ||
if(iter == columnVec.end())columnVec.push_back(j); | ||
} | ||
} | ||
} | ||
|
||
rowNum = rowVec.size(); | ||
if(rowNum == 0)return; | ||
|
||
int row; | ||
int column; | ||
|
||
//行处理 | ||
for(int i = 0;i<rowNum;i++){ | ||
row = rowVec[i]; | ||
for(int j = 0;j<columnNum;j++){ | ||
matrix[row][j] = 0; | ||
} | ||
} | ||
|
||
//列处理 | ||
columnNum = columnVec.size(); | ||
rowNum = matrix.size(); | ||
for(int i = 0 ; i < columnNum;i++){ | ||
column = columnVec[i]; | ||
for(int j = 0;j<rowNum;j++){ | ||
matrix[j][column] = 0; | ||
} | ||
} | ||
|
||
} | ||
}; | ||
|
||
//--------------------------------------------------------------------- | ||
//Solution2 | ||
|
||
class Solution { | ||
public: | ||
void setZeroes(vector<vector<int>>& matrix) { | ||
if(matrix.empty()) return; | ||
int m = matrix.size(); | ||
int n = matrix[0].size(); | ||
bool row = false , column = false; | ||
|
||
|
||
for(int i = 0; i < m; i++)//判断第1列的0; | ||
{ | ||
if(matrix[i][0] == 0) | ||
{ | ||
column = true; | ||
break; | ||
} | ||
} | ||
for(int i = 0; i < n; i ++)//判断第1行的0; | ||
{ | ||
if(matrix[0][i] == 0) | ||
{ | ||
row = true; | ||
break; | ||
} | ||
} | ||
|
||
for(int i = 1; i < m;i++) | ||
{ | ||
for(int j = 1; j < n;j++) | ||
{ | ||
if(matrix[i][j] == 0) | ||
{ | ||
matrix[0][j] = 0; | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
} | ||
for(int i = 1; i < m;i++) | ||
{ | ||
for(int j = 1; j < n;j++) | ||
{ | ||
if(matrix[i][0] == 0 || matrix[0][j] == 0) | ||
{ | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
if(row) | ||
for(int i = 0; i < n;i++) | ||
matrix[0][i] = 0; | ||
if(column) | ||
for(int i = 0; i < m;i++) | ||
matrix[i][0] = 0; | ||
|
||
return; | ||
|
||
|
||
} | ||
}; |
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.
这个
REAME.md
写得不是很规范噢。