一、
1、实现一个支持动态扩容的数组
还在理解
2、实现一个大小固定的有序数组,支持动态增删改操作
class Queue(object):
def __init__(self,size = 10,init_list = []):
"""初始一个队列,默认空队列"""
self.queue = init_list
self.max_size = size
def push(self, item):
"""添加一个新的元素item到队头"""
if self.is_full():
raise KeyError('queue is full')
else:
self.queue.append(item)
def pop(self):
"""弹出队尾元素"""
if self.is_empty():
raise KeyError('Queue is empty')
return self.queue.pop(0)
def is_empty(self):
"""判断队列是否为空"""
return self.queue == []
def peek(self):
"""返回队头元素"""
if self.is_empty():
raise KeyError('Queue is empty')
return self.queue[-1]
def size(self):
"""返回队列的元素个数"""
return len(self.queue)
def is_full(self):
"""判断是否满队列"""
return len(self.queue) == self.max_size
def show(self):
"""从队尾输出到队头"""
return [i for i in self.queue]
if __name__ == '__main__':
qu = Queue(10,[1,2])
qu.push(1)
qu.push(10)
qu.push(1)
qu.push(10)
qu.push(1)
qu.push(10)
qu.push(1)
qu.push(10)
print(qu.peek()) # 10
print(qu.show()) # [1, 2, 1, 10, 1, 10, 1, 10, 1, 10]
print(qu.pop()) # 1
print(qu.show()) # [2, 1, 10, 1, 10, 1, 10, 1, 10]
qu.queue[5] = 5 # 随意更改了队列内的值
print(qu.show()) # [2, 1, 10, 1, 10, 5, 10, 1, 10]
3、实现两个有序数组合并为一个有序数组 用python 特别好实现
class Solution:
def merge(self, nums1,nums2):
"""
Do not return anything, modify nums1 in-place instead.
"""
out=nums+nums2
out.sort()
print(out)
二、
1、两数之和
在python 中,字典{} 都是用哈希来做的
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
#创建一个空字典
d = {}
for x in range(n):
a = target - nums[x]
#字典d中存在nums[x]时
if nums[x] in d:
return d[nums[x]],x
#否则往字典增加键/值对
else:
d[a] = x
2、Happy Number
class Solution:
def isHappy(self, n: int) -> bool:
d = {}
while True:
listn = list(map(int,str(n)))
sum = 0
for i in listn:
sum += i**2
if sum == 1:
return True
if sum in d:
return False
n = sum
d[sum] = sum
三、
- 实现一个字符集,只包含 a~z 这 26 个英文字母的 Trie 树
class Trie: def __init__(self): """ Initialize your data structure here. """ self.root = {} self.word_end = -1 def insert(self, word): """ Inserts a word into the trie. :type word: str :rtype: void """ curNode = self.root for c in word: if not c in curNode: curNode[c] = {} curNode = curNode[c] curNode[self.word_end] = True def search(self, word): """ Returns if the word is in the trie. :type word: str :rtype: bool """ curNode = self.root for c in word: if not c in curNode: return False curNode = curNode[c] # Doesn't end here if self.word_end not in curNode: return False return True def startsWith(self, prefix): """ Returns if there is any word in the trie that starts with the given prefix. :type prefix: str :rtype: bool """ curNode = self.root for c in prefix: if not c in curNode: return False curNode = curNode[c] return True trie = Trie() li = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] for i in li: trie.insert(i) print(trie.search("r"))
2、实现朴素的字符串匹配算法
def n_matching(t, p): j, i = 0, 0 # i,j的初始化 n, m = len(t), len(p) while j < n and i < m: if t[j] == p[i]: j, i = j+1, i+1 else: j, i = j-i+1, 0 if i == m: return j-i return -1 # 不匹配返回异常值