Skip to content

Commit 4120477

Browse files
committed
Merge branch 'master' of github.com:doocs/leetcode
2 parents 035fc4f + 5cd4023 commit 4120477

File tree

20 files changed

+614
-2
lines changed

20 files changed

+614
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int firstMissingPositive(int* nums, int numsSize) {
2+
3+
int Max = nums[0], i, *Count;
4+
5+
for(i = 1; i<numsSize; i++){
6+
Max = (Max < nums[i]) ? nums[i] : Max;
7+
}
8+
9+
Count = (int*)calloc(Max+1, sizeof(int));
10+
for(i = 0; i<numsSize; i++){
11+
if(nums[i] > 0){
12+
Count[nums[i]]++;
13+
}
14+
}
15+
16+
i = 1;
17+
while(Count[i] != 0){
18+
i++;
19+
}
20+
21+
return i;
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def firstMissingPositive(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
8+
i = 1
9+
while i in nums:
10+
i += 1
11+
return i
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public void rotate(int[][] matrix) {
3+
if(matrix==null) return;
4+
int n=matrix.length;
5+
for(int i=0;i<n;i++){
6+
for(int j=i;j<n;j++){
7+
int temp=matrix[i][j];
8+
matrix[i][j]=matrix[j][i];
9+
matrix[j][i]=temp;
10+
}
11+
}
12+
for(int i=0;i<n;i++){
13+
for(int j=0;j<n/2;j++){
14+
int temp = matrix[i][j];
15+
matrix[i][j] = matrix[i][matrix.length-1-j];
16+
matrix[i][matrix.length-1-j] = temp;
17+
}
18+
}
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
Map<String, List<String>> map = new HashMap<>();
4+
for (String str : strs) {
5+
char[] chars = str.toCharArray();
6+
Arrays.sort(chars);
7+
String key = new String(chars);
8+
if (!map.containsKey(key)) map.put(key, new ArrayList<>());
9+
map.get(key).add(str);
10+
}
11+
return new ArrayList<>(map.values());
12+
}
13+
}

solution/0066.Plus One/Solution.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def plusOne(self, digits):
3+
"""
4+
:type digits: List[int]
5+
:rtype: List[int]
6+
"""
7+
8+
i = len(digits)-1
9+
digits[i] += 1
10+
11+
while i > 0 and digits[i] > 9 :
12+
digits[i] = 0
13+
i -= 1
14+
digits[i] += 1
15+
16+
if digits[0] > 9:
17+
digits[0] = 0
18+
digits.insert(0, 1)
19+
20+
return digits

solution/0066.Plus One/Solution2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def plusOne(self, digits):
3+
"""
4+
:type digits: List[int]
5+
:rtype: List[int]
6+
"""
7+
8+
return list(map(int, str(int("".join(map(str, digits)))+1)))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## 岛屿的个数
2+
### 题目描述
3+
给定一个由`'1'`(陆地)和`'0'`(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
4+
```
5+
示例 1:
6+
输入:
7+
11110
8+
11010
9+
11000
10+
00000
11+
输出: 1
12+
```
13+
```
14+
示例 2:
15+
输入:
16+
11000
17+
11000
18+
00100
19+
00011
20+
输出: 3
21+
```
22+
### 解法
23+
对网格进行遍历,每遇到一个岛屿,都将其中陆地`1`全部变成`0`,则遇到岛屿的总数即为所求。
24+
```python
25+
class Solution:
26+
def numIslands(self, grid):
27+
def dp(x, y):
28+
if x >= 0 and x < len(grid) and y >= 0 and y < len(grid[x]) and grid[x][y] == '1':
29+
grid[x][y] = '0'
30+
dp(x-1, y)
31+
dp(x+1, y)
32+
dp(x, y-1)
33+
dp(x, y+1)
34+
ans = 0
35+
for i in range(len(grid)):
36+
for j in range(len(grid[i])):
37+
if grid[i][j] == '1':
38+
ans += 1
39+
dp(i, j)
40+
return ans
41+
42+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def numIslands(self, grid):
3+
"""
4+
:type grid: List[List[str]]
5+
:rtype: int
6+
"""
7+
def dp(x, y):
8+
if x >= 0 and x < len(grid) and y >= 0 and y < len(grid[x]) and grid[x][y] == '1':
9+
grid[x][y] = '0'
10+
dp(x-1, y)
11+
dp(x+1, y)
12+
dp(x, y-1)
13+
dp(x, y+1)
14+
ans = 0
15+
for i in range(len(grid)):
16+
for j in range(len(grid[i])):
17+
if grid[i][j] == '1':
18+
ans += 1
19+
dp(i, j)
20+
return ans
Lines changed: 65 additions & 0 deletions
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+
```
Lines changed: 37 additions & 0 deletions
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+
};

0 commit comments

Comments
 (0)