- 博客(247)
- 收藏
- 关注
原创 Minimum Window Substring
传承自上一题,但又有很多要注意的地方。尤其是这个:map.put(s.charAt(prevIdx), map.get(s.charAt(prevIdx)) + 1); //if (map.get(s.charAt(prevIdx)) >= 0) { "bba", "ab" => "bba" i
2016-06-30 18:23:49 311
原创 Substring with Concatenation of All Words
参考小莹子同学的点击打开链接自己刚才一直在纠结这段code,其实就是保证还剩下最后一个当前满足条件的word能留下。if (curDict.get(temp) < dict.get(temp)) { count--; }ArrayList list = findSubstring("
2016-06-30 16:44:46 266
原创 Insert Interval
顺着前一题的思路,这一题做得很顺,除了对于list为0的情况考虑欠周,别的情况都考虑到了。加油!/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Inter
2016-06-30 12:21:11 224
原创 Merge Intervals
学会,熟练用排序,不要犯傻傻的错误。联系会议室问题。/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) {
2016-06-30 11:49:07 241
原创 Reverse Nodes in k-Group
这道题让自己做了很久,光是草稿纸画图,就用了3页。但我们觉得这是值得的!我们必须得有认真钻研的劲头,必须要搞得明明白白,不留死角。当中颠倒顺序的做法同之前的模板有不同。之前的是颠倒整个(子)list的顺序,不用考虑之后的链接。而这个是颠倒list中间一部分的顺序,必须要考虑整个list的链接。方法当中的prev和next是边界,不参与顺序颠倒,last是最开始要参与颠倒的节点,也是
2016-06-30 10:19:43 270
原创 Read N Characters Given Read4 II - Call multiple times
同I,此题还是最重要的还是得能准确理解题意,然后对着I的解法稍作修改,做的时候先把I写一遍,再改成II,这是最好的。/* The read4 API is defined in the parent class Reader4. int read4(char[] buf); */public class Solution extends Reader4 { Queue
2016-06-30 07:28:29 272
原创 Read N Characters Given Read4
/* The read4 API is defined in the parent class Reader4. int read4(char[] buf); */public class Solution extends Reader4 { /** * @param buf Destination buffer * @param n Maximum
2016-06-30 04:45:52 216
原创 Alien Dictionary
很好的参考:点击打开链接此题关键是想到通过建图,再拓扑排序来解。思路一定要活。每个不同的字母对就可以代表一条边。public class Solution { public String alienOrder(String[] words) { StringBuilder order = new StringBuilder(); if (word
2016-06-30 03:55:52 497
原创 H-Index II
对于此题,背诵就够了。不过要明白的是,n-index得到的是大于等于这个index的数量,即在数组中,大于等于citation[i](含自己)的数量。最后返回n-min。常用0,1,3,5,6,7,8作例子吧public class Solution { public int hIndex(int[] citations) { int n = citatio
2016-06-29 13:12:12 299
原创 Sqrt(x)
这个记住是2分法处理,i从0,j从x/2+1开始算。当中,必须用long而非int,否则遇到类似2147395599的大数,会超过int的范围,出差。public class Solution { public int mySqrt(int x) { long i = 0; long j = x/2 + 1; while (i <=
2016-06-29 11:56:48 290
原创 Pow(x, n)
最朴素的想法会超时,当n特别大的时候。pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==0,返回1。public class Solution { public double myPow(double x, int n) { if (n < 0) {
2016-06-29 11:31:56 323
原创 Course Schedule II
做了I,这个II就是在每次找出度数为0的课程时,将其加入数组中,并返回数组,整体程序没有变化。但当中两个注意:1. 课程数组为0时,即没有前置课程,那么直接打印返回0-n-1;2. 若遇到环,无法完成课程的情况,返回的是new int[0],而不是new int[numCourses]public class Solution { public int[] findOrde
2016-06-29 08:23:53 269
原创 Course Schedule
从这道题给自己很多启示:1. 以后对于corner case,可以扔异常,这也是昨天的经验;2. 数组courses是记录每节课的度数,对于度数为0的则是最基础的课程,直接加入queue;3. 接着就是纯粹的图的拓扑排序。public class Solution { public boolean canFinish(int n, int[][] edges) { if
2016-06-29 08:18:44 271
原创 Flatten Nested List Iterator
与其他的flatten题互动。这里主要明确的一点是,要用stack,每次next之后,可以pop掉,这样的话,需要在每次是以倒序的方式将NestedInteger压入栈中。/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or s
2016-06-29 04:45:47 269
原创 每天20篇blog的限制
现在看看能不能发啊!!!!!!!!!!!csdn竟然还有每天20篇blog的限制,是说我每天最多只能刷20道题吗???????????幸好还好有备份,哈哈哈哈啦啦啦啦,红红火火恍恍惚惚哼哼哈嘿好了,现在去吃午饭。
2016-06-27 01:52:56 747
原创 Intersection of Two Linked Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2016-06-26 17:34:01 254
原创 MinStack
public class MinStack { Stack stack; Stack miniStack; /** initialize your data structure here. */ public MinStack() { stack = new Stack<>(); miniStack = new Stack<>()
2016-06-26 17:16:02 273
原创 LRU Cache
定义的Node中的key是有用的:用于从map中删掉the least used Node。public class LRUCache { private class Node{ Node prev; Node next; int value; int key; public Node(int key,
2016-06-26 17:03:38 253
原创 Copy List with Random Pointer
/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * };
2016-06-26 16:14:10 207
原创 Linked List Cycle
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class
2016-06-26 16:00:44 167
原创 Word Ladder II
public class Solution { public List> findLadders(String beginWord, String endWord, Set wordList) { List> res = new LinkedList<>(); Set visited = new HashSet<>(); Set unVisi
2016-06-26 15:19:09 353
原创 Word Break
public class Solution { public boolean wordBreak(String s, Set wordDict) { boolean[] res = new boolean[s.length() + 1]; res[0] = true; for (int i = 0; i < s.length(); i++)
2016-06-26 14:53:07 226
原创 Word Ladder
public class Solution { public int ladderLength(String beginWord, String endWord, Set wordList) { Queue queue = new LinkedList<>(); WordNode wn = new WordNode(beginWord, 1);
2016-06-26 14:40:58 242
原创 Binary Tree Level Order Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {
2016-06-26 14:19:02 191
原创 Validate Binary Search Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {
2016-06-26 14:09:22 210
原创 Gray Code
public class Solution { public List grayCode(int n) { List list = new LinkedList<>(); list.add(0); for (int i = 0; i < n; i++) { int highbit = 1 << i;
2016-06-26 13:46:57 210
原创 Trapping Rain Water
public class Solution { public int trap(int[] height) { if (height == null || height.length < 3) { return 0; } int start = 0, end = height.length - 1; i
2016-06-26 13:05:08 237
原创 Merge Two Sorted Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode merg
2016-06-26 12:33:05 204
原创 Longest Palindromic Substring
public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) { return ""; } if (s.length() == 1) { ret
2016-06-26 09:58:20 231
原创 Longest Substring Without Repeating Characters
public class Solution { public int lengthOfLongestSubstring(String s) { if (s == null || s.length() < 1) { return 0; } char[] chars = s.toCharArray(); i
2016-06-26 09:35:41 215
原创 Add Two Numbers
一定要小心,不能互相影响// carry = (num + carry)/10;// num = (num + carry)%10; int res = num + carry; carry = res/10; num = res%10;public ListNode addTwoNumbers(ListNode l
2016-06-26 09:16:48 285
原创 Valid Parentheses
public class Solution { public boolean isValid(String s) { if (s == null || s.length() % 2 == 1) { return false; } Stack stack = new Stack<>(); for (int
2016-06-26 08:44:45 191
原创 Valid Anagram
public class Solution { public boolean isAnagram(String s, String t) { if (s == null || t == null || s.length() != t.length()) { return false; } Map map = new H
2016-06-26 08:22:34 182
原创 Two Sum
public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; Map map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums
2016-06-26 08:02:31 233
原创 Best Time to Buy and Sell Stock
public int maxProfit(int[] prices) { int min = Integer.MAX_VALUE; //int max = Integer.MIN_VALUE; int max = 0; for (int p: prices) { min = Math.min(min, p);
2016-06-26 07:50:31 220
原创 Word Ladder II
leetcode中最难的一道,常做常新public class Solution { public List> findLadders(String beginWord, String endWord, Set wordList) { List> res = new LinkedList<>(); Set visited = new HashSet<>(
2016-06-25 18:36:08 560
原创 Sliding Window Maximum
队列中存的是下标,这个队列是双向的,即我们对队列头操作,也对队列尾操作。参考:点击打开链接当我们遇到新的数时,将新的数和双向队列的末尾比较,如果末尾比新数小,则把末尾扔掉,直到该队列的末尾比新数大或者队列为空的时候才住手。这样,我们可以保证队列里的元素是从头到尾降序的,由于队列里只有窗口内的数,所以他们其实就是窗口内第一大,第二大,第三大...的数。public class Sol
2016-06-24 23:34:39 266
原创 Merge k Sorted Lists
基本功中的基本功。所以还忘了最开始的错误检测!!!!!!/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Sol
2016-06-24 22:23:15 214
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人