Given a string expression of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. You may return the answer in any order.
1 <= expression.length <= 20expression consists of digits and the operator '+', '-', and '*'. * All the integer values in the input expression are in the range [0, 99].
题目大意:
给定一个字符串含数字和加减乘除,求所有加括号方法得到的结果
Catalan解题思路(推荐):
求所有结果,用DFS,由于需要左右递归,双边递归,所以用Catalan法模板
解题步骤:
N/A
注意事项:
终止条件返回是一个list
Python中用eval来计算字符串运算结果返回值为整数,所以归纳左右递归结果要用str转为字符串
Python代码:
1 2 3 4 5 6 7 8 9 10 11
defdiffWaysToCompute(self, expression: str) -> List[int]: if expression.isdigit(): return [int(expression)] # remember to use list res = [] for i, char inenumerate(expression): if char notin'+-*/': continue left_res = self.diffWaysToCompute(expression[:i]) right_res = self.diffWaysToCompute(expression[i + 1:]) res += [eval(str(_l) + char + str(_r)) for _l in left_res for _r in right_res] # remember eval and str return res
defdfs(self, expression: str, cache) -> List[int]: if expression.isdigit(): return [int(expression)] # remember to use list if expression in cache: return cache[expression] res = [] for i, char inenumerate(expression): if char notin'+-*/': continue left_res = self.dfs(expression[:i], cache) right_res = self.dfs(expression[i + 1:], cache) res += [eval(str(_l) + char + str(_r)) for _l in left_res for _r in right_res] # remember eval and str cache[expression] = res return res
Given an array of strings wordsDict and two different strings that already exist in the array word1 and word2, return the shortest distance between these two words in the list.
`1 <= wordsDict.length <= 3 104*1 <= wordsDict[i].length <= 10*wordsDict[i]consists of lowercase English letters.
*word1andword2are inwordsDict.
*word1 != word2`
题目大意:
求单词列表中给定的两个单词的最短下标距离
解题思路:
同向双指针,扫一遍。贪婪法,肯定相邻,所以扫一遍
解题步骤:
N/A
注意事项:
同向双指针,分别指向两单词,计算结果时必须是找到才比较
Python代码:
1 2 3 4 5 6 7 8 9 10 11
defshortestDistance(self, wordsDict: List[str], word1: str, word2: str) -> int: p1 = p2 = -1 res = float('inf') for i, word inenumerate(wordsDict): if word == word1: p1 = i if word == word2: p2 = i if p1 != -1and p2 != -1: res = min(res, abs(p1 - p2)) return res
Given an m x n binary grid grid where each 1 marks the home of one friend, return the minimal total travel distance.
The total travel distance is the sum of the distances between the houses of the friends and the meeting point.
The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
Example 1:
Input: grid = [[1,0,0,0,1],[0,0,0,0,0],[0,0,1,0,0]] Output: 6 Explanation: Given three friends living at (0,0), (0,4), and (2,2). The point (0,2) is an ideal meeting point, as the total travel distance of 2 + 2 + 2 = 6 is minimal. So return 6.
Example 2:
Input: grid = [[1,1]] Output: 1
Constraints:
m == grid.lengthn == grid[i].length 1 <= m, n <= 200grid[i][j] is either 0 or 1. There will be *at least two friends in the grid.
defminTotalDistance(self, grid: List[List[int]]) -> int: x_coordinates, y_coordinates = [], [] for i inrange(len(grid)): for j inrange(len(grid[0])): if grid[i][j] == 1: x_coordinates.append(i) y_coordinates.append(j) x_coordinates.sort() y_coordinates.sort() res = 0 left, right = 0, len(y_coordinates) - 1 while left < right: res += y_coordinates[right] - y_coordinates[left] left += 1 right -= 1
left, right = 0, len(x_coordinates) - 1 while left < right: res += x_coordinates[right] - x_coordinates[left] left += 1 right -= 1 return res