随笔分类 - leetcode
摘要:15. 三数之和 class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> res = new ArrayList<>(); for(int k
阅读全文
摘要:11. 盛最多水的容器 双指针,移动短板,更新结果 class Solution { public int maxArea(int[] height) { int i = 0, j = height.length - 1, res = 0; while(i < j) { res = height[i
阅读全文
摘要:5. 最长回文子串 1.状态定义 //dp[i][j]表示s[i…j]是否为回文串 2.状态转移方程 dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1] 3.边界值处理 dp[i][i] = true class Solution { public Strin
阅读全文
摘要:4. 寻找两个正序数组的中位数 1 class Solution { 2 public double findMedianSortedArrays(int[] nums1, int[] nums2) { 3 int n = nums1.length; 4 int m = nums2.length;
阅读全文
摘要:76. 最小覆盖子串 class Solution { public String minWindow(String s, String t) { //记录字符串当前s窗口中各字符的数量 HashMap<Character, Integer> hs = new HashMap<>(); //记录字符
阅读全文
摘要:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; }
阅读全文
摘要:14. 最长公共前缀 class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length == 0) return ""; //初始化,res为第一个字符串 String res = strs[0];
阅读全文
摘要:维护一个进位标志符carry,利用stringBuilder一位一位append,最后reverse。 https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475 import java.util.*; public clas
阅读全文
摘要:1.反转链表 双指针法,先存cur.next,然后cur.next指向pre,接着往后移。 206. 反转链表 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next;
阅读全文
摘要:思路很简单,首先输出res.next可以避免链表头结点是0的问题。 然后不能在第一个while循环结束后直接把剩下的链表接上来,因为可能存在进位,仍然需要一步一步递加。 最后一位也有可能产生进位,如果有进位直接new ListNode(1)即可。 面试题 02.05. 链表求和 /** * Defi
阅读全文
摘要:1.状态定义 dp[i]表示组成面额 i,有多少种方案。 2.状态转移方程 int[] coins = new int[]{1,5,10,25}; for(int coin: coins) { dp[k] += dp[k - coin]; } 比如dp[36] = dp[36-1] + dp[36
阅读全文
浙公网安备 33010602011771号