并不是全面总结,仅供参考
1.队列
1.1python队列
queue = []
queue.append(x) #插入元素到尾部
queue.pop(0) #弹出第一个元素
len(queue) #队列长度
1.2python优先级队列
import heapq
class CompareAble:
def __init__(self,jobname):
self.priority = jobname #自定义优先级
self.jobname = jobname
# def __cmp__(self, other):
# if self.priority < other.priority:
# return -1
# elif self.priority == other.priority:
# return 0
# else:
# return 1
def __lt__(self, other):
if self.priority < other.priority: #优先级值越小越靠前
return True
else:
return False
nums = [1,10,2,3,4,4,5,6]
queue = []
for i in range(len(nums)):
heapq.heappush(queue,CompareAble(nums[i]))
for i in range(len(queue)):
print(heapq.heappop(queue).jobname)
1.3java队列
Queue<Integer> queue = new LinkedList<>();
Queue<TreeNode> queue = new LinkedList<>();
Queue<ListNode> queue = new LinkedList<>();
queue.add(index,x) #插入元素到index,index不填默认为最后
queue.poll() #弹出第一个元素
queue.size() #队列长度
queue.isEmpty()
2.栈
2.1python栈1
stack = []
stack.append(x) #插入元素到尾部
queue.pop() #弹出最后一个元素
python栈2
import heapq
stack = []
heapq.heappush(stack,x) #插入元素到栈中
heapq.heappop(stack) #弹出最后一个元素
2.2java栈
Stack<Integer> stack = new LinkedList<>();
stack.push(x) #插入元素
stack.pop() #弹出栈顶元素
queue.size() #队列长度
queue.isEmpty()
3.哈希表
3.1python
hash0 = {}
hash0[num] = hash0.get(num,0)+1 #字典键值对
3.2java哈希
HashMap<Integer, Integer> hash1 = new HashMap<>();
hash1.containsKey(target); //搜寻是否有目标键
hash1.put(num, hash1.getOrDefault(num, 0) + 1); //给键num赋值未赋值的num默认为0,加1
Set<Integer> hash2 = new HashSet<>();
hash2.add(num); //
4.字符串
4.1python字符串
s = "abcde123456%&^"
for i in range(len(s)):
print(s[i])
ord(s[i]) #ascii码
4.2java字符串
s = "abcde123456%&^"
for(char c : s.toCharArray()){
//TODO
}
for (int i = 0; i < s.length(); i++) {
char sc = s.charAt(i), tc = t.charAt(i);
}
s.substring(left,right) //左闭右开
s.equals(b)//判断是否相等