Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
题目大意:
最大子数组和
算法思路:
dp[i] = max(dp[i-1] + nums[i], nums[i])
注意事项:
引入全局最大的res,因为递归式是以末位为结尾的最大和
Python代码:
1 2 3 4 5 6 7 8 9 10
# dp[i] = max(dp[i-1] + nums[i], nums[i]) defmaxSubArray(self, nums: List[int]) -> int: sum, res = -sys.maxsize, -sys.maxsize for num in nums: if sum > 0: sum += num else: sum = num res = max(sum, res) return res
Given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order. Example 1:
Input: s = “owoztneoer” Output: “012”
Example 2:
Input: s = “fviefuro” Output: “45”
Constraints:1 <= s.length <= 10<sup>5</sup>s[i] is one of the characters ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]. s is *guaranteed to be valid.
1 <= strs.length <= 10<sup>4</sup>0 <= strs[i].length <= 100 * strs[i] consists of lowercase English letters.
题目大意:
对同字母不同序单词分组
算法思路:
N/A
注意事项:
list(id_to_words.values())要转成list
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
defgroupAnagrams(self, strs: List[str]) -> List[List[str]]: id_to_words = collections.defaultdict(list) for word in strs: id_to_words[self.get_id(word)].append(word) return list(id_to_words.values()) # remember to convert it to list
defget_id(self, word): char_to_freq = collections.Counter(word) res = '' for c in string.ascii_lowercase: if c in char_to_freq: res += c + str(char_to_freq[c]) return res
There are n gas stations along a circular route, where the amount of gas at the i<sup>th</sup> station is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the i<sup>th</sup> station to its next (i + 1)<sup>th</sup> station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas and cost, return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return-1. If there exists a solution, it is guaranteed to be unique
Example 1:
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] Output: 3 Explanation: Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 4. Your tank = 4 - 1 + 5 = 8 Travel to station 0. Your tank = 8 - 2 + 1 = 7 Travel to station 1. Your tank = 7 - 3 + 2 = 6 Travel to station 2. Your tank = 6 - 4 + 3 = 5 Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3. Therefore, return 3 as the starting index.
Example 2:
Input: gas = [2,3,4], cost = [3,4,3] Output: -1 Explanation: You can’t start at station 0 or 1, as there is not enough gas to travel to the next station. Let’s start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 0. Your tank = 4 - 3 + 2 = 3 Travel to station 1. Your tank = 3 - 3 + 3 = 3 You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3. Therefore, you can’t travel around the circuit once no matter where you start.
Constraints:
gas.length == ncost.length == n 1 <= n <= 10<sup>5</sup>0 <= gas[i], cost[i] <= 10<sup>4</sup>
题目大意:
N/A
算法思路:
只要总gas >= 总cost,就总有一个点满足gas-cost为非负
注意事项:
先判断不合法的情况sum(gas) < sum(cost)
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12
defcanCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: if sum(gas) < sum(cost): return-1 sum_gas, sum_cost, pos = 0, 0, 0 for i in range(len(gas)): sum_gas += gas[i] sum_cost += cost[i] if sum_gas < sum_cost: pos = i + 1 sum_gas = 0 sum_cost = 0 return pos
Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
Input: s = “1 + 1” Output: 2
Example 2:
Input: s = “ 2-1 + 2 “ Output: 3
Example 3:
Input: s = “(1+(4+5+2)-3)+(6+8)” Output: 23
Constraints:
`1 <= s.length <= 3 105*sconsists of digits,‘+’,‘-‘,‘(‘,‘)’, and‘ ‘.
*srepresents a valid expression.
*‘+’is **not** used as a unary operation (i.e.,“+1”and“+(2 + 3)”is invalid).
*‘-‘could be used as a unary operation (i.e.,“-1”and“-(2 + 3)”` is valid). There will be no two consecutive operators in the input.
Every number and running calculation will fit in a signed 32-bit integer.
# 1-(2+3)+(4+5) # 1+2+3 defcalculate(self, s: str) -> int: res, num, stack, sign = 0, 0, [], 1 s += '+' for char in s: if char == ' ': continue if char.isdigit(): num = num * 10 + int(char) if char == '+': res += sign * num sign = 1 num = 0 if char == '-': res += sign * num sign = -1 num = 0 if char == '(': stack.append(res) # [-4+] stack.append(sign) # sign = 1 res = 0 if char == ')': res += sign * num # 9 prev_sign = stack.pop() # + tmp = stack.pop() # -4 res = tmp + prev_sign * res # -4 +9 # sign = 1 next one will be +-, so num = 0 and sign doesn't matter num = 0 # else: # res += char return res