diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 34b099a2..b6b34be3 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -467,6 +467,13 @@ class Solution: # 将列表转换成字符串 return " ".join(words) ``` +(版本三) 拆分字符串 + 反转列表 +```python +class Solution: + def reverseWords(self, s): + words = s.split() #type(words) --- list + words = words[::-1] # 反转单词 + return ' '.join(words) #列表转换成字符串 ### Go: diff --git a/problems/0417.太平洋大西洋水流问题.md b/problems/0417.太平洋大西洋水流问题.md index b8448e93..5156ce22 100644 --- a/problems/0417.太平洋大西洋水流问题.md +++ b/problems/0417.太平洋大西洋水流问题.md @@ -177,14 +177,14 @@ public: // 记录从大西洋出发,可以遍历的节点 vector> atlantic = vector>(n, vector(m, false)); - - // 从最上最下行的节点出发,向高处遍历 + + // 从最左最右列的节点出发,向高处遍历 for (int i = 0; i < n; i++) { dfs (heights, pacific, i, 0); // 遍历最左列,接触太平洋 dfs (heights, atlantic, i, m - 1); // 遍历最右列,接触大西 } - // 从最左最右列的节点出发,向高处遍历 + // 从最上最下行的节点出发,向高处遍历 for (int j = 0; j < m; j++) { dfs (heights, pacific, 0, j); // 遍历最上行,接触太平洋 dfs (heights, atlantic, n - 1, j); // 遍历最下行,接触大西洋 @@ -297,6 +297,73 @@ class Solution { } ``` +```Java +class Solution { + + // 和Carl题解更加符合的Java DFS + private int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; + + /** + * @param heights 题目给定的二维数组 + * @param m 当前位置的行号 + * @param n 当前位置的列号 + * @param visited 记录这个位置可以到哪条河 + */ + + public void dfs(int[][] heights, boolean[][] visited, int m, int n){ + if(visited[m][n]) return; + visited[m][n] = true; + + for(int[] dir: directions){ + int nextm = m + dir[0]; + int nextn = n + dir[1]; + //出了2D array的边界,continue + if(nextm < 0||nextm == heights.length||nextn <0||nextn== heights[0].length) continue; + //下一个位置比当下位置还要低,跳过,继续找下一个更高的位置 + if(heights[m][n] > heights[nextm][nextn]) continue; + dfs(heights, visited, nextm, nextn); + } + } + + + public List> pacificAtlantic(int[][] heights) { + int m = heights.length; + int n = heights[0].length; + + // 记录从太平洋边出发,可以遍历的节点 + boolean[][] pacific = new boolean[m][n]; + // 记录从大西洋出发,可以遍历的节点 + boolean[][] atlantic = new boolean[m][n]; + + // 从最左最右列的节点出发,向高处遍历 + for(int i = 0; i> result = new ArrayList<>(); + for(int a = 0; a pair = new ArrayList<>(); + pair.add(a); + pair.add(b); + result.add(pair); + } + } + } + return result; + } +} +``` + 广度优先遍历: ```Java diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 91ed40b4..9f59d3ad 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -206,8 +206,26 @@ class Solution: ``` -### Go +栈 大饼干优先 +```python +from collecion import deque +class Solution: + def findContentChildren(self, g: List[int], s: List[int]) -> int: + #思路,饼干和孩子按从大到小排序,依次从栈中取出,若满足条件result += 1 否则将饼干栈顶元素重新返回 + result = 0 + queue_g = deque(sorted(g, reverse = True)) + queue_s = deque(sorted(s, reverse = True)) + while queue_g and queue_s: + child = queue_g.popleft() + cookies = queue_s.popleft() + if child <= cookies: + result += 1 + else: + queue_s.appendleft(cookies) + return result +``` +### Go ```golang //排序后,局部最优 func findContentChildren(g []int, s []int) int { diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 47b34a0f..7b46abee 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -159,7 +159,89 @@ public: * 时间复杂度: O(kmn),k 为strs的长度 * 空间复杂度: O(mn) +C++: +使用三维数组的版本 +```CPP +class Solution { +public: + int findMaxForm(vector& strs, int m, int n) { + int num_of_str = strs.size(); + + vector>> dp(num_of_str, vector>(m + 1,vector(n + 1, 0))); + + /* dp[i][j][k] represents, if choosing items among strs[0] to strs[i] to form a subset, + what is the maximum size of this subset such that there are no more than m 0's and n 1's in this subset. + Each entry of dp[i][j][k] is initialized with 0 + + transition formula: + using x[i] to indicates the number of 0's in strs[i] + using y[i] to indicates the number of 1's in strs[i] + + dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - x[i]][k - y[i]] + 1) + + */ + + + // num_of_zeros records the number of 0's for each str + // num_of_ones records the number of 1's for each str + // find the number of 0's and the number of 1's for each str in strs + vector num_of_zeros; + vector num_of_ones; + for (auto& str : strs){ + int count_of_zero = 0; + int count_of_one = 0; + for (char &c : str){ + if(c == '0') count_of_zero ++; + else count_of_one ++; + } + num_of_zeros.push_back(count_of_zero); + num_of_ones.push_back(count_of_one); + + } + + + // num_of_zeros[0] indicates the number of 0's for str[0] + // num_of_ones[0] indiates the number of 1's for str[1] + + // initialize the 1st plane of dp[i][j][k], i.e., dp[0][j][k] + // if num_of_zeros[0] > m or num_of_ones[0] > n, no need to further initialize dp[0][j][k], + // because they have been intialized to 0 previously + if(num_of_zeros[0] <= m && num_of_ones[0] <= n){ + // for j < num_of_zeros[0] or k < num_of_ones[0], dp[0][j][k] = 0 + for(int j = num_of_zeros[0]; j <= m; j++){ + for(int k = num_of_ones[0]; k <= n; k++){ + dp[0][j][k] = 1; + } + } + } + + /* if j - num_of_zeros[i] >= 0 and k - num_of_ones[i] >= 0: + dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - num_of_zeros[i]][k - num_of_ones[i]] + 1) + else: + dp[i][j][k] = dp[i-1][j][k] + */ + + for (int i = 1; i < num_of_str; i++){ + int count_of_zeros = num_of_zeros[i]; + int count_of_ones = num_of_ones[i]; + for (int j = 0; j <= m; j++){ + for (int k = 0; k <= n; k++){ + if( j < count_of_zeros || k < count_of_ones){ + dp[i][j][k] = dp[i-1][j][k]; + }else{ + dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - count_of_zeros][k - count_of_ones] + 1); + } + } + } + + } + + return dp[num_of_str-1][m][n]; + + } +}; +``` ## 总结 diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 5c1b9c43..9ec51524 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -262,6 +262,26 @@ class Solution: return None ``` +(方法三) 栈-遍历 +```python +class Solution: + def searchBST(self, root: TreeNode, val: int) -> TreeNode: + stack = [root] + while stack: + node = stack.pop() + # 根据TreeNode的定义 + # node携带有三类信息 node.left/node.right/node.val + # 找到val直接返回node 即是找到了该节点为根的子树 + # 此处node.left/node.right/val的前后顺序可打乱 + if node.val == val: + return node + if node.right: + stack.append(node.right) + if node.left: + stack.append(node.left) + return None +``` + ### Go diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index d23f948d..b10620d0 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -178,6 +178,24 @@ class Solution: return sorted(x*x for x in nums) ``` +```Python +(版本四) 双指针+ 反转列表 +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + #根据list的先进排序在先原则 + #将nums的平方按从大到小的顺序添加进新的list + #最后反转list + new_list = [] + left, right = 0 , len(nums) -1 + while left <= right: + if abs(nums[left]) <= abs(nums[right]): + new_list.append(nums[right] ** 2) + right -= 1 + else: + new_list.append(nums[left] ** 2) + left += 1 + return new_list[::-1] + ### Go: ```Go diff --git a/problems/kamacoder/0055.右旋字符串.md b/problems/kamacoder/0055.右旋字符串.md index 28ee5e30..1d64ae0b 100644 --- a/problems/kamacoder/0055.右旋字符串.md +++ b/problems/kamacoder/0055.右旋字符串.md @@ -217,6 +217,13 @@ s = s[len(s)-k:] + s[:len(s)-k] print(s) ``` +```Python 切片法 +k = int(input()) +s = input() + +print(s[-k:] + s[:-k]) +``` + ### Go: ```go package main