- 博客(98)
- 收藏
- 关注
原创 102. Binary Tree Level Order Traversal
link: https://leetcode.com/problems/binary-tree-level-order-traversal/Solution1: recursiveclass Solution { List<List<Integer>> levels = new ArrayList<List<Integer>>(); public void helper(TreeNode root, int level) {
2020-06-03 10:44:39
161
原创 145. Binary Tree Postorder Traversal
link: https://leetcode.com/problems/binary-tree-postorder-traversal/Solution1: 递归class Solution { public void helper(TreeNode root, ArrayList<Integer> res) { if(root.left != null) helper(root.left, res); if(root.right != null) h
2020-05-28 13:01:52
144
原创 94. Binary Tree Inorder Traversal
link: https://leetcode.com/problems/binary-tree-inorder-traversal/Solution1:recursive递归class Solution { public void helper(TreeNode root, ArrayList<Integer> res) { if(root.left != null) helper(root.left, res); res.add(root.val);
2020-05-27 11:55:15
155
原创 [Leetcode]144. Binary Tree Preorder Traversal
link: https://leetcode.com/problems/binary-tree-preorder-traversal/Solution1: 使用递归class Solution { public void helper(TreeNode root, List<Integer> res) { if(root != null) res.add(root.val); if(root.left != null) helper(root.left
2020-05-26 12:30:41
142
原创 [Leetcode] Binary Search(69, 374, 33, 278, 162, 153, 34, 658, 270, 702, 50, 367, 349, 287, 4)
Binary Searchclass Solution { public int search(int[] nums, int target) { if(nums.length == 0) return -1; int l = 0, r = nums.length -1; while(l <= r) { int...
2020-04-28 11:10:20
323
原创 [Leetcode] 283. Move Zeroes
link: https://leetcode.com/problems/move-zeroes/Solution: two pointersclass Solution { public void moveZeroes(int[] nums) { int len = nums.length; int left = 0; for(int i...
2020-04-27 11:29:33
114
原创 [Leetcode] 26. Remove Duplicates from Sorted Array
link: https://leetcode.com/problems/remove-duplicates-from-sorted-array/Solution: two pointersclass Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; ...
2020-04-26 11:12:24
119
原创 [Leetcode] 557. Reverse Words in a String III
link: https://leetcode.com/problems/reverse-words-in-a-string-iii/Solution: spilt + reverseclass Solution { public String reverseWords(String s) { if(s.length() == 0) return s; S...
2020-04-26 11:02:50
96
原创 [Leetcode] 151. Reverse Words in a String
link: https://leetcode.com/problems/reverse-words-in-a-string/Solution: split + reverseclass Solution { public String reverseWords(String s) { if(s.length() == 0) return s; Strin...
2020-04-26 10:49:05
98
原创 [Leetcode] 119. Pascal's Triangle II
link: https://leetcode.com/problems/pascals-triangle-ii/class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> res = new ArrayList<>(); List&...
2020-04-25 12:00:57
121
原创 [Leetcode] 189. Rotate Array
link: https://leetcode.com/problems/rotate-array/solution/Solution: using reverseclass Solution { public void rotate(int[] nums, int k) { int len = nums.length; k = k % len; ...
2020-04-24 12:24:47
115
原创 [Leetcode] 209. Minimum Size Subarray Sum
link: https://leetcode.com/problems/minimum-size-subarray-sum/Solution: 双指针需要注意左边指针右移次数的判定class Solution { public int minSubArrayLen(int s, int[] nums) { int sum = 0, cnt = Integer.MAX_...
2020-04-23 10:28:44
126
原创 [Leetcode] 485. Max Consecutive Ones
link: https://leetcode.com/problems/max-consecutive-ones/Solutionclass Solution { public int findMaxConsecutiveOnes(int[] nums) { int cnt = 0; int maxCnt = 0; int k = 0;...
2020-04-22 13:19:15
133
原创 [Leetcode] 27. Remove Element
link: https://leetcode.com/problems/remove-element/Solution: two pointersclass Solution { public int removeElement(int[] nums, int val) { int k = 0; for(int i=0; i < nums.leng...
2020-04-22 09:39:32
111
原创 [Leetcode] 167. Two Sum II - Input array is sorted
link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/Solution: two pointersclass Solution { public int[] twoSum(int[] numbers, int target) { int[] res = new int[2]; ...
2020-04-20 01:08:39
125
原创 [Leetcode] 561. Array Partition I
link: https://leetcode.com/problems/array-partition-i/Solution:Sort以后取偶数位数字之和Sort以后取相邻会使得min(a, b)之和最小的原因是:假设a > b,则对于最后的结果,取(a, b)造成的损失是a-b要使得损失最小,则a和b要离得最近,即相邻class Solution { public in...
2020-04-18 23:20:40
113
原创 [Leetcode] 344. Reverse String
link: https://leetcode.com/problems/reverse-string/遇上reverse,考虑双指针是否可用Solution: 双指针class Solution { public void reverseString(char[] s) { int len = s.length; if(len != 0) { ...
2020-04-18 22:41:00
109
原创 [Leetcode] 14. Longest Common Prefix
link: https://leetcode.com/problems/longest-common-prefix/Solution 1: 横向比较追踪最长前缀的长度,并依次进行比较class Solution { public String longestCommonPrefix(String[] strs) { int len = strs.length; ...
2020-04-18 04:57:06
119
原创 [Leetcode] 28. Implement strStr()
link: https://leetcode.com/problems/implement-strstr/Solution 1:寻找substringclass Solution { public int strStr(String haystack, String needle) { // needle is empty if(nee...
2020-04-17 10:11:15
155
原创 [Leetcode] 118. Pascal's Triangle
link: https://leetcode.com/problems/pascals-triangle/Solution:动态规划row[i] = preRow[i-1] + preRow[i]class Solution { public List<List<Integer>> generate(int numRows) { List&l...
2020-04-15 23:09:28
93
原创 [Leetcode] 54. Spiral Matrix
Link: https://leetcode.com/problems/spiral-matrix/Solution 1:记录matrix已经走过的row和col并进行标记class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> res =...
2020-04-15 10:15:34
113
原创 [Leetcode] 498. Diagonal Traverse
Link: https://leetcode.com/problems/diagonal-traverse/Solution 1:矩阵最外围的数字是每一次traverse slices的head偶数位的traverse方向是up right奇数位的方向是down left重点是要找到最外围数字的遍历方法class Solution { public int[] findDiag...
2020-04-14 09:06:19
103
原创 [Leetcode] 66. Plus One
Problem:https://leetcode.com/problems/plus-one/Solution: Copy arrayclass Solution { public int[] plusOne(int[] digits) { int l = digits.length; for(int i = l-1; i >= 0; i--) {...
2020-02-20 13:17:59
119
原创 [Leetcode] 747. Largest Number At Least Twice of Others
Problem: https://leetcode.com/problems/largest-number-at-least-twice-of-others/class Solution { public int dominantIndex(int[] nums) { int max = 0; int max_idx = 0; for(in...
2020-02-19 13:28:40
103
原创 [Leetcode] 724. Find Pivot Index
Problems: https://leetcode.com/problems/find-pivot-index/class Solution { public int pivotIndex(int[] nums) { if(nums.length == 0) { return -1; } else { in...
2020-02-19 13:19:18
149
原创 [Leetcode] 937. Reorder Data in Log Files
Problems: https://leetcode.com/problems/reorder-data-in-log-files/思路:把log分开考虑,从第二个string开始看是否是digit,分为digit和letter两个listletterList重写sort方法,从第二个string开始按照字母顺序进行排序,需要注意的是如果字母顺序完全相同,则考虑index的字母排序Solu...
2019-10-26 03:42:20
280
原创 [Leetcode] 415. Add Strings
Problems: https://leetcode.com/problems/add-strings/Solution:十进制进位计算class Solution { public String addStrings(String num1, String num2) { StringBuilder sb = new StringBuilder(); ...
2019-10-18 13:08:09
78
原创 [Leetcode] 426. Convert Binary Search Tree to Sorted Doubly Linked List
Problem: https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/Soluiton:使用inorder traversal读取值,读出来的node和cur double linked最后再把dummy node删除class Solution { public...
2019-10-18 12:36:06
134
原创 [Leetcode] 138. Copy List with Random Pointer
Problem: https://leetcode.com/problems/copy-list-with-random-pointer/Solution1:Use HashMap to store the copy of original nodeCreate a new list to store the copyRandom pointer points to copyclass ...
2019-10-18 11:21:51
81
原创 [LeetCode] 157&158. Read N Characters Given Read4 + call multiple times
Problem: https://leetcode.com/problems/read-n-characters-given-read4/Solution for call one time:read4读4个 -> 4个一组放入buf中 -> read4继续读 -> 直到读满n个或者字符串已经读完(不满n个)public class Solution extends Rea...
2019-10-17 08:53:31
119
原创 [Leetcode] 211. Add and Search Word - Data structure design
Problems: https://leetcode.com/problems/add-and-search-word-data-structure-design/Solution:本题如果使用hashmap的话,在search时将会很难应付“…ab”的情况class WordDictionary { class TrieNode { boolean isTrie;...
2019-10-17 07:31:48
104
原创 [LeetCode] 208. Implement Trie (Prefix Tree)
Problems: https://leetcode.com/problems/implement-trie-prefix-tree/Solution:class Trie { // TrieNode是Trie的基本元素 // isTrie判断该字符是否是prefix结束 // children记录字符的child class TrieNode { boolean...
2019-10-17 01:16:24
90
原创 [Leetcode] 23. Merge k Sorted Lists
Problems: https://leetcode.com/problems/merge-k-sorted-lists/Solution1:使用priorityQueue,将每个list的第一个值放入queue中,小的作为head,在取出的同时把node的下一个放入queue中需要注意的是,要不断check node.next是否为空class Solution { public...
2019-10-16 23:01:58
86
原创 [Leetcode] 239. Sliding Window Maximum
Problems: https://leetcode.com/problems/sliding-window-maximum/Solution:利用deque,deque peek存储当前window内最大的值,实现的方法为:当添加新unit时检查val,如果大于当前queue内的val,则将原unit pop出来,然后再加入新的unit当添加的unit小于原来的val,则直接加入deq...
2019-10-16 08:45:50
100
原创 [Leetcode] 240. Search a 2D Matrix II
Problems:https://leetcode.com/problems/search-a-2d-matrix-ii/Solution:鉴于matrix的特性,binary search已经不适用,为了迭代方便,从逆对角线的两端下手(任意选一端即可)class Solution { public boolean searchMatrix(int[][] matrix, int ...
2019-10-16 07:04:54
94
原创 [LeetCode] 74. Search a 2D Matrix
Problems:https://leetcode.com/problems/search-a-2d-matrix/Solution1:class Solution { public boolean searchMatrix(int[][] matrix, int target) { // 矩阵为空,需要注意的是depth和width都需要判断 if(mat...
2019-10-16 05:23:34
131
原创 [Leetcode] 146. LRU Cache
Problems: https://leetcode.com/problems/lru-cache/Solution:结合double linkedlist和hashmaphashmap用于存储并且快速查找double linkedlist用于快速删除/增加class LRUCache { // linkedlist中node的设定 class Node { ...
2019-10-12 13:23:48
104
原创 [Leetcode] 269. Alien Dictionary
Problems: https://leetcode.com/problems/alien-dictionary/Solution:Topological Sort建立拓扑图并寻找所有字符的入度(buildGraph),再寻找入度为0的字符取出,邻接字符入度减一(bfs)class Solution { public String alienOrder(String[] words...
2019-10-11 08:41:18
288
原创 [Leetcode] 79. Word Search
Problems: https://leetcode.com/problems/word-search/Solution:DFSclass Solution { public boolean exist(char[][] board, String word) { if(board == null) { return false; ...
2019-10-11 05:50:39
108
原创 [Leetcode] 139. Word Break
Problems: https://leetcode.com/problems/word-break/Solution1Brute force: check all possible substringsclass Solution { public boolean wordBreak(String s, List<String> wordDict) { ...
2019-10-10 11:08:50
90
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人