From 93fbcda70527c1ea7417df84530c91166c30dc68 Mon Sep 17 00:00:00 2001 From: zhangzc Date: Mon, 1 Nov 2021 16:21:27 +0800 Subject: [PATCH] update exercises --- .../solution.md" | 212 ++++++++++++++- .../solution.md" | 102 +++++++- .../solution.md" | 206 +++++++++++++++ .../solution.md" | 242 +++++++++++++++++- .../solution.md" | 186 +++++++++++++- .../solution.md" | 156 ++++++++++- .../solution.md" | 173 ++++++++++++- .../solution.md" | 114 ++++++++- .../solution.md" | 132 +++++++++- .../solution.md" | 172 ++++++++++++- .../solution.md" | 154 ++++++++++- .../solution.md" | 127 ++++++++- .../solution.md" | 146 ++++++++++- .../solution.md" | 175 ++++++++++++- .../solution.md" | 86 ++++++- .../solution.md" | 131 +++++++++- .../1.leetcode/49_Pow(x, n)/solution.md" | 110 +++++++- .../solution.md" | 216 +++++++++++++++- .../solution.md" | 186 +++++++++++++- 19 files changed, 2979 insertions(+), 47 deletions(-) diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/33_\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/33_\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/solution.md" index ec1353af6..b8318b05e 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/33_\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/33_\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/solution.md" @@ -3,30 +3,238 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector res; + vector nums{5, 7, 7, 8, 8, 10}; + int target = 8; + + res = sol.searchRange(nums, target); + for (auto i : res) + cout << i << " "; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + int left(vector &nums, int target) + { + int left = 0; + int right = nums.size() - 1; + while (left <= right) + { + int mid = (left + right) / 2; + if (nums[mid] == target) + { + if (mid == 0 || nums[mid - 1] < target) + { + return mid; + } + right = mid + 1; + } + else if (nums[mid] > target) + { + right = mid + 1; + } + else + { + left = mid - 1; + } + } + return -1; + } + int right(vector &nums, int target) + { + int left = 0; + int right = nums.size() - 1; + while (left <= right) + { + int mid = (left + right) / 2; + if (nums[mid] == target) + { + if (mid == nums.size() - 1 || nums[mid + 1] > target) + { + return mid; + } + left = mid - 1; + } + else if (nums[mid] > target) + { + right = mid + 1; + } + else + { + left = mid - 1; + } + } + return -1; + } + vector searchRange(vector &nums, int target) + { + vector result; + result.push_back(left(nums, target)); + result.push_back(right(nums, target)); + return result; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + vector searchRange(vector &nums, int target) + { + vector res(2, -1); + int i = 0, j = nums.size(); + int mid = (i + j) / 2; + int p = -1; + while (i < j) + { + if (nums[mid] == target) + { + p = mid; + break; + } + if (nums[mid] > target) + { + if (j == mid) + break; + j = mid; + mid = (i + j) / 2; + } + else + { + if (i == mid) + break; + i = mid; + mid = (i + j) / 2; + } + } + + if (p == -1) + { + return res; + } + else + { + int a = p, b = p; + while (a > 0 && nums[a - 1] == target) + a--; + while (b < nums.size() - 1 && nums[b + 1] == target) + b++; + vector h; + h.push_back(a); + h.push_back(b); + return h; + } + } +}; ``` ### B ```cpp +class Solution +{ +public: + vector searchRange(vector &nums, int target) + { + vector res; + res.push_back(binary_search_begin(nums, target)); + res.push_back(binary_search_end(nums, target)); + return res; + } +private: + int binary_search_begin(vector nums, int target) + { + int lo = -1; + int hi = nums.size(); + while (lo + 1 < hi) + { + int mid = lo + (hi - lo) / 2; + if (target > nums[mid]) + { + lo = mid; + } + else + { + hi = mid; + } + } + if (hi == nums.size() || nums[hi] != target) + { + return -1; + } + else + { + return hi; + } + } + int binary_search_end(vector nums, int target) + { + int lo = -1; + int hi = nums.size(); + while (lo + 1 < hi) + { + int mid = lo + (hi - lo) / 2; + if (target < nums[mid]) + { + hi = mid; + } + else + { + lo = mid; + } + } + if (lo == -1 || nums[lo] != target) + { + return -1; + } + else + { + return lo; + } + } +}; ``` ### C ```cpp - +class Solution +{ +public: + vector searchRange(vector &nums, int target) + { + int start = -1, end = -1; + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] == target) + { + if (start == -1) + start = i; + end = i; + } + } + vector ans; + ans.push_back(start); + ans.push_back(end); + return ans; + }; +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/34_\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/34_\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/solution.md" index 4fe0c8672..529c5cacf 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/34_\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/34_\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/solution.md" @@ -3,30 +3,124 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int res; + vector nums{1, 3, 5, 6}; + int target = 5; + + res = sol.searchInsert(nums, target); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + int searchInsert(vector &nums, int target) + { + + int len = nums.size(); + if (target <= nums[0]) + return 0; + + for (int i = 0; i < len; i++) + { + if (nums[i] == target) + return i - 1; + else if (target < nums[i]) + return i + 1; + } + return len; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int searchInsert(vector &nums, int target) + { + int lo = -1; + int hi = nums.size(); + while (lo + 1 < hi) + { + int mid = lo + (hi - lo) / 2; + if (target > nums[mid]) + { + lo = mid; + } + else + { + hi = mid; + } + } + return hi; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + int searchInsert(vector &nums, int target) + { + int len = nums.size(); + if (len == 0) + return 0; + for (int i = 0; i < len; i++) + { + if (nums[i] >= target) + return i; + } + return len; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int searchInsert(vector &nums, int target) + { + int mid = 0; + int head = 0; + int last = nums.size() - 1; + while (head < last) + { + mid = (last - head) / 2 + head; + if (target > nums[mid]) + { + head = mid + 1; + } + else if (target < nums[mid]) + { + last = mid - 1; + } + else + return mid; + } + if (target <= nums[head]) + return head; + return head + 1; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/35_\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/35_\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/solution.md" index a0d2b9a08..43433f92d 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/35_\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/35_\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/solution.md" @@ -12,21 +12,227 @@ ## 答案 ```cpp +class Solution +{ +public: + bool isValidSudoku(vector> &board) + { + int rule1[10][10] = {0}, rule2[10][10] = {0}, rule3[10][10] = {0}; + for (int row = 0; row < 9; row++) + { + for (int col = 0; col < 9; col++) + { + if (board[row][col] >= '0' && board[row][col] <= '9') + { + rule1[row][board[row][col] - '0']++; + if (rule1[row][board[row][col] - '0'] > 1) + return false; + rule2[board[row][col] - '0'][col]++; + if (rule2[board[row][col] - '0'][col] > 1) + return false; + int name = row / 3 + (col % 3) * 3; + rule3[name][board[row][col] - '0']++; + if (rule3[name][board[row][col] - '0'] > 1) + return false; + } + } + } + return true; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + bool isValidSudoku(vector> &board) + { + for (int i = 0; i < board.size(); i++) + { + vector mark(10); + for (int j = 0; j < board.size(); j++) + { + if (!valid(board, mark, i, j)) + { + return false; + } + } + } + for (int j = 0; j < board.size(); j++) + { + vector mark(10); + for (int i = 0; i < board.size(); i++) + { + if (!valid(board, mark, i, j)) + { + return false; + } + } + } + for (int k = 0; k < board.size(); k++) + { + int sr = k / 3 * 3; + int sc = (k % 3) * 3; + vector mark(10); + for (int i = sr; i < sr + 3; i++) + { + for (int j = sc; j < sc + 3; j++) + { + if (!valid(board, mark, i, j)) + { + return false; + } + } + } + } + return true; + } +private: + bool valid(vector> &board, vector &mark, int i, int j) + { + if (board[i][j] != '.') + { + int index = board[i][j] - '0'; + if (mark[index]) + { + return false; + } + else + { + mark[index] = 1; + } + } + return true; + } +}; ``` ### B ```cpp +class Solution +{ +public: + bool isValidSudoku(vector> &board) + { + int len = 9; + for (int i = 0; i < len; ++i) + { + if (isRowValid(i, board) == false) + return false; + } + + for (int i = 0; i < len; ++i) + { + if (isColumnValid(i, board) == false) + return false; + } + + for (int i = 0; i < len; i += 3) + { + for (int j = 0; j < len; j += 3) + { + if (isNineValid(i, j, board) == false) + return false; + } + } + return true; + } + + bool isRowValid(int row, vector> &board) + { + vector temp; + for (int i = 0; i < board[0].size(); ++i) + { + if ('.' == board[row][i]) + continue; + else + { + temp.push_back(board[row][i]); + } + for (int j = 0; j < temp.size() - 1; ++j) + { + if (temp[j] == board[row][i]) + return false; + } + } + return true; + } + + bool isColumnValid(int column, vector> &board) + { + vector temp; + for (int i = 0; i < board[0].size(); ++i) + { + if (board[i][column] == '.') + continue; + else + { + temp.push_back(board[i][column]); + } + for (int j = 0; j < temp.size() - 1; ++j) + { + if (temp[j] == board[i][column]) + return false; + } + } + return true; + } + + bool isNineValid(int row, int column, vector> &board) + { + vector temp; + for (int i = row; i < row + 3; ++i) + { + for (int j = column; j < column + 3; ++j) + { + if (board[i][j] == '.') + continue; + else + { + temp.push_back(board[i][j]); + } + for (int k = 0; k < temp.size() - 1; ++k) + { + if (temp[k] == board[i][j]) + return false; + } + } + } + return true; + } +}; ``` ### C ```cpp +class Solution +{ +public: + bool isValidSudoku(vector> &board) + { + vector> rows(9, vector(9, false)); + vector> cols(9, vector(9, false)); + vector> blocks(9, vector(9, false)); + for (int i = 0; i < 9; ++i) + { + for (int j = 0; j < 9; ++j) + { + if (board[i][j] == '.') + continue; + int c = board[i][j] - '1'; + if (rows[i][c] || cols[j][c] || blocks[i - i % 3 + j / 3][c]) + return false; + rows[i][c] = cols[j][c] = blocks[i - i % 3 + j / 3][c] = true; + } + } + return true; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/36_\350\247\243\346\225\260\347\213\254/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/36_\350\247\243\346\225\260\347\213\254/solution.md" index 4edb60ec4..4f6ccff25 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/36_\350\247\243\346\225\260\347\213\254/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/36_\350\247\243\346\225\260\347\213\254/solution.md" @@ -12,21 +12,261 @@ ## 答案 ```cpp +class Solution +{ +public: + int row[9][9] = {0}, column[9][9] = {0}, grid[9][9] = {0}; + void generate(int i, int j, vector> &board, bool &selved) + { + if (i > 8) + { + selved = true; + return; + } + if (board[i][j] == '.') + { + int n = 3 * (i / 3) + j / 3; + for (int k = 0; k < 9; k++) + { + if (!row[i][k] && !column[j][k] && !grid[n][k]) + { + row[i][k]++; + column[j][k]++; + grid[n][k]++; + board[i][j] = k + '0' + 1; + if (j == 8) + generate(i + 1, 0, board, selved); + else + generate(i, j + 1, board, selved); + if (!selved) + { + row[i][k]--; + column[j][k]--; + grid[n][k]--; + board[i][j] = '.'; + } + } + } + } + else + { + if (j == 8) + generate(i + 1, 0, board, selved); + else + generate(i, j + 1, board, selved); + } + } + void solveSudoku(vector> &board) + { + int i, j; + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + if (board[i][j] == '.') + continue; + int num = board[i][j] - '0' - 1; + row[i][num]++; + column[j][num]++; + grid[3 * (i % 3) + j % 3][num]++; + } + } + bool selved = false; + generate(0, 0, board, selved); + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + void solveSudoku(vector> &board) + { + int size = board.size(); + vector> rows(size, vector(10)); + vector> cols(size, vector(10)); + vector> boxes(size, vector(10)); + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + { + if (board[i][j] != '.') + { + int num = board[i][j] - '0'; + int idx = i / 3 * 3 + j / 3; + rows[i][num] = true; + cols[j][num] = true; + boxes[idx][num] = true; + } + } + } + dfs(board, 0, rows, cols, boxes); + } +private: + bool valid(int num, int row, int col, int idx, vector> &rows, + vector> &cols, vector> &boxes) + { + return !rows[row][num] && !cols[col][num] && !boxes[idx][num]; + } + bool dfs(vector> &board, int size, vector> &rows, + vector> &cols, vector> &boxes) + { + if (size == 9 * 9) + { + return true; + } + else + { + bool ok = false; + int row = size / 9; + int col = size % 9; + int idx = row / 3 * 3 + col / 3; + if (board[row][col] == '.') + { + for (int i = 1; i <= 9; i++) + { + if (valid(i, row, col, idx, rows, cols, boxes)) + { + board[row][col] = i + '0'; + rows[row][i] = true; + cols[col][i] = true; + boxes[idx][i] = true; + ok = dfs(board, size + 1, rows, cols, boxes); + if (!ok) + { + rows[row][i] = false; + cols[col][i] = false; + boxes[idx][i] = false; + board[row][col] = '.'; + } + } + } + } + else + { + ok = dfs(board, size + 1, rows, cols, boxes); + } + return ok; + } + } +}; ``` ### B ```cpp +class Solution +{ +public: + void solveSudoku(vector> &board) + { + + Dfs(board, 0); + } + +private: + bool flag = false; + void Dfs(vector> &board, int n) + { + + if (n > 0 && n <= 81) + if (!JudgeIsNoWant(board, n - 1)) + return; + + if (n >= 81) + { + flag = true; + return; + } + + int x = n / 9; + int y = n % 9; + if (board[x][y] != '.') + Dfs(board, n + 1); + else + { + + for (int i = 1; i < 10; i++) + { + board[x][y] = i + 48; + Dfs(board, n + 1); + if (flag == true) + return; + board[x][y] = '.'; + } + } + } + + bool JudgeIsNoWant(vector> &board, int n) + { + int x = n / 9; + int y = n % 9; + + for (size_t i = 0; i < 9; i++) + { + if (board[x][i] == board[x][y] && i != y) + return false; + if (board[i][y] == board[x][y] && i != x) + return false; + } + + for (int i = x / 3 * 3; i < x / 3 * 3 + 3; i++) + for (int j = y / 3 * 3; j < y / 3 * 3 + 3; j++) + if (board[i][j] == board[x][y] && (i != x || j != y)) + return false; + + return true; + } +}; ``` ### C ```cpp +class Solution +{ +public: + int row[9][9], col[9][9], block[9][9]; + void solveSudoku(vector> &board) + { + for (int i = 0; i < 9; ++i) + { + for (int j = 0; j < 9; ++j) + { + if (board[i][j] == '.') + continue; + int num = board[i][j] - '1'; + row[i][num] = col[j][num] = block[i / 3 * 3 + j / 3][num] = 1; + } + } + dfs(board, 0, 0); + } + bool dfs(vector> &board, int r, int c) + { + if (r > 8) + return true; + if (board[r][c] == '.') + { + for (int i = 0; i < 9; ++i) + { + if (row[r][i] || col[c][i] || block[r / 3 * 3 + c / 3][i]) + continue; + board[r][c] = i + 1 + '0'; + row[r][i] = col[c][i] = block[r / 3 * 3 + c / 3][i] = 1; + + if (dfs(board, r + (c + 1) / 9, (c + 1) % 9)) + return true; + board[r][c] = '.'; + row[r][i] = col[c][i] = block[r / 3 * 3 + c / 3][i] = 0; + } + } + else + return dfs(board, r + (c + 1) / 9, (c + 1) % 9); + return false; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/37_\345\244\226\350\247\202\346\225\260\345\210\227/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/37_\345\244\226\350\247\202\346\225\260\345\210\227/solution.md" index 1227ae06b..27755d9df 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/37_\345\244\226\350\247\202\346\225\260\345\210\227/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/37_\345\244\226\350\247\202\346\225\260\345\210\227/solution.md" @@ -3,30 +3,206 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + string res; + int n = 4; + + res = sol.countAndSay(n); + + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + string countAndSay(int n) + { + if (n == 1) + return "1"; + string last = countAndSay(n - 1); + string ans; + int length = last.length(), cnt = 1; + for (int i = 0; i < length; i++) + { + if (i + 1 == length || last[i] != last[i + 1]) + { + ans.push_back(cnt + '0'); + ans.push_back(last[i]); + cnt++; + } + } + return ans; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + string countAndSay(int n) + { + string s = "1"; + char num[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + for (int i = 1; i < n; i++) + { + string news = ""; + int l = s.length(), count = 0; + char thischar = '*'; + for (int j = 0; j < l - 1; j++) + { + if (s[j] == s[j + 1]) + { + thischar = s[j]; + count++; + } + else if (s[j] != s[j + 1]) + { + if (count == 0) + { + count = 1; + thischar = s[j]; + news = news + num[count]; + news = news + thischar; + count = 0; + } + else if (count != 0) + { + count++; + news = news + num[count]; + news = news + thischar; + count = 0; + } + } + } + if (s[l - 2] == s[l - 1]) + { + count++; + news = news + num[count]; + news = news + thischar; + } + else if (s[l - 2] != s[l - 1]) + { + count++; + news = news + num[count]; + news = news + s[l - 1]; + } + s = news; + } + return s; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + string countAndSay(int n) + { + string ans = "11"; + if (n == 1) + { + return "1"; + } + else if (n == 2) + { + return "11"; + } + else + { + for (int i = 3; i <= n; i++) + { + int num = 1, flag = 0; + string temp = ans; + char last = temp[0]; + for (int j = 1; j < temp.size(); j++) + { + if (last == temp[j]) + { + num++; + } + else + { + if (flag == 0) + { + ans = to_string(num) + to_string(last - '0'); + } + else + { + ans += to_string(num) + to_string(last - '0'); + } + flag = 1; + last = temp[j]; + num = 1; + } + if (j + 1 == temp.size()) + { + if (flag == 0) + { + ans = to_string(num) + to_string(last - '0'); + } + else + { + ans += to_string(num) + to_string(last - '0'); + } + } + } + } + } + return ans; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + string countAndSay(int n) + { + if (n == 1) + { + return "1"; + } + string tmp = countAndSay(n - 1); + string result; + int length = tmp.length(); + int m = tmp[0] - '0'; + int count = 1; + for (int i = 1; i < length; i++) + { + if (m == (tmp[i] - '0')) + { + count++; + } + else + { + result.push_back(count + '0'); + result.push_back(m + '0'); + m = tmp[i] - '0'; + count = 1; + } + } + result.push_back(count + '0'); + result.push_back(m + '0'); + return result; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/38_\347\273\204\345\220\210\346\200\273\345\222\214/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/38_\347\273\204\345\220\210\346\200\273\345\222\214/solution.md" index 875240299..49400fe6a 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/38_\347\273\204\345\220\210\346\200\273\345\222\214/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/38_\347\273\204\345\220\210\346\200\273\345\222\214/solution.md" @@ -3,30 +3,184 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector> res; + vector candidates = {2, 3, 6, 7}; + int target = 7; + + res = sol.combinationSum(candidates, target); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + vector> combinationSum(vector &candidates, int target) + { + set> res; + vector temp; + sort(candidates.begin(), candidates.end()); + fun(candidates, target, 0, temp, res); + vector> result; + for (auto mem : res) + { + result.push_back(mem); + } + return result; + } + void fun(const vector &candidates, int target, int index, vector &temp, set> &res) + { + if (target < 0) + return; + if (target == 0) + { + res.insert(temp); + return; + } + while (index < candidates.size()) + { + temp.push_back(candidates[index]); + fun(candidates, target - candidates[index], index + 1, temp, res); + temp.pop_back(); + ++index; + } + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + vector> combinationSum(vector &candidates, int target) + { + vector> res; + dfs(candidates, 0, target, res); + return res; + } + +private: + vector stack; + void dfs(vector &candidates, int start, int target, vector> &res) + { + if (target < 0) + { + return; + } + else if (target == 0) + { + res.push_back(stack); + } + else + { + for (int i = start; i < candidates.size(); i++) + { + stack.push_back(candidates[i]); + dfs(candidates, i, target - candidates[i], res); + stack.pop_back(); + } + } + } +}; ``` ### B ```cpp +class Solution +{ +public: + void compute(int start, int target, vector &tmp, vector &candidates, vector> &ans) + { + int n = candidates.size(); + for (int i = start; i < n; i++) + { + if (target > 0) + { + tmp.push_back(candidates[i]); + compute(i, target - candidates[i], tmp, candidates, ans); + tmp.pop_back(); + } + else if (target < 0) + return; + else + { + ans.push_back(tmp); + return; + } + } + } + + vector> combinationSum(vector &candidates, int target) + { + vector> ans; + vector tmp; + int v; + + sort(candidates.begin(), candidates.end()); + compute(0, target, tmp, candidates, ans); + + return ans; + } +}; ``` ### C ```cpp +class Solution +{ +private: + vector> res; + vector ans; + +public: + vector> combinationSum(vector &candidates, int target) + { + sort(candidates.begin(), candidates.end()); + int left = 0, right = 0; + for (; right < candidates.size() && candidates[right] <= target; right++) + ; + backtrack(candidates, left, right == candidates.size() ? right - 1 : right, target); + return res; + } +private: + void backtrack(vector &candidates, int left, int right, int target) + { + if (target < 0) + return; + if (!target) + { + res.push_back(ans); + return; + } + for (int i = left; i <= right && candidates[i] <= target; i++) + { + ans.push_back(candidates[i]); + backtrack(candidates, i, right, target - candidates[i]); + ans.pop_back(); + } + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/39_\347\273\204\345\220\210\346\200\273\345\222\214 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/39_\347\273\204\345\220\210\346\200\273\345\222\214 II/solution.md" index e14e092e5..762f952ee 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/39_\347\273\204\345\220\210\346\200\273\345\222\214 II/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/39_\347\273\204\345\220\210\346\200\273\345\222\214 II/solution.md" @@ -3,30 +3,199 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector> res; + vector candidates = {10, 1, 2, 7, 6, 1, 5}; + int target = 8; + + res = sol.combinationSum2(candidates, target); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + vector> combinationSum2(vector &candidates, int target) + { + vector temp; + vector> result; + sort(candidates.begin(), candidates.end()); + getans(candidates, target, 0, result, temp); + return result; + } + void getans(vector candidates, int target, int start, vector> &result, vector temp) + { + if (target == 0) + { + result.push_back(temp); + } + else if (target > 0) + { + int k = candidates.size() - 1; + while (target < candidates[k]) + k--; + for (int i = start; i <= k; i++) + { + if (i != start && candidates[i] == candidates[i - 1]) + continue; + temp.push_back(candidates[i]); + getans(candidates, target, i, result, temp); + temp.pop_back(); + } + } + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + vector> combinationSum2(vector &candidates, int target) + { + vector> res; + sort(candidates.begin(), candidates.end()); + dfs(candidates, 0, target, res); + return res; + } +private: + vector stack; + void dfs(vector &candidates, int start, int target, vector> &res) + { + if (target < 0) + { + return; + } + else if (target == 0) + { + res.push_back(stack); + } + else + { + int last = INT_MIN; + for (int i = start; i < candidates.size(); i++) + { + if (last != candidates[i]) + { + stack.push_back(candidates[i]); + dfs(candidates, i + 1, target - candidates[i], res); + stack.pop_back(); + } + last = candidates[i]; + } + } + } +}; ``` ### B ```cpp +class Solution +{ +public: + vector> combinationSum2(vector &candidates, int target) + { + vector> res; + vector temp; + backtrace(candidates, temp, 0, target); + res.assign(m_set.begin(), m_set.end()); + return res; + } + + void backtrace(vector &candidates, + vector &temp, + int index, + int target) + { + + if (target == 0) + { + sort(temp.begin(), temp.end()); + /* 去重 */ + m_set.insert(temp); + return; + } + /* 设定边界*/ + if (index == candidates.size()) + { + return; + } + + if (target >= candidates[index]) + { + vector tmp(temp); + tmp.push_back(candidates[index]); + backtrace(candidates, tmp, index + 1, target - candidates[index]); + } + backtrace(candidates, temp, index + 1, target); + } + +private: + set> m_set; +}; ``` ### C ```cpp +class Solution +{ +public: + void dfs(vector> &ans, vector &candidates, vector &tmp, int target, int start) + { + if (target == 0) + { + ans.push_back(tmp); + return; + } + else if (target < 0) + { + return; + } + else + { + for (int i = start; i < candidates.size(); i++) + { + tmp.push_back(candidates[i]); + dfs(ans, candidates, tmp, target - candidates[i], i + 1); + tmp.pop_back(); + while (i + 1 < candidates.size() && candidates[i] == candidates[i + 1]) + i++; + } + } + } + + vector> combinationSum2(vector &candidates, int target) + { + vector> ans; + vector tmp; + + if (candidates.empty()) + return ans; + sort(candidates.begin(), candidates.end()); + dfs(ans, candidates, tmp, target, 0); + return ans; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/40_\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/40_\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/solution.md" index ccc97893c..b36cbfd87 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/40_\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/40_\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/solution.md" @@ -3,30 +3,134 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector nums = {1, 2, 0}; + int res = 8; + + res = sol.firstMissingPositive(nums); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int firstMissingPositive(vector &nums) + { + int n = nums.size(); + for (int i = 0; i < n; ++i) + { + while (nums[i] > 0 && nums[i] < n && nums[nums[i] - 1] != nums[i]) + { + swap(nums[i], nums[nums[i] - 1]); + } + } + for (int i = 0; i < n; ++i) + { + if (nums[i] != i + 1) + break; + } + return n + 1; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int firstMissingPositive(vector &nums) + { + if (nums.size() == 0) + { + return 1; + } + int i = 0; + while (i < nums.size()) + { + if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i]) + { + swap(nums[i], nums[nums[i] - 1]); + } + else + { + i++; + } + } + for (i = 0; i < nums.size(); i++) + { + if (nums[i] != i + 1) + { + break; + } + } + return i + 1; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + int firstMissingPositive(vector &nums) + { + int i, k, n = nums.size(); + for (i = 0; i < n; i++) + { + while (nums[i] > 0 && nums[i] <= n) + { + k = nums[i] - 1; + if (nums[k] == nums[i]) + break; + swap(nums[i], nums[k]); + } + } + for (i = 0; i < n; i++) + { + if (i + 1 != nums[i]) + break; + } + return i + 1; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int firstMissingPositive(vector &nums) + { + int n = nums.size(); + for (int i = 0; i < n; i++) + { + if (nums[i] <= 0 || nums[i] > n || (nums[nums[i] - 1] == nums[i])) + continue; + swap(nums[nums[i] - 1], nums[i]); + i--; + } + for (int i = 0; i < n; i++) + { + if (nums[i] != i + 1) + return i + 1; + } + return n + 1; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/41_\346\216\245\351\233\250\346\260\264/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/41_\346\216\245\351\233\250\346\260\264/solution.md" index 8144d407b..0bff0637e 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/41_\346\216\245\351\233\250\346\260\264/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/41_\346\216\245\351\233\250\346\260\264/solution.md" @@ -3,30 +3,158 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector nums = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; + int res; + + res = sol.trap(nums); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + int trap(vector &height) + { + int n = height.size(), area = 0; + stack> st; + for (int i = 0; i < n; i++) + { + if (st.empty()) + st.push(make_pair(height[i], i)); + else if (height[i] < st.top().first) + { + st.push(make_pair(height[i], i)); + } + else + { + while (!st.empty() && height[i] >= st.top().first) + { + auto tmp = st.top(); + st.pop(); + area += (i - 1 - st.top().second) * (min(st.top().first, height[i]) - tmp.first); + } + st.push(make_pair(height[i], i)); + } + } + return area; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int trap(vector &height) + { + int res = 0; + int left = 0, left_max = 0; + int right = height.size() - 1, right_max = 0; + while (left < right) + { + if (height[left] < height[right]) + { + if (height[left] > left_max) + { + left_max = height[left]; + } + else + { + res += left_max - height[left]; + } + left++; + } + else + { + if (height[right] > right_max) + { + right_max = height[right]; + } + else + { + res += right_max - height[right]; + } + right--; + } + } + return res; + } +}; ``` ### B ```cpp +class Solution +{ +public: + int trap(vector &height) + { + int res = 0; + for (int i = 1; i < height.size(); ++i) + { + int left_m = height[i], right_m = left_m; + + for (int j = 0; j < i; ++j) + left_m = max(left_m, height[j]); + for (int j = i + 1; j < height.size(); ++j) + right_m = max(right_m, height[j]); + int m = min(left_m, right_m); + res += m > height[i] ? m - height[i] : 0; + } + return res; + } +}; ``` ### C ```cpp +class Solution +{ +public: + int trap(vector &height) + { + int len = height.size(); + if (len == 0) + return 0; + vector left_max(len, 0); + vector right_max(len, 0); + int ans = 0; + + left_max[0] = height[0]; + for (int i = 1; i < len; i++) + { + left_max[i] = max(height[i], left_max[i - 1]); + } + + right_max[len - 1] = height[len - 1]; + for (int i = len - 2; i >= 0; i--) + { + right_max[i] = max(height[i], right_max[i + 1]); + } + + for (int i = 0; i < len; i++) + { + ans += min(left_max[i], right_max[i]) - height[i]; + } + return ans; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/42_\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/42_\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/solution.md" index 345e628d2..439597ee5 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/42_\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/42_\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/solution.md" @@ -3,30 +3,196 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + string num1 = "2", num2 = "3"; + string res; + + res = sol.multiply(num1, num2); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + string multiply(string num1, string num2) + { + if (num1 == "0" || num2 == "0") + return "0"; + int size1 = num1.size(), size2 = num2.size(); + string str(size1 + size2, '0'); + for (int i = size2 - 1; i >= 0; --i) + { + int mulflag = 0, addflag = 0; + for (int j = size1 - 1; j >= 0; --j) + { + int temp1 = (num2[i] - '0') * (num1[j] - '0') + mulflag; + mulflag = temp1 / 10; + temp1 = temp1 % 10; + int temp2 = str[i + j + 1] - '0' + temp1 + addflag; + str[i + j] = temp2 % 10 + '0'; + addflag = temp2 / 10; + } + str[i] += mulflag + addflag; + } + if (str[0] == '0') + str = str.substr(1, str.size()); + return str; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + string multiply(string num1, string num2) + { + string res(num1.length() + num2.length(), '0'); + for (int i = num2.length() - 1; i >= 0; i--) + { + int j, carry = 0; + for (j = num1.length() - 1; j >= 0; j--) + { + carry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0'); + res[i + j + 1] = carry % 10 + '0'; + carry /= 10; + } + res[i + j + 1] = carry + '0'; + } + int i; + for (i = 0; i < res.length() - 1; i++) + { + if (res[i] != '0') + { + break; + } + } + return res.substr(i); + } +}; ``` ### B ```cpp +class Solution +{ +public: + string multiply(string num1, string num2) + { + if (num1 == "0" || num2 == "0") + { + return "0"; + } + int n1 = num1.size(), n2 = num2.size(); + int n = n1 + n2; + vector ves(n, 0); + int k = n - 2; + for (int i = 0; i < n1; i++) + for (int j = 0; j < n2; j++) + { + ves[k - i - j] += (num1[i] - '0') * (num2[j] - '0'); + } + int carry = 0; + for (int i = 0; i < n; i++) + { + ves[i] += carry; + carry = ves[i] / 10; + ves[i] %= 10; + } + int t = n - 1; + while (ves[t] == 0) + { + --t; + } + string result; + while (t >= 0) + { + result.append(1, ves[t] + '0'); + --t; + } + return result; + } +}; ``` ### C ```cpp +class Solution +{ +public: + string multiply(string num1, string num2) + { + string &upper = num1; + string &down = num2; + deque res, adv; + for (int i = 0; i < down.length(); i++) + { + if (!res.empty()) + { + for (int k = 0; k < upper.length() - 1; k++) + { + adv.push_back(res.back()); + res.pop_back(); + } + } + for (int j = 0; j < upper.length(); j++) + { + if (adv.empty()) + { + res.push_back( + (down[i] - '0') * (upper[j] - '0')); + } + else + { + res.push_back( + adv.back() + (down[i] - '0') * (upper[j] - '0')); + adv.pop_back(); + } + } + } + + deque::iterator it = res.end() - 1; + while (it != res.begin() - 1) + { + if (*it > 9) + { + if (it == res.begin()) + { + int tmp = *it; + *it = tmp % 10; + res.push_front(tmp / 10); + } + else + { + int tmp = *it; + *it = tmp % 10; + *(it - 1) += tmp / 10; + } + } + it--; + } + string _r; + for (it = res.begin(); it != res.end(); it++) + { + _r += (*it + '0'); + } + return _r; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/43_\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/43_\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/solution.md" index db7bc77f9..bf9a830d3 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/43_\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/43_\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/solution.md" @@ -3,30 +3,178 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + string s = "adceb", p = "*a*b"; + bool res; + + res = sol.isMatch(s, p); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + bool isMatch(string s, string p) + { + if (s == "" && p == "" || (s == "" && p == "*")) + return true; + if (s == p) + return true; + int lens = s.length(); + int lenp = p.length(); + bool questionm = false, starm = false; + for (int k = 0; k < lenp; k++) + { + if (p[k] == '?') + questionm = true; + if (p[k] == '*') + starm = true; + } + if (lenp != lens && questionm == false && starm == false) + return false; + int i = 0, j = 0; + int mstar, sstar = -1; + while (i < lens) + { + if (j < lenp && p[j] == '*') + { + mstar = i; + sstar = j; + j += 1; + } + else if (sstar != -1) + { + mstar += 1; + j = sstar + 1; + i = mstar; + } + else + return false; + } + while (j < lenp) + { + if (p[j] != '*') + return false; + j++; + } + return true; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + bool isMatch(string s, string p) + { + int m = s.size(); + int n = p.size(); + + vector> dp(m + 1, vector(n + 1)); + dp[0][0] = true; + + for (int i = 1; i <= n; i++) + { + if (p[i - 1] == '*') + dp[0][i] = true; + else + break; + } + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + + if (p[j - 1] == '*') + { + dp[i][j] |= dp[i][j - 1]; + dp[i][j] |= dp[i - 1][j]; + } + else + { + if (p[j - 1] == '?' || s[i - 1] == p[j - 1]) + { + dp[i][j] |= dp[i - 1][j - 1]; + } + } + } + } + + return dp[m][n]; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + bool isMatch(string s, string p) + { + vector> dp(s.size() + 1, vector(p.size() + 1)); + dp[0][0] = 1; + for (int j = 1; j <= p.size(); j++) + { + dp[0][j] = dp[0][j - 1] && p[j - 1] == '*'; + } + for (int i = 1; i <= s.size(); i++) + { + for (int j = 1; j <= p.size(); j++) + { + if (p[j - 1] == '*') + { + dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; + } + else + { + dp[i][j] = (s[i - 1] == p[j - 1] || p[j - 1] == '?') && dp[i - 1][j - 1]; + } + } + } + return dp[s.size()][p.size()]; + } +}; ``` ### C ```cpp +class Solution +{ +public: + bool isMatch(string s, string p) + { + if (p.empty()) + return s.empty(); + if (s.empty()) + { + if (p[0] == '*') + return isMatch(s, p.substr(1)); + else + return false; + } + if (p[0] == '*') + return isMatch(s, p.substr(1)) || isMatch(s.substr(1), p); + else + return (s[0] == p[0] || p[0] == '?') && isMatch(s.substr(1), p.substr(1)); + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/44_\350\267\263\350\267\203\346\270\270\346\210\217 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/44_\350\267\263\350\267\203\346\270\270\346\210\217 II/solution.md" index a270b2be3..866e402f5 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/44_\350\267\263\350\267\203\346\270\270\346\210\217 II/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/44_\350\267\263\350\267\203\346\270\270\346\210\217 II/solution.md" @@ -3,30 +3,151 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector nums = {2, 3, 1, 1, 4}; + int res; + + res = sol.jump(nums); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + int jump(vector &nums) + { + int res_min = 0; + int end = 0; + int longest_distance = 0; + for (int i = 0; i < nums.size(); ++i) + { + longest_distance = max(longest_distance, nums[i]); + if (i == end) + { + if (end != nums.size() - 1) + { + ++res_min; + end = longest_distance; + if (longest_distance >= nums.size() - 1) + break; + } + else + break; + } + } + return res_min; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + int jump(vector &nums) + { + int steps = 0; + int lo = 0, hi = 0; + while (hi < nums.size() - 1) + { + int right = 0; + for (int i = lo; i <= hi; i++) + { + right = max(i + nums[i], right); + } + lo = hi + 1; + hi = right; + steps++; + } + return steps; + } +}; ``` ### B ```cpp +class Solution +{ +public: + int jump(vector &nums) + { + if (nums.size() == 1) + return 0; + int steps = 0, oldIdx = 0; + int nextJump = oldIdx + nums[oldIdx]; + if (nextJump >= nums.size() - 1) + return steps + 1; + while (oldIdx < nums.size()) + { + int maxJump = 0, newIdx = oldIdx; + for (int j = oldIdx; j <= oldIdx + nums[oldIdx]; j++) + { + int nextJump = j + nums[j]; + if (nextJump >= nums.size() - 1) + return steps + 2; + if (j + nums[j] > maxJump) + { + maxJump = j + nums[j]; + newIdx = j; + } + } + + oldIdx = newIdx; + steps++; + } + } +}; ``` ### C ```cpp - +class Solution +{ +public: + int jump(vector &nums) + { + int i, j, n = nums.size(); + if (n == 1) + return 0; + int ans = 0; + for (i = 0; i < n;) + { + if (i == n - 1) + break; + if (i + nums[i] >= n - 1) + { + ans++; + break; + } + else + { + int max = i + 1; + for (j = 2; j <= nums[i]; j++) + { + if (nums[max] + max <= nums[i + j] + i + j) + max = i + j; + } + ans++; + i = max; + } + } + return ans; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/45_\345\205\250\346\216\222\345\210\227/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/45_\345\205\250\346\216\222\345\210\227/solution.md" index 15e1319c6..e66b5e033 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/45_\345\205\250\346\216\222\345\210\227/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/45_\345\205\250\346\216\222\345\210\227/solution.md" @@ -3,30 +3,170 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector nums = {1, 2, 3}; + vector> res; + + res = sol.permute(nums); + + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + vector> permute(vector &nums) + { + vector> ans; + vector tem1, tem2; + if (!nums.size()) + return ans; + tem1.push_back(nums[0]); + ans.push_back(tem1); + for (int i = 1; i < nums.size(); i++) + { + int len = ans.size(); + while (len) + { + tem1 = ans[--len]; + ans.erase(ans.begin() + len); + int j = i; + while (j--) + { + tem2 = tem1; + tem2.insert(tem2.begin() + j, nums[i]); + ans.push_back(tem2); + } + } + } + return ans; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + vector> permute(vector &nums) + { + vector> res; + vector used(nums.size()); + dfs(nums, used, res); + return res; + } +private: + vector stack; + void dfs(vector &nums, vector &used, vector> &res) + { + if (stack.size() == nums.size()) + { + res.push_back(stack); + } + else + { + for (int i = 0; i < nums.size(); i++) + { + if (!used[i]) + { + used[i] = true; + stack.push_back(nums[i]); + dfs(nums, used, res); + stack.pop_back(); + used[i] = false; + } + } + } + } +}; ``` ### B ```cpp +class Solution +{ +public: + vector> permute(vector &nums) + { + vector> res; + + BT(res, nums, 0); + return res; + } + void BT(vector> &res, vector &nums, int start) + { + if (start == nums.size()) + { + res.push_back(nums); + return; + } + else + { + for (int i = start; i < nums.size(); i++) + { + swap(nums[i], nums[start]); + BT(res, nums, start + 1); + swap(nums[i], nums[start]); + } + } + } +}; ``` ### C ```cpp - +class Solution +{ +public: + vector> result; + vector> permute(vector &nums) + { + vector temp(nums.size()); + permutation(nums.size(), 0, nums, temp); + return result; + } + void permutation(int n, int index, vector &nums, vector &temp) + { + if (index == n) + { + result.push_back(temp); + return; + } + for (int i = 0; i < n; i++) + { + bool flag = true; + for (int j = 0; j <= index - 1; j++) + { + if (temp[j] == nums[i]) + flag = false; + } + if (flag) + { + temp[index] = nums[i]; + permutation(n, index + 1, nums, temp); + } + } + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/46_\345\205\250\346\216\222\345\210\227 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/46_\345\205\250\346\216\222\345\210\227 II/solution.md" index 0720de77e..d000a996e 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/46_\345\205\250\346\216\222\345\210\227 II/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/46_\345\205\250\346\216\222\345\210\227 II/solution.md" @@ -3,30 +3,203 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector nums = {1, 1, 2}; + vector> res; + + res = sol.permuteUnique(nums); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +private: + vector> result; + vector path; + void backtracking(vector &nums, vector &used) + { + + if (path.size() == nums.size()) + { + result.push_back(path); + return; + } + for (int i = 0; i < nums.size(); i++) + { + if (used[i] == false) + { + used[i] = true; + path.push_back(nums[i]); + backtracking(nums, used); + path.pop_back(); + used[i] = false; + } + } + } +public: + vector> permuteUnique(vector &nums) + { + result.clear(); + path.clear(); + sort(nums.begin(), nums.end()); + vector used(nums.size(), false); + backtracking(nums, used); + return result; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + vector> permuteUnique(vector &nums) + { + vector> res; + int n = nums.size(); + vector temp; + vector visited(n, false); + sort(nums.begin(), nums.end()); + backtrackpermuteUnique(0, n, nums, temp, res, visited); + return res; + } + void backtrackpermuteUnique(int k, int n, vector nums, vector &temp, vector> &res, vector &visited) + { + if (k == n) + { + res.push_back(temp); + return; + } + for (int i = 0; i < n; i++) + { + if (!visited[i]) + { + if (i > 0 && nums[i] == nums[i - 1] && visited[i - 1]) + continue; + temp.push_back(nums[i]); + visited[i] = true; + backtrackpermuteUnique(k + 1, n, nums, temp, res, visited); + + temp.pop_back(); + visited[i] = false; + } + } + } +}; ``` ### B ```cpp +class Solution +{ +public: + vector> permuteUnique(vector &nums) + { + vector> res; + vector used(nums.size()); + sort(nums.begin(), nums.end()); + dfs(nums, used, res); + return res; + } +private: + vector stack; + void dfs(vector &nums, vector &used, vector> &res) + { + if (stack.size() == nums.size()) + { + res.push_back(stack); + } + else + { + for (int i = 0; i < nums.size(); i++) + { + if (!used[i]) + { + if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) + { + continue; + } + stack.push_back(nums[i]); + used[i] = true; + dfs(nums, used, res); + stack.pop_back(); + used[i] = false; + } + } + } + } +}; ``` ### C ```cpp +class Solution +{ +public: + vector> permuteUnique(vector &nums) + { + vector now_p; + vector> all_p; + vector> numCnt; + + sort(nums.begin(), nums.end()); + for (int i = 0; i < nums.size();) + { + int num = nums[i], cnt = 1; + while (i + cnt < nums.size() && nums[i + cnt] == num) + ++cnt; + i += cnt; + numCnt.emplace_back(num, cnt); + } + + function generate; + generate = [&generate, &nums, &numCnt, &now_p, &all_p](int index) + { + if (index == nums.size()) + { + all_p.push_back(now_p); + return; + } + + for (auto &i : numCnt) + { + if (i.second) + { + --i.second; + now_p.emplace_back(i.first); + generate(index + 1); + now_p.pop_back(); + ++i.second; + } + } + }; + + generate(0); + return all_p; + } +}; + ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/47_\346\227\213\350\275\254\345\233\276\345\203\217/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/47_\346\227\213\350\275\254\345\233\276\345\203\217/solution.md" index 442f8b49d..2d9b663f3 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/47_\346\227\213\350\275\254\345\233\276\345\203\217/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/47_\346\227\213\350\275\254\345\233\276\345\203\217/solution.md" @@ -12,21 +12,101 @@ ## 答案 ```cpp - +class Solution +{ +public: + void rotate(vector> &matrix) + { + int n = matrix.size(); + int temp = 0; + for (int i = 0; i < n; ++i) + { + for (int j = i; j < n; ++j) + { + temp = matrix[i][j]; + matrix[i][j] = matrix[n - j - 1][i]; + matrix[j][i] = temp; + } + } + for (int i = 0; i < n; ++i) + { + reverse(matrix[i].begin(), matrix[i].end()); + } + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + void rotate(vector> &matrix) + { + int temp = 0; + int n = matrix.size(); + for (int i = 0; i < n / 2; i++) + { + for (int j = i; j < n - i - 1; j++) + { + temp = matrix[i][j]; + matrix[i][j] = matrix[n - j - 1][i]; + matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]; + matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]; + matrix[j][n - i - 1] = temp; + } + } + } +}; ``` ### B ```cpp - +class Solution +{ +public: + void rotate(vector> &matrix) + { + int n = matrix.size(); + int tmp = 0; + for (int i = 0; i < n; i++) + for (int j = i; j < n; j++) + { + tmp = matrix[j][i]; + matrix[j][i] = matrix[i][j]; + matrix[i][j] = tmp; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < n / 2; j++) + { + tmp = matrix[i][j]; + matrix[i][j] = matrix[i][n - j - 1]; + matrix[i][n - j - 1] = tmp; + } + } +}; ``` ### C ```cpp +class Solution +{ +public: + void rotate(vector> &matrix) + { + + int n = matrix.size(); + vector> matrix_temp = matrix; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + matrix_temp[j][n - i - 1] = matrix[i][j]; + } + } + matrix = matrix_temp; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/48_\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/48_\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/solution.md" index 8d02088da..b5e0a1826 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/48_\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/48_\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/solution.md" @@ -3,30 +3,153 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector strs = {"eat", "tea", "tan", "ate", "nat", "bat"}; + vector> res; + + res = sol.groupAnagrams(strs); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + vector> groupAnagrams(vector &strs) + { + vector> res; + map kvmap; + string s; + int index = 0; + for (int i = 0; i < strs.size(); i++) + { + s = strs[i]; + sort(s.begin(), s.end()); + if (kvmap.find(s) == kvmap.end()) + { + index++; + res.push_back(vector{}); + } + res[kvmap[s]].push_back(strs[i]); + } + return res; + } +}; ``` ## 选项 ### A ```cpp - +class Solution +{ +public: + vector> groupAnagrams(vector &strs) + { + vector> res; + unordered_map> ht; + for (const auto &str : strs) + { + int counts[26] = {0}; + for (char c : str) + { + counts[c - 'a']++; + } + string key; + for (int i : counts) + { + key.push_back('#'); + key.push_back(i + '0'); + } + ht[key].push_back(str); + } + for (const auto &t : ht) + { + res.push_back(t.second); + } + return res; + } +}; ``` ### B ```cpp - +class Solution +{ +public: + vector> groupAnagrams(vector &strs) + { + map> anagram; + vector> result; + for (int i = 0; i < strs.size(); i++) + { + string str = strs[i]; + sort(str.begin(), str.end()); + if (anagram.find(str) == anagram.end()) + { + std::vector item; + anagram[str] = item; + } + anagram[str].push_back(strs[i]); + } + map>::iterator it; + for (it = anagram.begin(); it != anagram.end(); it++) + { + result.push_back((*it).second); + } + return result; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + vector> groupAnagrams(vector &strs) + { + int len = strs.size(); + if (len == 0) + return {{""}}; + std::map mymap; + vector> ans; + int cur = 0; + for (int i = 0; i < len; i++) + { + string tmp(strs[i]); + sort(tmp.begin(), tmp.end()); + std::map::iterator it = mymap.find(tmp); + if (it != mymap.end()) + { + ans[it->second].push_back(strs[i]); + } + else + { + vector item; + item.push_back(strs[i]); + ans.push_back(item); + mymap.insert(make_pair(tmp, cur)); + cur++; + } + } + return ans; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/49_Pow(x, n)/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/49_Pow(x, n)/solution.md" index 751c5adfa..da40b20b7 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/49_Pow(x, n)/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/49_Pow(x, n)/solution.md" @@ -3,30 +3,136 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + double x = 2.00000; + int n = 10; + double res; + + res = sol.myPow(x, n); + cout << fixed << setprecision(5) << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + double myPow(double x, int n) + { + if (n == 0) + return 1.0; + unsigned long long u = llabs(n); + double ans = 1.0; + while (u) + { + if (u & 1) + ans *= x; + x *= x; + u >>= 1; + } + return n > 0 ? 1.0 / ans : ans; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + double myPow(double x, int n) + { + if (n == INT_MIN) + { + double t = dfs(x, -(n / 2)); + return 1 / t * 1 / t; + } + else + { + return n < 0 ? 1 / dfs(x, -n) : dfs(x, n); + } + } +private: + double dfs(double x, int n) + { + if (n == 0) + { + return 1; + } + else if (n == 1) + { + return x; + } + else + { + double t = dfs(x, n / 2); + return (n % 2) ? (x * t * t) : (t * t); + } + } +}; ``` ### B ```cpp +class Solution +{ +public: + double myPow(double x, int n) + { + if (n == 0) + return 1; + if (n % 2 == 1) + { + double temp = myPow(x, n / 2); + return temp * temp * x; + } + else if (n % 2 == -1) + { + double temp = myPow(x, n / 2); + return temp * temp / x; + } + else + + { + double temp = myPow(x, n / 2); + return temp * temp; + } + } +}; ``` ### C ```cpp +class Solution +{ +public: + double helper(double x, int n) + { + if (n == 0) + return 1.0; + double y = helper(x, n / 2); + return n % 2 == 0 ? y * y : y * y * x; + } + double myPow(double x, int n) + { + long long N = static_cast(n); + if (N == 0) + return 1; + return N > 0 ? helper(x, N) : 1. / helper(x, -N); + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/50_N \347\232\207\345\220\216/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/50_N \347\232\207\345\220\216/solution.md" index 8c97939c3..c3376d4a3 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/50_N \347\232\207\345\220\216/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/50_N \347\232\207\345\220\216/solution.md" @@ -3,30 +3,242 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int n = 4; + vector> res; + res = sol.solveNQueens(n); + for (auto i : res) + { + for (auto j : i) + cout << j << " "; + cout << endl; + } + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + bool isvalid(vector &temp, int i, int j) + { + + for (int k = 0; k < i; ++k) + { + if (temp[k][j] == 'Q') + return false; + } + for (int p = i - 1, q = j - 1; p >= 0 && q >= 0; --p, --q) + { + if (temp[p][q] == 'Q') + return false; + } + for (int p = i - 1, q = j + 1; p >= 0 && q < temp.size(); --p, ++q) + { + if (temp[p][q] == 'Q') + return false; + } + return true; + } + + void dfs(vector> &re, vector &temp, int i, int n) + { + if (i == n) + { + re.push_back(temp); + return; + } + for (int j = 0; j < n; ++j) + { + if (isvalid(temp, i, j)) + { + temp[i][j] = 'Q'; + dfs(re, temp, i, n); + } + temp[i][j] = '.'; + } + return; + } + + vector> solveNQueens(int n) + { + vector> re; + string aa; + for (int i = 0; i < n; ++i) + aa += '.'; + vector temp(n, aa); + dfs(re, temp, 0, n); + return re; + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + vector> solveNQueens(int n) + { + vector> res; + vector stack(n); + vector solution(n, string(n, '.')); + dfs(n, 0, stack, solution, res); + return res; + } +private: + void dfs(int n, int row, vector &stack, vector &solution, vector> &res) + { + if (row == n) + { + res.push_back(solution); + } + else + { + for (int i = 0; i < n; i++) + { + if (row == 0 || !conflict(stack, row, i)) + { + solution[row][i] = 'Q'; + stack[row] = i; + dfs(n, row + 1, stack, solution, res); + solution[row][i] = '.'; + } + } + } + } + bool conflict(vector &stack, int row, int col) + { + for (int i = 0; i < row; i++) + { + if (col == stack[i] || abs(row - i) == abs(col - stack[i])) + { + return true; + } + } + return false; + } +}; ``` ### B ```cpp +class Solution +{ +public: + vector> res; + vector> solveNQueens(int n) + { + vector board(n, string(n, '.')); + backtrack(board, 0); + return res; + } + + void backtrack(vector &board, int row) + { + if (row == board.size()) + { + res.push_back(board); + return; + } + int n = board[row].size(); + for (int col = 0; col < n; col++) + { + if (!isValid(board, row, col)) + { + continue; + } + board[row][col] = 'Q'; + backtrack(board, row + 1); + board[row][col] = '.'; + } + } + bool isValid(vector &board, int row, int col) + { + int n = board.size(); + for (int i = 0; i < row; i++) + { + if (board[i][col] == 'Q') + { + return false; + } + } + for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) + { + if (board[i][j] == 'Q') + { + return false; + } + } + for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) + { + if (board[i][j] == 'Q') + { + return false; + } + } + return true; + } +}; ``` ### C ```cpp - +class Solution +{ +public: + bool isValue(vector &pos, int row, int col) + { + for (int i = 0; i < row; ++i) + { + if (col == pos[i] || abs(row - i) == abs(col - pos[i])) + return false; + } + return true; + } + void solveNQueensDFS(vector &pos, int row, vector> &ans) + { + int n = pos.size(); + if (row == n) + { + vector tmp(n, string(n, '.')); + for (int i = 0; i < n; ++i) + tmp[i][pos[i]] = 'Q'; + ans.push_back(tmp); + } + else + { + for (int col = 0; col < n; ++col) + { + if (isValue(pos, row, col)) + { + pos[row] = col; + solveNQueensDFS(pos, row + 1, ans); + pos[row] = -1; + } + } + } + } + vector> solveNQueens(int n) + { + vector pos(n, -1); + vector> ans; + solveNQueensDFS(pos, 0, ans); + return ans; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/51_N\347\232\207\345\220\216 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/51_N\347\232\207\345\220\216 II/solution.md" index dac9f2825..78ea98783 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/51_N\347\232\207\345\220\216 II/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/51_N\347\232\207\345\220\216 II/solution.md" @@ -3,30 +3,214 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + int n = 4; + int res; + res = sol.totalNQueens(n); + cout << res; + return 0; +} ``` ## 答案 ```cpp +class Solution +{ +public: + int totalNQueens(int n) + { + int res = 0; + int queenPos[100]; + NQueen(res, n, 0, queenPos); + return res; + } + void NQueen(int &res, int N, int k, int queenPos[]) + { + int i; + if (k == N) + { + res++; + return; + } + for (i = 0; i < N; i++) + { + int j; + for (j = 0; j < k; j++) + { + if (queenPos[j] == i || abs(queenPos[j] - i) == abs(k - j)) + { + break; + } + } + if (j == k) + { + queenPos[k] = i; + NQueen(res, N, k, queenPos); + } + } + } +}; ``` ## 选项 ### A ```cpp +class Solution +{ +public: + int totalNQueens(int n) + { + vector stack(n); + return dfs(n, 0, stack); + } +private: + int dfs(int n, int row, vector &stack) + { + int count = 0; + if (row == n) + { + return count + 1; + } + else + { + for (int i = 0; i < n; i++) + { + if (row == 0 || !conflict(stack, row, i)) + { + stack[row] = i; + count += dfs(n, row + 1, stack); + } + } + return count; + } + } + bool conflict(vector &stack, int row, int col) + { + for (int i = 0; i < row; i++) + { + if (col == stack[i] || abs(row - i) == abs(col - stack[i])) + { + return true; + } + } + return false; + } +}; ``` ### B ```cpp +class Solution +{ +public: + void recurse(vector solution, int pos, vector> validPos, int &result) + { + int n = solution[0].size(); + if (pos == n) + { + result++; + return; + } + + for (int i = 0; i < n; i++) + { + if (!validPos[pos][i]) + continue; + + vector> newPos = validPos; + for (int j = pos; j < n; j++) + { + newPos[j][i] = false; + if (i - j + pos >= 0) + newPos[j][i - j + pos] = false; + if (i + j - pos < n) + newPos[j][i + j - pos] = false; + } + solution[pos][i] = 'Q'; + + recurse(solution, pos + 1, newPos, result); + solution[pos][i] = '.'; + } + + return; + } + int totalNQueens(int n) + { + int result = 0; + vector solution(n, string(n, '.')); + vector> validPos = vector>(n, vector(n, true)); + + recurse(solution, 0, validPos, result); + + return result; + } +}; ``` ### C ```cpp +class Solution +{ +public: + bool isvalid(vector &temp, int i, int j) + { //判断棋盘是否有效 + //for (int k = 0; k= 0 && q >= 0; --p, --q) + { //判断左上对角线 + if (temp[p][q] == 'Q') + return false; + } + for (int p = i - 1, q = j + 1; p >= 0 && q < temp.size(); --p, ++q) + { //判断右上对角线 + if (temp[p][q] == 'Q') + return false; + } + return true; + } + + int dfs(int &count, vector &temp, int i, int n) + { + if (i == n) + return ++count; + + for (int j = 0; j < n; ++j) + { + if (isvalid(temp, i, j)) + { + temp[i][j] = 'Q'; //递归前修改 + dfs(count, temp, i + 1, n); + } + temp[i][j] = '.'; //递归后恢复 + } + return count; + } + int totalNQueens(int n) + { + int count = 0; + string aa; + for (int i = 0; i < n; ++i) + aa += '.'; + vector temp(n, aa); + return dfs(count, temp, 0, n); + } +}; ``` -- GitLab