题目
EASY
String
14. Longest Common Prefix(zip和set用法)
找到前几个一样的字母
1 找到所有字符串最短长度,遍历到该长度找最长字符串
# ans = ""
# minlen = self.minLen(strs)
# if minlen == 0: return ""
# for i in range(minlen):
# if self.prefixCheck(i,strs)==True:
# ans = strs[0][:i+1]
# else:
# break
# return ans
2 算出最小的字符串和最大的字符串,比较他们两个即可
str1, str2 = min(strs), max(strs)
print(str1)
print(str2)
i = 0
while i < len(str1):
if str1[i] != str2[i]:
str1 = str1[:i]
i +=1
return str1
3.用zip和set解决
strs = ["flower","flow","flight"]
y = zip(strs)
print(list(y))
>>> [('flower',), ('flow',), ('flight',)]
l = list(zip(*strs))
>>> l = [('f', 'f', 'f'), ('l', 'l', 'l'), ('o', 'o', 'i'), ('w', 'w', 'g')]
set(['l','l','l'])
>>> ["l"] # 用集合原理筛掉重复项输出无序的集合结果
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
l = list(zip(*strs))
prefix = ""
for i in l:
if len(set(i))==1:
prefix += i[0]
else:
break
return prefix
28. Implement strStr()*KMP算法
输出出现该字符串的位置
- 找到第一个位置截取片段进行比较
if len(haystack) ==0: return 0
if haystack == needle : return 0
i =0
while i < len(haystack)-len(needle)+1:
if haystack[i] ==needle[0]:
if needle == haystack[i:i+len(needle)]:
return i
else:
i = i+1
else:
i = i+1
return -1
- find函数
def strStr(self, haystack, needle):
return haystack.find(needle)
- Rabin Karp, built-in hash, constant time (tested)
def strStr(self, haystack, needle):
n, h = len(needle), len(haystack)
hash_n = hash(needle)
for i in range(h-n+1):
if hash(haystack[i:i+n]) == hash_n:
return i
return -1
- Rabin Karp, numeral base for both uppercase and lowercase letters, constant time
def strStr(self, haystack, needle):
def f(c):
return ord(c)-ord('A')
n, h, d, m = len(needle), len(haystack), ord('z')-ord('A')+1, sys.maxint
if n > h: return -1
nd, hash_n, hash_h = d**(n-1), 0, 0
for i in range(n):
hash_n = (d*hash_n+f(needle[i]))%m
hash_h = (d*hash_h+f(haystack[i]))%m
if hash_n == hash_h: return 0
for i in range(1, h-n+1):
hash_h = (d*(hash_h-f(haystack[i-1])*nd)+f(haystack[i+n-1]))%m # e.g. 10*(1234-1*10**3)+5=2345
if hash_n == hash_h: return i
return -1
- KMP
def strStr(self, haystack, needle):
n, h = len(needle), len(haystack)
i, j, nxt = 1, 0, [-1]+[0]*n
while i < n: # calculate next array
if j == -1 or needle[i] == needle[j]:
i += 1
j += 1
nxt[i] = j
else:
j = nxt[j]
i = j = 0
while i < h and j < n:
if j == -1 or haystack[i] == needle[j]:
i += 1
j += 1
else:
j = nxt[j]
return i-j if j == n else -1
58. Length of Last Word(two pointer 和split函数)
最后一个单词的长度
- two pointers
i = len(s)-1
while s[i] == " " and i >= 0:
i = i-1
if i == -1:
return len(s)# 没有空格:直接输出
j = i
while s[j] != " " and j >= 0:
j = j-1
return i-j
- split函数
wordlist = s.split() # 按空格分开为列表
if wordlist:
return len(wordlist[-1])
return 0
Array
26. Remove Duplicates from Sorted Array
删掉重复项
- 后面找到的数直接覆盖修改到前面
i = 0
for j in range(1,len(nums)):
if nums[j] == nums[i]:
pass
else:
i =i+1
nums[i] = nums[j]
return i+1
- pop掉
i =1
while i<len(nums):
if nums[i] ==nums[i-1]:
nums.pop(i)
else:
i = i+1
27. Remove Element(Two Pointer)
移除列表内某个值:
- two pointer:如果重复把后面的值贴到前面再检查直到两指针重合
i = 0
j = len(nums)-1
while i <= j:
if nums[i]!= val:
i = i+1
else:
nums[i] = nums[j]
j = j-1#不动i,再检查贴过来的值
return i
- pop掉(必须用while否则会超范围)
i= 0
while i < len(nums):
if nums[i] == val:
nums.pop(i)
else:
i = i+1
35. Search Insert Position(Binary Search)
搜索数字该插入的位置
边缘样例:数字插在最后面情况
- 遍历到小于和大于之间后输出
for i in range(len(nums)):
if nums[i]<target:
pass
else:
return i
return i+1
- python 自带函数
nums.append(target)
nums.sort()
return nums.index(target)
- 二分查找
start = 0
end = len(nums)
while start<end:
mid = ( start + end )//2
if nums[mid] < target:
start = mid +1
elif nums[mid] == target:
return mid
else:
end = mid
return start