随笔分类 - Leetcode
Leetcode: Plus One Linked List
摘要:Solution: Use Stack, O(N) space my another solution: don't create new node, modify the existed node Solution 3: reverse the inputs, add one, then reve
阅读全文
Leetcode: Sort Transformed Array
摘要:参考:https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution the problem seems to have many cases a>0, a=0
阅读全文
Leetcode: Rearrange String k Distance Apart
摘要:Analysis: Solution 1: The greedy algorithm is that in each step, select the char with highest remaining count if possible (if it is not in the waiting
阅读全文
Leetcode: Line Reflection
摘要:本题精妙之处在于:1. 如何最快找到possible的line的x axis(我最开始想到要用quickselect find median的方法,结果别人有min max方法) 2. 如何最方便确定一个点关于该line的reflection是否存在,由于既有x又有y,不太好处理,别人有个聪明的办法
阅读全文
Leetcode: Design Snake Game
摘要:HashSet + Queue: 吃东西的时候保留尾巴,不吃的时候删去尾巴 one case to notice蛇转弯的时候,要先删去尾巴,再把新的点加进去。如果这个顺序不对的话,本来不会撞上尾巴的结果会判断会撞上 如果没有39行,程序会说dir没有initialize, 其实更好的写法应该是
阅读全文
Leetcode: Android Unlock Patterns
摘要:我自己的backtracking做法 最开始把cur设置为一个dummy value 0 最好的DFS with Optimization beat 97%: https://discuss.leetcode.com/topic/46260/java-dfs-solution-with-clear-
阅读全文
Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
摘要:我的做法:维护一个window,r移动到超出k distinct character限制是更新max,然后移动l使distinct character <=k; 这种做法更新max只发生在超出限制的时候,有可能永远都没有超出限制,所以while loop完了之后要补上一个max的更新 别人非常棒的做
阅读全文
Design Tic-Tac Toe
摘要:Hint: 看了hint之后想到:既然用数组的话,一行一列都是一个element来代表,估计这个element是要用sum了,那么,能不能用sum来代表一行,使它有且只有一种可能,全部是player1完成的/全部是Player2;所以想到了是Player1就+1,是player2就-1,看最后sum
阅读全文
Leetcode: Nested List Weight Sum II
摘要:Inspired by: https://discuss.leetcode.com/topic/49041/no-depth-variable-no-multiplication Instead of multiplying by depth, add integers multiple times
阅读全文
Leetcode: Largest BST Subtree
摘要:refer to https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments/2 这道题不好从root到leaf一层一层限制subtree取值范围,因为有可能p
阅读全文
Leetcode: Wiggle Sort II
摘要:这道题应该是hard,首先思想上面参考了https://discuss.leetcode.com/topic/32861/3-lines-python-with-explanation-proof I put the smaller half of the numbers on the even i
阅读全文
Leetcode: Delete Node in a BST
摘要:recursively find the node that needs to be deleted Once the node is found, have to handle the below 4 cases node doesn't have left nor right - return
阅读全文
Leetcode: Find All Numbers Disappeared in an Array
摘要:Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Cou...
阅读全文
Leetcode: Find All Duplicates in an Array
摘要:my solution: put integer A[i] at index A[i]-1 unless A[i] already equals i +1 or A[i] == A[A[i]-1] later scan again if at index i, A[i] != i+1, then A
阅读全文
Leetcode: Minimum Genetic Mutation
摘要:the same with word ladder 写的时候语法上出了一些问题 第5行不用给char数组赋大小 第20行用stringbuilder的时候曾经写成:String afterMutation = new StringBuilder(cur).setCharAt(i, c).toStri
阅读全文
浙公网安备 33010602011771号