classSolution(object): deftwoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ for index, value in enumerate(nums): left = target-value if left in nums[(index+1):]: left_index = nums.index(left) if index == left_index: left_index = left_index + nums[left_index+1:].index(left) + 1 return [index, left_index] return []
classSolution(object): deftwoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ hashmap = {} for key, value in enumerate(nums): hashmap[value] = key for i, num in enumerate(nums): j = hashmap.get(target - num) if j isnotNoneand j != i: return [i, j]
classSolution(object): defreverse(self, x): """ :type x: int :rtype: int """ if x > 0: x = int(str(x)[::-1]) else: x= int('-' + str(abs(x))[::-1]) if x < 2**31and x > -2**31: return x else: return0
classSolution(object): defisPalindrome(self, x): """ :type x: int :rtype: bool """ if x > 0: y = int(str(x)[::-1]) if x == y: returnTrue elif x == 0: returnTrue else: returnFalse
#13_roman-to-integer 罗马数字转整数
Question
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
例如: ‘MMCMLXIV’: 第一位是M,它的后一位也是M,所以 num = 0 + 1000,之后 M > C, num = num + 1000 =2000,之后到特殊值,因为C<M,所以num=num-C =2000-100=1900,之后再加上1000得到 num=2900……以此类推进行下去,最后可得到 num = 2964
for i in range(len(s)): if s[i] in left_par: l.append(s[i]) elif l == [] and s[i] in right_par: returnFalse else: if left_par.index(l.pop(-1)) != right_par.index(s[i]): returnFalse if l == []: returnTrue else: returnFalse
方法二
其实思路是一样的,关键是用字典来替换方法一的两个初始列表
1 2 3 4 5 6 7 8
classSolution: defisValid(self, s: str) -> bool: dic = {'{': '}', '[': ']', '(': ')', '?': '?'} stack = ['?'] for c in s: if c in dic: stack.append(c) elif dic[stack.pop()] != c: returnFalse return len(stack) == 1
classSolution(object): defremoveDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ i = 0 for j in range(1, len(nums)): if nums[i] != nums[j]: i = i+1 nums[i] = nums[j]
return i+1
#27_remove-element
Question
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
Example
示例一
1 2 3 4 5
给定 nums = [3,2,2,3], val = 3,
函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。
你不需要考虑数组中超出新长度后面的元素。
示例二
1 2 3 4 5 6 7
给定 nums = [0,1,2,2,3,0,4,2], val = 2,
函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。
注意这五个元素可为任意顺序。
你不需要考虑数组中超出新长度后面的元素。
Solution
方法一
思路
首先遍历循环nums,并使用count来计数有多少个待删除的元素;
再执行count次remove函数删除nums中值为val的元素
1 2 3 4 5 6 7 8 9 10 11
classSolution: defremoveElement(self, nums: List[int], val: int) -> int: count = 0 for i in range(len(nums)): if nums[i] == val: count = count+1 for j in range(count): nums.remove(val) return len(nums)
classSolution(object): defremoveDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ i = 0 for j in range(len(nums)): if nums[j] != val: nums[i] = nums[j] i = i+1
classSolution: defstrStr(self, haystack: str, needle: str) -> int: needle_len = len(needle) if needle_len == 0: return0 char = needle[0] if char notin haystack: return-1 else: for i in range(len(haystack)): if char == haystack[i]: if haystack[i:(i+needle_len)] == needle: return i return-1
方法二
思路
和方法一的思路相似,只是代码写的更为简洁,但是感觉更为暴力。
1 2 3 4 5 6
classSolution: defstrStr(self, haystack: str, needle: str) -> int: for i in range(len(haystack) - len(needle)+1): if haystack[i:i+len(needle)] == needle: return i return-1