Skip to content

Commit 00b78b1

Browse files
committed
Week04 Home Work
1 parent f7cd447 commit 00b78b1

14 files changed

Lines changed: 481 additions & 1 deletion
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int max = 0;
5+
for (int i = 1; i < prices.size(); i++)
6+
if (prices[i] > prices[i-1])
7+
max += prices[i] - prices[i-1];
8+
return max;
9+
}
10+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
4+
if (beginWord.empty() || endWord.empty() || wordList.size() == 0) return 0;
5+
if (find(wordList.begin(), wordList.end(), endWord) == wordList.end()) return 0;
6+
7+
vector<int> visited(wordList.size(), 0);
8+
queue<string> q;
9+
q.push(beginWord);
10+
int step = 1;
11+
12+
while(q.size()) {
13+
step++;
14+
int len = q.size();
15+
for (int k = 0; k < len; k++) {
16+
string tmp = q.front();
17+
q.pop();
18+
19+
for (int i = 0; i < wordList.size(); i++) {
20+
if (visited[i] == 0) {
21+
int diff = 0;
22+
for (int j = 0; j < tmp.length(); j++)
23+
if (tmp[j] != wordList[i][j]) diff++;
24+
25+
if (diff == 1) {
26+
if (wordList[i] == endWord) return step;
27+
visited[i] = 1;
28+
q.push(wordList[i]);
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
return 0;
36+
}
37+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int findMin(vector<int>& nums) {
4+
if (!nums.size()) return 0;
5+
if (nums.size() == 1) return nums[0];
6+
if (nums[0] < nums[nums.size() - 1] ) return nums[0];
7+
8+
int left = 0, right = nums.size() - 1;
9+
while(left < right) {
10+
if (left + 1 == right) return nums[right];
11+
int mid = (left + right) / 2;
12+
if (nums[mid - 1] > nums[mid]) return nums[mid];
13+
else if (nums[mid] > nums[mid + 1]) return nums[mid + 1];
14+
else if (nums[mid] < nums[right]) {//右侧有序
15+
right = mid - 1;
16+
} else {//左侧有序
17+
left = mid + 1;
18+
}
19+
}
20+
21+
return 0;
22+
23+
}
24+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int numIslands(vector<vector<char>>& grid) {
4+
if (!grid.size() || !grid[0].size()) return 0;
5+
int lands = 0;
6+
for (int i = 0; i < grid.size(); i++)
7+
for (int j = 0; j < grid[0].size(); j++)
8+
if (grid[i][j] == '1') {
9+
lands++;
10+
clear(i, j, grid);
11+
}
12+
return lands;
13+
}
14+
void clear(int row, int col, vector<vector<char>>& grid) {
15+
if (grid[row][col] == '0') return;
16+
grid[row][col] = '0';
17+
if (col < grid[0].size() - 1) clear(row, col + 1, grid); //清理右边
18+
if (row < grid.size() - 1) clear(row + 1, col, grid); //清理下边
19+
if (col > 0) clear(row, col - 1, grid); //清理左边,有必要
20+
if (row > 0) clear(row - 1, col, grid);//清理上边,好像没必要,因为调用是从上至下的,被一个S型用例整服了
21+
}
22+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int search(vector<int>& nums, int target) {
4+
if (nums.empty()) return -1;
5+
6+
int left = 0, right = nums.size() - 1;
7+
while (left <= right) {
8+
int pivot = (left + right) / 2;
9+
10+
if (target == nums[pivot]) return pivot;
11+
else if (nums[pivot] < nums[right]) { //右侧有序
12+
if (target > nums[pivot] && target <= nums[right]) {//在有序区
13+
left = pivot + 1;
14+
} else {
15+
right = pivot - 1;
16+
}
17+
} else { //左侧有序
18+
if (target >= nums[left] && target < nums[pivot]) {
19+
right = pivot - 1;
20+
} else {
21+
left = pivot + 1;
22+
}
23+
}
24+
25+
}
26+
return -1;
27+
}
28+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
int minMutation(string start, string end, vector<string>& bank) {
4+
if (start.empty() || end.empty() || bank.size() == 0) return -1;
5+
if (find(bank.begin(), bank.end(), end) == bank.end()) return -1;
6+
7+
8+
vector<int> visited(bank.size(), 0);
9+
queue<string> q;
10+
q.push(start);
11+
int step = 0;
12+
13+
while(q.size()) {
14+
step++;
15+
int len = q.size();
16+
17+
for (int k = 0; k < len; k++) {
18+
string tmp = q.front();
19+
q.pop();
20+
21+
for (int i = 0; i < bank.size(); i++) {
22+
if (visited[i] == 0) {
23+
int diff = 0;
24+
for (int j = 0; j < tmp.length(); j++){
25+
if (tmp[j] != bank[i][j]) diff++;
26+
}
27+
28+
if (diff == 1) {
29+
if (bank[i] == end) return step;
30+
visited[i] = 1;
31+
q.push(bank[i]);
32+
}
33+
}
34+
}
35+
}
36+
}
37+
return -1;
38+
}
39+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int findContentChildren(vector<int>& g, vector<int>& s) {
4+
sort(g.begin(), g.end());
5+
sort(s.begin(), s.end());
6+
7+
int num = 0, i = 0, j = 0;
8+
while(i < g.size() && j < s.size()) {
9+
if (s[j] >= g[i]) {
10+
j++;
11+
i++;
12+
num++;
13+
} else {
14+
j++;
15+
}
16+
}
17+
18+
return num;
19+
}
20+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int jump(vector<int>& nums) {
4+
int maxpos = 0, end = 0, step = 0;
5+
for (int i = 0; i < nums.size() - 1; i++) {
6+
if (maxpos >= i) {
7+
maxpos = max(maxpos, i + nums[i]);
8+
if ( end == i) {
9+
end = maxpos;
10+
step++;
11+
}
12+
}
13+
}
14+
15+
return step;
16+
}
17+
};
18+
19+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
4+
if (!board.size() || !board[0].size()) return board;
5+
6+
char c = board[click[0]][click[1]];
7+
if (c == 'M') board[click[0]][click[1]] = 'X';
8+
else if (c == 'E') {
9+
calcBlock(click[0], click[1], board);
10+
} else {
11+
;
12+
}
13+
return board;
14+
}
15+
16+
void calcBlock(int row, int col, vector<vector<char>>& board) {
17+
int rows = board.size();
18+
int cols = board[0].size();
19+
if (row < 0 || row >= rows || col < 0 || col >= cols || board[row][col] != 'E')
20+
return;
21+
22+
int Ms = 0;
23+
int begin_row = row ? row - 1 : row;
24+
int begin_col = col ? col - 1 : col;
25+
int end_row = row == rows - 1 ? row : row + 1;
26+
int end_col = col == cols - 1 ? col : col + 1;
27+
28+
for (int i = begin_row; i <= end_row; i++)
29+
for (int j = begin_col; j <= end_col; j++) {
30+
if (board[i][j] == 'M') Ms++;
31+
}
32+
33+
if (Ms) {
34+
board[row][col] = '0' + Ms;
35+
} else {
36+
board[row][col] = 'B';
37+
for (int i = begin_row; i <= end_row; i++)
38+
for (int j = begin_col; j <= end_col; j++) {
39+
calcBlock(i, j, board);
40+
}
41+
}
42+
}
43+
};

Week04/LeetCodes/55_Jump_Game.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// class Solution {
2+
// public:
3+
// bool canJump(vector<int>& nums) {
4+
// int k = 0;
5+
// for (int i = 0; i < nums.size() && k < nums.size(); i++) {
6+
// if (i > k) return false;
7+
// k = max(k, i + nums[i]);
8+
// }
9+
10+
// return true;
11+
// }
12+
// };
13+
14+
class Solution {
15+
public:
16+
bool canJump(vector<int>& nums) {
17+
int last = nums.size() - 1;
18+
for (int i = last - 1; i >= 0; i--) {
19+
if (nums[i] + i >= last) {
20+
last = i;
21+
}
22+
}
23+
24+
return last <= 0;
25+
}
26+
};

0 commit comments

Comments
 (0)