
LeetCode
文章平均质量分 70
Dylan_Java_NYC
练很重要,总结更重要,感谢优快云给了我这么好的平台交流。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode Longest Substring Without Repeating Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-without-repeating-characters/String 题目,基本思路就是维护一个窗口[walker,runner]. walker,runner都是向右移,同时维护一个HashSet hs,用来存储这一段中的所有不同character。首先右侧窗口runner在前跑原创 2015-08-08 10:30:16 · 505 阅读 · 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 评论 -
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 Substring with Concatenation of All Words
原题链接在这里: https://leetcode.com/problems/substring-with-concatenation-of-all-words/这道题思路与Longest Substring Without Repeating Characters 相似。 不同的是这道题需要先生成并维护一个字典,检查新词是否是字典里的词。同时维护一个窗口[start,end], 外层循环原创 2015-08-12 10:14:05 · 408 阅读 · 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 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 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 Excel Sheet Column Title
原题链接在这里: https://leetcode.com/problems/excel-sheet-column-title/刚开始以为是简单地26进制,结果有个特殊情况就是"Z", n=26, return "Z"; n = 52, return "AZ". 当current digit 是26时,要把当前位increase 26,并且总体的值要减去26,因为when n = 52, it原创 2015-08-14 00:20:13 · 434 阅读 · 0 评论 -
LeetCode Excel Sheet Column Number
原题链接在这里:https://leetcode.com/problems/excel-sheet-column-number/这道题与Excel Sheet Column Title相呼应。Time O(n), Space O(1).注意cast时后面的数要加个整体的括号。AC Java:public class Solution { public int title原创 2015-08-14 00:37:10 · 404 阅读 · 0 评论 -
LeetCode Roman to Integer
原题链接在这里:https://leetcode.com/problems/roman-to-integer/取当前数值若是比之前的小,就正常加上当前数值,若是比之前位大说明刚才加错了,需要先减掉在加上(cur-pre).e.g. "XIV", at the beginning, res = 10. When i = 1, cur = 1, pre = 10, curpre, res =原创 2015-08-14 04:02:34 · 355 阅读 · 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 Sudoku Solver
原题链接在此:https://leetcode.com/problems/sudoku-solver/这道题是递归回溯. Note: 1. 设值新的helper recursive function 要带有返回boolean 值,因为要判断此递归方案正确与否。 2. 检查本轮时候正确后还需检查下一轮,这里还是有点不明白。 3. 注意什么时候返回。先要枚举所有本轮的解,对于每原创 2015-07-22 09:12:35 · 392 阅读 · 0 评论 -
LeetCode Anagrams
原题链接在这里:https://leetcode.com/problems/anagrams/作者道题让我想起刚才的 Valid Anagram 简直想的太麻烦了。这道题的思路就是sort strs 数组中的每一个string,建立HashMap,key 是 sorted string, value 是 list of all original strings.最后iterate hm原创 2015-08-03 12:09:17 · 373 阅读 · 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 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 Integer to Roman
原题链接在这里:https://leetcode.com/problems/integer-to-roman/与Roman to Integer相呼应。采用贪心算法。之所以需要考虑900,400,90,40这些情况而不考虑800,300,200,80,30,20这些情况是因为:只有左侧加有特罗马字符的才需要添加,右侧加罗马字符可以等下一位。e.g. num = 8, 加"V",然后剩下原创 2015-08-14 11:39:11 · 324 阅读 · 0 评论 -
LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/求factorial后结尾有多少个0,就是求有多少个2和5的配对。但是2比5多了很多,所以就是求5得个数。但是有的5是叠加起来的比如 25,125是5的幂数,所以就要降幂。e.g. n = 100, n/5 =20, n/25= 4, n/125=0,所以加起原创 2015-08-14 12:24:21 · 346 阅读 · 0 评论 -
LeetCode Linked List Cycle
原题链接在这里:https://leetcode.com/problems/linked-list-cycle/两个指针,一快runner,每次走两步;一慢walker,每次走一步,若是它俩相遇就是有cycle。Note: 这里的while loop 终止条件是runner!=null && runner.next!=null, 因为下面会用到 runner.next.next. 这类原创 2015-08-29 22:12:16 · 286 阅读 · 0 评论 -
LeetCode Power of Two
原题链接在这里:https://leetcode.com/problems/power-of-two/思路:n一直除以2,直到1,若中间过程出现非偶数,就返回false。Note: 1 也是power of two, 2的零次方。 AC Java:public class Solution { public boolean isPowerOfTwo(int n) {原创 2015-08-15 08:39:09 · 304 阅读 · 0 评论 -
LeetCode Valid Anagram
原题链接在这里:https://leetcode.com/problems/valid-anagram/这道题可以用HashMap来解决,首先判断 s 和 t 长度是否相等,若不等返回 false。若想等,对于s的每一个character存入hm,value是该character出现的次数。然后遍历t的每一个character,从hm中相应减出去,若在减除过程中碰到hm中没有的char原创 2015-08-03 10:48:01 · 952 阅读 · 0 评论 -
LeetCode Isomorphic Strings
原题链接在此:https://leetcode.com/problems/isomorphic-strings/Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to原创 2015-07-23 04:41:14 · 478 阅读 · 0 评论 -
LeetCode Swap Nodes in Pairs
原题链接在这里:https://leetcode.com/problems/swap-nodes-in-pairs/需要建一个dunmy,最后return dunmy.next. 如此可以避免边界问题的讨论,例如头两个点的转换。AC Java:/** * Definition for singly-linked list. * public class ListNode { *原创 2015-08-29 21:51:05 · 351 阅读 · 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 评论 -
LeetCode Rectangle Area
原题链接在这里:https://leetcode.com/problems/rectangle-area/思路: 方块1面积 + 方块2面积 - 重叠面积Note: 算重叠的面积时会有overflow,并且很难发现. Math.min(C,G) 之前必须加 cast, 原来写(long)(Math.min(C,G) -Math.max(A,E))会报错是因为Math.min(C原创 2015-08-15 07:56:55 · 791 阅读 · 0 评论 -
LeetCode Linked List Cycle II
原题链接在这里:https://leetcode.com/problems/linked-list-cycle-ii/首先找到是否有cycle,若是没有返回null。若是有cycle,可以从head,用two pointers一个一个试直到找到cycle beginner,Time O(n^2).另外一种方法参见了这篇帖子,是数学想法。AC Java:/** * Defini原创 2015-09-01 03:45:18 · 345 阅读 · 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 Add Digits
原题链接在这里:https://leetcode.com/problems/add-digits/最基本的想法就是每一位相加知道成为个位数,但本题有follow-up, require O(1) time.最后的结果只能是0-9,列数看结果,发现num%9对于大多数正数都合适,但corner case 9和9的倍数,本应该返回九,却返回了0,所以单独讨论。Method 3 是返回(num-原创 2015-08-17 01:13:41 · 592 阅读 · 0 评论 -
LeetCode Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/解这道题的思路就是观察remainder是否出现重复,若出现重复,就进入了infinite loop,需要加括号。Note: 1. 注意检查corner case,若是 numerator 或 denominator 出现 0的时候,返回相应的值。原创 2015-08-04 04:51:00 · 303 阅读 · 0 评论 -
LeetCode 3Sum
原题链接在这里:https://leetcode.com/problems/3sum/Two Sum 是这道题的子问题,但是对于3Sum来说又不方便用HashMap,因为不方便存入相同的key,e.g -1,-1,2.因此,这道题需要先进行排序,对于0 到 length-3 的每一个数后面生成两个指针,一个指向当前元素的下一元素,一个指向最后一个元素,判断三个数的和是否是target,若比原创 2015-08-05 04:42:42 · 345 阅读 · 0 评论 -
LeetCode 4Sum
原题链接在这里:https://leetcode.com/problems/4sum/这道题其实是3Sum的扩展,使用的方法基本相同,只是多加了一层loop.但要注意一点:For inner j loop, if statement, 判断overflow 时要写成 j>i+1, 而不是j>0, 与 j 的 初始条件有关。若是写成j>0的话,含有四个相同数的输入就会被跳过。e.g. 0,0原创 2015-08-05 05:41:39 · 261 阅读 · 0 评论 -
LeetCode Happy Number
原题链接在这里:https://leetcode.com/problems/happy-number/思路就是用HashSet 存储过往的和,若是遇见了相同的和,则表示出现了无限循环,没有happy number。Note: 1. Math.pow()的argument 和 return value 都是 double 型,返回时要注意cast。 2. ^ operator stan原创 2015-07-23 06:38:36 · 570 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted Array II
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/本题是Remove Duplicates from Sorted Array的延伸。不同就是允许一次重复,所以可以加一个limit,当limit小于1的时候可以,同时limit++, limit等于1时跳过。注意这里的条件是小于1而不是2原创 2015-09-02 13:01:43 · 504 阅读 · 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 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 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 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 评论 -
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 Single Number II
原题链接在这里:https://leetcode.com/problems/single-number-ii/这道题的关键就是要求no extra memory space. 所以就用到了bit manipulation.思路就是维护一个32位的数组,因为int 都是32位的,外层loop是对应每一位,内层loop是算所有的数在这一位上出现次数的和。Mod 3 后剩下的就是那个只出现了原创 2015-08-06 08:31:14 · 392 阅读 · 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 评论