Skip to content

Commit ca1869b

Browse files
authored
Add files via upload
1 parent e340cc4 commit ca1869b

File tree

83 files changed

+1093
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1093
-0
lines changed

leetcode/012.整数转罗马数字.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def intToRoman(self, num):
3+
res = ''
4+
d = {'M': 1000, 'CM': 900, 'D': 500, 'CD': 400, 'C': 100, 'XC': 90,
5+
'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1}
6+
# 字典无序,需要先排序,再逆转
7+
for char, val in sorted(d.items(), key = lambda x: x[1])[::-1]:
8+
while num >= val:
9+
res += char
10+
num -= val
11+
return res

leetcode/013.罗马数字转整数.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 字符的值比后面小则可以合并,即先减当前数,再加后面数
2+
class Solution(object):
3+
def romanToInt(self, s):
4+
d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
5+
res = 0
6+
for i in range(len(s)):
7+
if i<len(s)-1 and d[s[i]] < d[s[i+1]]:
8+
res -= d[s[i]]
9+
else:
10+
res += d[s[i]]
11+
return res

leetcode/014.最长公共前缀.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 方法一
2+
class Solution(object):
3+
def longestCommonPrefix(self, strs):
4+
if not strs:
5+
return ''
6+
# 遍历索引
7+
for i in range(len(strs[0])):
8+
# 遍历每个字符串
9+
for str in strs:
10+
# 索引超过字符串长度或字符不相等
11+
if len(str) <= i or strs[0][i] != str[i]:
12+
return strs[0][:i]
13+
return strs[0]
14+
15+
# 方法二:使用python函数
16+
class Solution(object):
17+
def longestCommonPrefix(self, strs):
18+
return os.path.commonprefix(strs)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 递归
2+
class Solution(object):
3+
def letterCombinations(self, digits):
4+
d = {'2':'abc', '3':'def', '4':'ghi', '5':'jkl', '6':'mno', '7':'pqrs', '8':'tuv', '9':'wxyz'}
5+
6+
def combine(s, digits):
7+
# 终止条件,数字为空时把字符串添加到数组中
8+
if not digits:
9+
res.append(s)
10+
else:
11+
for char in d[digits[0]]:
12+
combine(s+char, digits[1:])
13+
14+
res = []
15+
if not digits:
16+
return res
17+
combine('', digits)
18+
return res

leetcode/020.有效的括号.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 利用栈找对应括号,遇到左括号则进栈,遇到右括号则出栈,判断两个括号是否匹配
2+
class Solution(object):
3+
def isValid(self, s):
4+
left, right = '([{', '}])'
5+
stack = []
6+
for i in s:
7+
if i in left:
8+
stack.append(i)
9+
if i in right:
10+
if not stack:
11+
return False
12+
res = stack.pop()
13+
if (i==')' and res!='(') or (i==']' and res!='[') or (i=='}' and res!='{'):
14+
return False
15+
return not stack

leetcode/022.括号生成.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def generateParenthesis(self, n):
3+
self.res = []
4+
self.generate('', 0, 0, n)
5+
return self.res
6+
7+
def generate(self, s, left, right, n):
8+
# 左右括号用完时,添加该括号字符串
9+
if left == n and right == n:
10+
self.res.append(s)
11+
# 左括号未完时,添加左括号,再递归
12+
if left < n:
13+
self.generate(s+'(', left+1, right, n)
14+
# 右括号少于左括号时,可添加右括号,再递归
15+
if right < left:
16+
self.generate(s+')', left, right+1, n)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 遍历数组,遇到不重复的值就按顺序复制到前面
2+
class Solution(object):
3+
def removeDuplicates(self, nums):
4+
if not nums:
5+
return 0
6+
j = 0
7+
for i in range(len(nums)):
8+
if nums[i] != nums[j]:
9+
nums[j+1] = nums[i]
10+
j += 1
11+
return j+1

leetcode/027.移除元素.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 将非val的值复制到数组前,最后一个复制完后,返回的索引即前面数组的长度
2+
class Solution(object):
3+
def removeElement(self, nums, val):
4+
j = 0
5+
for i in range(len(nums)):
6+
if nums[i] != val:
7+
nums[j] = nums[i]
8+
j += 1
9+
return j

leetcode/028.实现strStr().py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 方法一
2+
class Solution(object):
3+
def strStr(self, haystack, needle):
4+
if not needle:
5+
return 0
6+
elif needle not in haystack:
7+
return -1
8+
else:
9+
return haystack.index(needle)
10+
11+
# 方法二
12+
class Solution(object):
13+
def strStr(self, haystack, needle):
14+
return haystack.find(needle)

leetcode/035.搜索插入位置.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def searchInsert(self, nums, target):
3+
if target in nums:
4+
return nums.index(target)
5+
for i in range(len(nums)):
6+
if nums[i] > target:
7+
return i
8+
return i+1

0 commit comments

Comments
 (0)