leetcode146. LRU缓存机制(双向链表+HashMap)
代码太长了,先看别的
leetcode121. 买卖股票的最佳时机(记录到当前遍历位置为止股票价格最小值,当前遍历值减最小值即为到当前位置为止最大利润,leetcode该题有视频讲解)
// 只买卖一次股票,所以要尽可能的在低点买入,高点卖出
public class Solution {
public int maxProfit(int prices[]) {
// 记录到当前遍历位置位置股票价格最小值,初始化为最大Int值,或者第一个股票价格也可
int minprice = Integer.MAX_VALUE;
// 记录到当前遍历位置位置能获取的最大利润
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
// 当前股票价格<已记录股票价格最小值,更新股票价格最小值为当前股票价格
if (prices[i] < minprice) {
minprice = prices[i];
// else if 说明当前股票价格大于等于已记录股票价格最小值,那么如果当前时间卖出,则收益是prices[i] - minprice,如果该收益大于已记录的能获取的最大利润,则更新最大利润值
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}
leetcode3. 无重复字符的最长子串(滑动窗口,leetcode该题有视频讲解)
class Solution {
public int lengthOfLongestSubstring(String s) {
// 哈希集合,记录每个字符是否出现过
Set<Character> occ = new HashSet<Character>();
// s字符串长度、无重复字符的最长子串,初始值为0、滑动窗口左侧指针
int n = s.length(), res = 0, left = 0;
// 右指针不断右移遍历
for(int right = 0; right < n; right++){
// 如果occ中不包含右指针指向字符,则将该字符放入occ中,更新res(当前无重复字符的最大长度为right-left+1,如果该值大于res,则更新res为right-left+1,否则res不变)
if(!occ.contains(s.charAt(right))){
occ.add(s.charAt(right));
res = Math.max(res, right - left + 1);
// occ中已经包含右指针指向字符,则左指针右移,移到与右指针指向字符为同一个字符的下一个位置,这时left到right的所有字符都不是重复的了。并且在移动过程中不断从occ中删除遍历到的字符
}else{
while(s.charAt(left) != s.charAt(right)){
occ.remove(s.charAt(left));
left += 1;
}
left += 1;
}
}
return res;
}
}
leetcode206. 反转链表
// 遍历链表所有节点,当前节点指向前一个节点,然后更新前一个节点为当前节点、当前节点为下一个节点
class Solution {
public ListNode reverseList(ListNode head) {
// 当前节点的前一个节点,初始为null
ListNode prev = null;
// 当前遍历节点,初始为头节点
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
// 当前节点指向前一个节点prev
curr.next = prev;
// 前一个节点prev更新为当前节点
prev = curr;
// 当前节点更新为下一个节点
curr = next;
}
return prev;
}
}
leetcode128. 最长连续序列(set集合)
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> num_set = new HashSet<Integer>();
// 将nums中的元素放到set集合中,set集合中的元素是唯一的
for (int num : nums) {
num_set.add(num);
}
// 返回结果,即最长连续序列长度,初始化为0
int longestStreak = 0;
for (int num : num_set) {
// set集合中没有num-1,则说明num为它所属连续序列中的最小值
if (!num_set.contains(num - 1)) {
// 连续序列当前值,初始化为num
int currentNum = num;
// 当前连续序列长度,初始化为1,即只有num
int currentStreak = 1;
// 如果set集合中包含currentNum + 1,则更新连续序列当前值和长度,都+1,while循环遍历,直到set集合中不包含currentNum + 1值,则该连续序列从num开始到currentNum结束
while (num_set.contains(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
// 更新返回结果值,取之前的最长连续序列长度longestStreak和当前遍历位置的连续序列长度currentStreak中的较大值
longestStreak = Math.max(longestStreak, currentStreak);
}
}
return longestStreak;
}
}
leetcode103. 二叉树的锯齿形层次遍历
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ans = new LinkedList<List<Integer>>();
if (root == null) {
return ans;
}
Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
nodeQueue.offer(root);
boolean isOrderLeft = true;
while (!nodeQueue.isEmpty()) {
Deque<Integer> levelList = new LinkedList<Integer>();
int size = nodeQueue.size();
for (int i = 0; i < size; ++i) {
TreeNode curNode = nodeQueue.poll();
if (isOrderLeft) {
levelList.offerLast(curNode.val);
} else {
levelList.offerFirst(curNode.val);
}
if (curNode.left != null) {
nodeQueue.offer(curNode.left);
}
if (curNode.right != null) {
nodeQueue.offer(curNode.right);
}
}
ans.add(new LinkedList<Integer>(levelList));
isOrderLeft = !isOrderLeft;
}
return ans;
}
}