
java
文章平均质量分 71
Dylan_Java_NYC
练很重要,总结更重要,感谢优快云给了我这么好的平台交流。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode Binary Tree Zigzag Level Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/这道题是BFS的变形,与Binary Tree Level Order Traversal相似。但是要求偶数行从左到右,奇数行从右到左。这种顺序正反交替可以用两个stack来实现。一个用来读取,一个用来存储下一层节点。Time O原创 2015-08-21 07:48:21 · 364 阅读 · 0 评论 -
LeetCode Min Stack
原题链接在这里: https://leetcode.com/problems/min-stack/push(), pop(),top() 都是原有的API, 关键是getMin(). 可以用另一个stack, min_stk来维护这个最小值。push()时检测x是否小于等于min_stk 的peek(), 若true,则同时push进min_stk.pop()时若该值等于min_stk.p原创 2015-08-19 05:07:49 · 366 阅读 · 0 评论 -
LeetCode Insertion Sort List
原题链接在这里:https://leetcode.com/problems/insertion-sort-list/思路:设置cur = head,判断cur.val 和 cur.next.val 大小关系,若是后者小, 就从头往后扫值,一直到有一点的val大于cur.next.val, 然后把cur.next这个点加在那里。Note: 1. 有可能会更改head,所以加一个dummy h原创 2015-09-03 01:16:18 · 434 阅读 · 0 评论 -
LeetCode Single Number
原题链接在这里:https://leetcode.com/problems/single-number/首先会想到HashMap,HashSet的想法,但会用到extra O(n) space.所以就要用到bit manipulation,这里和Single Number II非常相似,就不复述了。但这里有个更快的方法,就是用异或 ^ operator. 异或 every nu原创 2015-08-06 08:58:00 · 346 阅读 · 0 评论 -
LeetCode Sort List
原题链接在这里:https://leetcode.com/problems/sort-list/思路: 1. 递归采用merge sort2. 通过midList找到中点,从中点和中点后一点断开3. 前后段分别递归merge sort,一直拆到剩一个点,然后再mergeNote: 1. 必须记得要断开原有list,否则会栈溢出。2. 若有偶数个点,midList找的是中间一对前原创 2015-09-03 02:59:34 · 428 阅读 · 0 评论 -
LeetCode 3Sum Closest
原题链接在这里:https://leetcode.com/problems/3sum-closest/与3Sum类似,但是不能去掉重复的值,因为这里不需要避免存入重复的值。基本思路就是维护一个最小的diff,排序之后夹逼。Time Complexity is O(n*logn+n^2) = O(n^2). Space Complexity is O(1).Note: 1. 注意sum 和原创 2015-08-06 02:44:57 · 315 阅读 · 0 评论 -
LeetCode Implement Stack using Queues
原题链接在这里:https://leetcode.com/problems/implement-stack-using-queues/Method 1: 用两个queue来实现stack,push(x) 时,x先放到queue2,在把queue1逐个接到queue2上,再把queue1和queue2互换即可。pop()时直接poll出来queue1的第一个元素即可。top() 与原创 2015-08-18 10:57:53 · 466 阅读 · 0 评论 -
LeetCode Implement Queue using Stacks
原题链接在这里:https://leetcode.com/problems/implement-queue-using-stacks/本题与Implement Stack using Queues相对应。用Stack implement queue时,可以采用两个stack,add时就是一直像stk1中压栈。poll()时把stk1的所有元素逐个压入另一个stack stk2中,现在s原创 2015-08-18 11:35:01 · 370 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted List
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-list/思路很简单,维护双指针,pre 和 cur的值相等就移动cur,不等就移动pre.AC Java:/** * Definition for singly-linked list. * public class ListNode { *原创 2015-09-02 12:19:11 · 450 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted List II
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/本题与Remove Duplicates from Sorted List相似,不同就是要完全去掉duplicate. 所以要维护一个前指针,当cur.val == cur.next.val 时,就一直移动cur直到不同,然后pre的n原创 2015-09-02 12:25:56 · 690 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted Array
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-array/利用counter来更改新array前面的元素,因为原题中知名length后面的元素没有关系。AC Java:public class Solution { public int removeDuplicates(int[]原创 2015-09-02 12:38:00 · 394 阅读 · 0 评论 -
LeetCode Valid Parentheses
原题链接在这里:https://leetcode.com/problems/valid-parentheses/当遇到'(', '[', '{' 时压栈,当遇到')', ']', '}'时观察stk是否为空,若是空,返回false,若不是,pop()出来的第一个元素是否对应,若不对应, 返回false. 读完整个string若stk不空,返回false。若没问题,返回true。AC原创 2015-08-18 12:02:08 · 302 阅读 · 0 评论 -
Java Binary Tree DFS
DFS Recursion:public void DFS(TreeNode root){ if(root == null){ return; } System.out.println(root.val); DFS(root.left); DFS(root.right);}原创 2015-09-06 12:02:49 · 441 阅读 · 0 评论 -
Java Difference between Private and Protected
Private means this could only be seen within this class.Protected means "package private", this could be seen by subclasses and package members.原创 2015-09-03 03:07:10 · 362 阅读 · 0 评论 -
LeetCode Binary Tree Level Order Traversal II
原题链接在这里:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/与Binary Tree Level Order Traversal相似,只是返过来加链表。自然想到多用一个stack即可。像BFS用queue一层一层扫,出queue时添加到list里,用curCount计数,为0时表示当前level走完,li原创 2015-09-05 01:47:27 · 317 阅读 · 0 评论 -
LeetCode Reverse Linked List II
原题链接在这里:https://leetcode.com/problems/reverse-linked-list-ii/本题是Reverse Linked List的拓展。基本思路和Reverse Linked List的Method 2 相似 扩展的地方在于本题限定了reverse的范围。需先找到reverse范围的前一个点; 翻转时while loop走一次,翻转一个点,所以用一原创 2015-08-22 05:20:23 · 257 阅读 · 0 评论 -
LeetCode Evaluate Reverse Polish Notation
原题链接在这里:https://leetcode.com/problems/evaluate-reverse-polish-notation/思路: 利用栈,遇到数字就压栈,遇到运算符就先pop() op2, 再pop() op1, 按op1 运算符op2 计算,得出结果压回栈,最后站内剩下的就是结果。Note: 1.String str to character, use str.t原创 2015-08-22 01:10:12 · 302 阅读 · 0 评论 -
Java Convert String & Int
To convert a int to string:int num = 123;String str = String.valueOf(num);To convert a string to int:String str = "123";int num = Integer.valueOf(str);原创 2015-09-06 12:41:05 · 390 阅读 · 0 评论 -
LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II对于Majority Element I 来说,有多重解法。Method 1:最容易想到的就是用HashMap 计数,数值大于n/2(注意不是大于等于而是大于),就是返回值。Time O(n), Space O(n).Method 2: 用了sort,返回sort后array的中值即可。Ti原创 2015-08-20 10:32:51 · 363 阅读 · 0 评论 -
LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/与Basic Calculator类似。思路: 扫一遍string,遇见数字时看栈顶是不是 '*' 或者 '/',若不是,就压栈,若是就取出栈顶两个元素算结果,再押回栈。遇见符号就压栈。基本就是先算了 乘除运算。剩下栈里的就是加减运算了。这里注意需用到翻转栈, 使原创 2015-08-21 12:21:54 · 308 阅读 · 0 评论 -
LeetCode Binary Tree Inorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-inorder-traversal/#本题与Binary Tree Preorder Traversal相呼应。可以分别采用Recursion, Iteration 和 Morris Traversal 三种方法。Method 1: RecursionRecursio原创 2015-08-20 02:48:01 · 381 阅读 · 0 评论 -
LeetCode Binary Tree Postorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/与Binary Tree Preorder Traversal 和 Binary Tree Inorder Traversal 类似。Method 1: RecursionTime O(n), Space O(logn)/** * De原创 2015-08-20 08:01:01 · 367 阅读 · 0 评论 -
LeetCode Binary Tree Preorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-preorder-traversal/虽然原题要求不让用recursion,但还是试了一下。一试发现自己的recursion真心无语。Method 1 是Recursion,学到的新方法就是若是要在recursion中维护一个生成的变量,可以再造一个函数,然后把这个要维护的变量当成argum原创 2015-08-19 09:15:12 · 585 阅读 · 0 评论 -
LeetCode Construct Binary Tree from Inorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/这道题与Construct Binary Tree from Preorder and Inorder Traversal 思路相似,不同之处在于这里的root在postorder的最有一位,其他都相同原创 2015-08-21 03:03:10 · 419 阅读 · 0 评论 -
LeetCode Construct Binary Tree from Preorder and Inorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/利用preorder 和 inorder来构造树。e.g. preorder 1, 2, 4, 5, 3, 6, 7 inorder 4, 2, 5, 1, 6, 3, 7先原创 2015-08-21 02:33:55 · 550 阅读 · 0 评论 -
LeetCode Basic Calculator
原题链接在这里:https://leetcode.com/problems/basic-calculator/思路: 1. 遇到数字位,看后一位是否为数字,若是位数字,当前位需要进十.2. 开始设sign = 1,若遇到 ' - ', sign 改为 -1,若遇到 '+',sign改回1.3. 遇到 '(', 压栈,先压之前的res,后压sign,然后初始化res和sign.4.原创 2015-08-21 09:42:54 · 359 阅读 · 0 评论 -
LeetCode Multiply Strings
原题链接在这里:https://leetcode.com/problems/multiply-strings/Method 1: 通过这道题,学到了BigInteger的用法, 他的constructor 可以直接从string建立BigInteger, 但要注意它的乘法API是bi1.multiply(bi2).若是中间需要考虑溢出,还有bi1.intValue(), bi1.lon原创 2015-08-18 03:32:59 · 268 阅读 · 0 评论 -
LeetCode Partition List
原题链接在这里:https://leetcode.com/problems/partition-list/思路: 从头到尾每当发现大于等于x的点,就删掉加在list尾部。Note: 1. 建立一个dummy head放在链表头用来处理原有head.val大于等于x的边界情况。2.用一个mark标注原有的tail,while loop 的条件是iter.next != mark 而不是原创 2015-09-01 04:28:25 · 337 阅读 · 0 评论 -
LeetCode Sudoku Solver
原题链接在此:https://leetcode.com/problems/sudoku-solver/这道题是递归回溯. Note: 1. 设值新的helper recursive function 要带有返回boolean 值,因为要判断此递归方案正确与否。 2. 检查本轮时候正确后还需检查下一轮,这里还是有点不明白。 3. 注意什么时候返回。先要枚举所有本轮的解,对于每原创 2015-07-22 09:12:35 · 392 阅读 · 0 评论 -
Java Final Keyword
Keyword final can be used in three different ways:1. final variable2. final method3. final class1. final variable 基本上就是 constant,一旦被赋值,就不能被变更;如果在class里declare a final variable, 那么在此class生成原创 2015-07-22 03:24:24 · 625 阅读 · 0 评论 -
LeetCode Valid Sudoku
原题链接在这里:https://leetcode.com/problems/valid-sudoku/Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cell原创 2015-07-21 23:45:48 · 282 阅读 · 0 评论 -
LeetCode Add Two Numbers
原题链接:https://leetcode.com/problems/add-two-numbers/思路两个val和进位carry相加,组成新点往后连,注意两个list长度不同和最后是否还有一个进位的情况。AC Java:/** * Definition for singly-linked list. * public class ListNode { * int va原创 2015-08-29 05:39:12 · 305 阅读 · 0 评论 -
LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/思路: 1. 找到距离各自tail 相同距离的起始ListNode,可以通过两个pointer,长的那个先移动Math.abs(lenA-lenB).2. 两个pointer各自移动直到找到相同的ListNode.Node: 1. 若有inter原创 2015-08-29 04:07:21 · 460 阅读 · 0 评论 -
LeetCode Palindrome Linked List
原题链接在这里:https://leetcode.com/problems/palindrome-linked-list/思路: 原题要求time O(n), space O(1). 所以不能用额外空间。先找到中点,reverse中点后面的list部分,再与head开始逐个比较val. 期中reverse部分可以参见Reverse Linked List中的Method 2.AC Ja原创 2015-08-29 03:19:01 · 262 阅读 · 0 评论 -
LeetCode Remove Nth Node From End of List
原题链接在这里:https://leetcode.com/problems/remove-nth-node-from-end-of-list/Method 1: 算出总长度,再减去n,即为要从头多动的点。但要新要求,only one pass。Method 2: 两个指针,一个runner,一个walker,runner先走n步,随后runner和walker一起走,直到runner指为空原创 2015-08-29 01:00:18 · 223 阅读 · 0 评论 -
LeetCode Reverse Nodes in k-Group
原题链接在这里:https://leetcode.com/problems/reverse-nodes-in-k-group/思路: 1. 检测list长度,用此长度除以k,得到的就是需要roate的次数,记为rotNum,如果rotNum等于0,则不需要rotate,直接返回head .2. 这类内部rotate都需要标记前一个点,用以连接rotate后的那段head,这个标记记为m原创 2015-08-27 02:53:15 · 309 阅读 · 0 评论 -
LeetCode Minimum Window Substring
原题链接在这里: https://leetcode.com/problems/minimum-window-substring/又是一道双指针问题,与Longest Substring Without Repeating Characters, Substring with Concatenation of All Words相似。维护一个字典和一个窗口,不同的地方是题目说contai原创 2015-08-13 04:51:50 · 392 阅读 · 0 评论 -
LeetCode Remove Linked List Elements
原题链接:https://leetcode.com/problems/remove-linked-list-elements/思路: 建一个dunmy,dunmy.next = head,为了防止head.val 等于要被踢掉的value,删掉head不方便表示。再建一个helper,这里是temp用来遍历list,每次建材temp.next 是否为null.Note: 这里犯了一个错原创 2015-08-23 00:14:11 · 285 阅读 · 0 评论 -
LeetCode Reverse Linked List
原题链接在这里:https://leetcode.com/problems/reverse-linked-list/Method 1 和 Method 2 都是用的Iteration.Method 1 是建一个dunmy,从前往后扫原链表,遇到一个就用改点的值建一个新的Node加在dunmy和dunmy.next之间。Time O(n), Space O(n), 因为建了一个新的lis原创 2015-08-22 04:15:48 · 756 阅读 · 0 评论 -
LeetCode Add Binary
原题链接在这里: https://leetcode.com/problems/add-binary/这道题与Plus One相似。都是维护一个currentDigit(%) 和一个carryDigit(/). Time O(max(m,n)), Space O(max(m,n)), m = a.length(), n = b.length()Note:1. 注意是从String原创 2015-08-15 09:36:03 · 373 阅读 · 0 评论