
LeetCode
Shlyan
这个作者很懒,什么都没留下…
展开
-
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[LeetCode] 680. Valid Palindrome II
Problems: https://leetcode.com/problems/valid-palindrome-ii/Solution:若不回文s [ i : j ],比较s [ i+1 : j ]和s [ i : j+1 ]class Solution { public boolean isPalindrome(String s) { for (int i = 0...原创 2019-10-06 22:26:17 · 79 阅读 · 0 评论 -
[Leetcode] 278. First Bad Version
Problems: https://leetcode.com/problems/first-bad-version/Solution:Binary Searchpublic class Solution extends VersionControl { public int firstBadVersion(int n) { int l = 1; int...原创 2019-10-08 02:27:25 · 86 阅读 · 0 评论 -
[Leetcode] 199. Binary Tree Right Side View
Problems: https://leetcode.com/problems/binary-tree-right-side-view/Solution1:level order,将每层分好后取出最右的值class Solution { List<ArrayList<Integer>> levels = new ArrayList<>(); ...原创 2019-10-07 23:12:20 · 107 阅读 · 0 评论 -
[Leetcode] Binary Tree Traversal
Preorder: https://leetcode.com/problems/binary-tree-preorder-traversal/Inorder: https://leetcode.com/problems/binary-tree-inorder-traversal/Postorder: https://leetcode.com/problems/binary-tree-posto...原创 2019-10-07 12:11:18 · 102 阅读 · 0 评论 -
[Leetcode] 528. Random Pick with Weight
还需要进一步理解Solution1:class Solution { Random rand; int[] a; public Solution(int[] w) { this.rand = new Random(); this.a = new int[w.length]; a[0] = w[0]; for...原创 2019-10-07 08:38:54 · 130 阅读 · 0 评论 -
[Leetcode] 1. Two Sum
Problem: https://leetcode.com/problems/two-sum/solution/Solution1:Brute Forceclass Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; for (int i...原创 2019-10-07 01:13:06 · 107 阅读 · 0 评论 -
[Leetcode] 273. Integer to English Words
Problem: https://leetcode.com/problems/integer-to-english-words/Solution:重点在于如何把长数分割成小的部分考虑(分别为billion,million,thousand,各部分均有三位数字)class Solution { public String single(int num) { switch...原创 2019-10-07 00:24:28 · 74 阅读 · 0 评论 -
[Leetcode] 523. Continuous Subarray Sum
Problems: https://leetcode.com/problems/continuous-subarray-sum/Solution1:利用sum存储到第 i 位的sum注意计算diff时subarray长度至少为2class Solution { public boolean checkSubarraySum(int[] nums, int k) { ...原创 2019-10-06 23:25:03 · 114 阅读 · 0 评论 -
[Leetcode] 896. Monotonic Array
Problems: https://leetcode.com/problems/monotonic-array/Solution1:添加新方法判断class Solution { public boolean isMonotonic(int[] A) { return isIncreasing(A) || isDecreasing(A); } ...原创 2019-10-08 03:26:26 · 84 阅读 · 0 评论 -
[Leetcode] 88. Merge Sorted Array
Problems: https://leetcode.com/problems/merge-sorted-array/Solution1:Merge from startclass Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { //因为要对nums1进行改变,所以先将num...原创 2019-10-08 03:49:51 · 75 阅读 · 0 评论 -
[LeetCode][Array] 217. Contains Duplicate
ProblemL: https://leetcode.com/problems/contains-duplicate/Solution1:class Solution { public boolean containsDuplicate(int[] nums) { // 暴力枚举 for(int i = 0; i < nums.length; i++...原创 2019-08-29 23:50:31 · 163 阅读 · 0 评论 -
[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 · 289 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[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 阅读 · 0 评论 -
[Leetcode] 350. Intersection of Two Arrays II
Problems: https://leetcode.com/problems/intersection-of-two-arrays-ii/Solution1:Using map只需要存储nums1的map对于输出,不需要另外创建数组,而是直接重写nums1class Solution { public int[] intersect(int[] nums1, int[] n...原创 2019-10-09 06:44:15 · 66 阅读 · 0 评论 -
[Leetcode] 301. Remove Invalid Parentheses
Problems: https://leetcode.com/problems/remove-invalid-parentheses/Solution:class Solution { public List<String> removeInvalidParentheses(String s) { char[] arr = s.toCharArray();...原创 2019-10-09 05:47:31 · 81 阅读 · 0 评论 -
[Leetcode] 349. Intersection of Two Arrays
Problems: https://leetcode.com/problems/intersection-of-two-arrays/Solution1:Using two setsclass Solution { public int[] set_intersection(HashSet<Integer> s1, HashSet<Integer> s2) {...原创 2019-10-09 02:34:02 · 74 阅读 · 0 评论 -
[Leetcode] 56. Merge Intervals
Problems: https://leetcode.com/problems/merge-intervals/Solution:class Solution { public int[][] merge(int[][] intervals) { List<int[]> res = new ArrayList<>(); if(in...原创 2019-10-08 09:56:30 · 73 阅读 · 0 评论 -
[Leetcode] 621. Task Scheduler
Problems: https://leetcode.com/problems/task-scheduler/Solution1:k为最高频字母出现的次数,n为interval数,p为最高频率相同的字母个数理想状态下的结果ans = (k-1) * (n+1) + p;class Solution { public int leastInterval(char[] tasks, i...原创 2019-10-06 21:53:20 · 86 阅读 · 0 评论 -
[Leetcode] 560. Subarray Sum Equals K
Problem: https://leetcode.com/problems/subarray-sum-equals-k/Solution1:利用累计和class Solution { public int subarraySum(int[] nums, int k) { // 计算累计和 int[] sum = new int[nums.length...原创 2019-10-06 10:25:55 · 74 阅读 · 0 评论 -
[Leetcode] 125. Valid Palindrome
Problems:https://leetcode.com/problems/valid-palindrome/Solution:class Solution { public boolean isPalindrome(String s) { // 将字符小写化 String ls = s.toLowerCase(); // 删除不是数字/字...原创 2019-10-06 09:09:16 · 79 阅读 · 0 评论 -
[LeetCode][Array] 164. Maximum Gap
Problem: https://leetcode.com/problems/maximum-gap/Solution: still has run time error.利用桶排序定义好bucket的大小和个数存数找到bucket中的max和min比较bucket[i].min 和 bucket[i-1].max 的差,即相邻两数的最大差class Bucket { i...原创 2019-09-16 09:37:14 · 101 阅读 · 0 评论 -
[LeetCode][Array] 128. Longest Consecutive Sequence
Problems:https://leetcode.com/problems/longest-consecutive-sequence/Solution1:动态规划算法首先对数组进行排序,数组dp[]存储到到当前位置的最大连续次数,设定dp的初始值为1若前后差值为1,则dp加一,若前后差值为0,则dp不变最终输出dp的最大值class Solution { public in...原创 2019-09-11 05:01:34 · 102 阅读 · 0 评论 -
[LeetCode][Array] 334. Increasing Triplet Subsequence
Problem:https://leetcode.com/problems/increasing-triplet-subsequence/submissions/Solution:class Solution { public boolean increasingTriplet(int[] nums) { int small = Integer.MAX_VALUE;...原创 2019-09-11 04:03:29 · 74 阅读 · 0 评论