├── 0002_addTwoNumbers └── Solution.java ├── 0015_threeSum └── Solution.java ├── 0018_fourSum └── Solution.java ├── 001_twoSum └── Solution.java ├── 0024_swapPairs └── Solution.java ├── 0025_reverseKGroup └── Solution.java ├── 003_lengthOfLongestSubstring └── Solution.java ├── 004_findMedianSortedArrays_1 └── Solution.java ├── 004_findMedianSortedArrays_2 └── Solution.java ├── 005_longestPalindrome └── Solution.java ├── 0075_sortColors └── Solution.java ├── 0105_buildTree_1 └── Solution.java ├── 0105_buildTree_2 └── Solution.java ├── 0106_buildTree_1 └── Solution.java ├── 0106_buildTree_2 └── Solution.java ├── 010_isMatch_1 └── Solution.java ├── 010_isMatch_2 └── Solution.java ├── 010_isMatch_3 └── Solution.java ├── 0116_connect └── Solution.java ├── 011_maxArea └── Solution.java ├── 0129_sumNumbers └── Solution.java ├── 0133_cloneGraph_1 └── Solution.java ├── 0133_cloneGraph_2 └── Solution.java ├── 0141_hasCycle └── Solution.java ├── 0142_detectCycle_1 └── Solution.java ├── 0142_detectCycle_2 └── Solution.java ├── 017_letterCombinations └── Solution.java ├── 019_removeNthFromEnd └── Solution.java ├── 020_isValid └── Solution.java ├── 021_mergeTwoLists_1 └── Solution.java ├── 021_mergeTwoLists_2 └── Solution.java ├── 022_generateParenthesis └── Solution.java ├── 023_mergeKLists_1 └── Solution.java ├── 023_mergeKLists_2 └── Solution.java ├── 031_nextPermutation_1 └── Solution.java ├── 031_nextPermutation_2 └── Solution.java ├── 032_longestValidParentheses_1 └── Solution.java ├── 032_longestValidParentheses_2 └── Solution.java ├── 032_longestValidParentheses_3 └── Solution.java ├── 032_longestValidParentheses_4 └── Solution.java ├── 032_longestValidParentheses_5 └── Solution.java ├── 033_search └── Solution.java ├── 0344_reverseString └── Solution.java ├── 0349_intersection_1 └── Solution.java ├── 034_searchRange_1 └── Solution.java ├── 034_searchRange_2 └── Solution.java ├── 036_isValidSudoku_1 └── Solution.java ├── 036_isValidSudoku_2 └── Solution.java ├── 0380_RandomizedSet └── RandomizedSet.java ├── 0381_RandomizedCollection_1 └── RandomizedCollection.java ├── 0381_RandomizedCollection_2 └── RandomizedCollection.java ├── 0381_RandomizedCollection_3 └── RandomizedCollection.java ├── 039_combinationSum_1 └── Solution.java ├── 039_combinationSum_2 └── Solution.java ├── 039_combinationSum_3 └── Solution.java ├── 042_trap_1 └── Solution.java ├── 042_trap_2 └── Solution.java ├── 0445_addTwoNumbers_1 └── Solution.java ├── 0445_addTwoNumbers_2 └── Solution.java ├── 0459_repeatedSubstringPattern_1 └── Solution.java ├── 0459_repeatedSubstringPattern_2 └── Solution.java ├── 0459_repeatedSubstringPattern_3 └── Solution.java ├── 045_jump_1 └── Solution.java ├── 045_jump_2 └── Solution.java ├── 045_jump_3 └── Solution.java ├── 0463_islandPerimeter_1 └── Solution.java ├── 0463_islandPerimeter_2 └── Solution.java ├── 0463_islandPerimeter_3 └── Solution.java ├── 0463_islandPerimeter_4 └── Solution.java ├── 046_permute_1 └── Solution.java ├── 046_permute_2 └── Solution.java ├── 048_rotate └── Solution.java ├── 049_groupAnagrams_1 └── Solution.java ├── 049_groupAnagrams_2 └── Solution.java ├── 050_myPow_1 └── Solution.java ├── 050_myPow_2 └── Solution.java ├── 0518_change └── Solution.java ├── 053_maxSubArray └── Solution.java ├── 054_spiralOrder └── Solution.java ├── 055_canJump_1 └── Solution.java ├── 055_canJump_2 └── Solution.java ├── 056_merge └── Solution.java ├── 059_generateMatrix └── Solution.java ├── 061_rotateRight └── Solution.java ├── 062_uniquePaths_1 └── Solution.java ├── 062_uniquePaths_2 └── Solution.java ├── 064_minPathSum_1 └── Solution.java ├── 064_minPathSum_2 └── Solution.java ├── 064_minPathSum_3 └── Solution.java ├── 064_minPathSum_4 └── Solution.java ├── 0684_findRedundantConnection └── Solution.java ├── 0685_findRedundantDirectedConnection └── Solution.java ├── 069_mySqrt_1 └── Solution.java ├── 069_mySqrt_2 └── Solution.java ├── 070_climbStairs_1 └── Solution.java ├── 070_climbStairs_2 └── Solution.java ├── 072_minDistance └── Solution.java ├── 073_setZeroes_1 └── Solution.java ├── 073_setZeroes_2 └── Solution.java ├── 075_sortColors └── Solution.java ├── 0763_partitionLabels_1 └── Solution.java ├── 0763_partitionLabels_2 └── Solution.java ├── 0763_partitionLabels_3 └── Solution.java ├── 0763_partitionLabels_4 └── Solution.java ├── 076_minWindow └── Solution.java ├── 078_subsets_1 └── Solution.java ├── 078_subsets_2 └── Solution.java ├── 078_subsets_3 └── Solution.java ├── 078_subsets_4 └── Solution.java ├── 079_exist └── Solution.java ├── 0834_sumOfDistancesInTree └── Solution.java ├── 0844_backspaceCompare └── Solution.java ├── 0845_longestMountain_1 └── Solution.java ├── 0845_longestMountain_2 └── Solution.java ├── 0845_longestMountain_3 └── Solution.java ├── 084_largestRectangleArea_1 └── Solution.java ├── 084_largestRectangleArea_2 └── Solution.java ├── 084_largestRectangleArea_3 └── Solution.java ├── 0941_validMountainArray └── Solution.java ├── 094_inorderTraversal_1 └── Solution.java ├── 094_inorderTraversal_2 └── Solution.java ├── 094_inorderTraversal_3 └── Solution.java ├── 096_numTrees_1 └── Solution.java ├── 096_numTrees_2 └── Solution.java ├── 098_isValidBST_1 └── Solution.java ├── 098_isValidBST_2 └── Solution.java ├── 1002_commonChars_1 └── Solution.java ├── 1002_commonChars_2 └── Solution.java ├── 1019_nextLargerNodes_1 └── Solution.java ├── 1019_nextLargerNodes_2 └── Solution.java ├── 101_isSymmetric └── Solution.java ├── 1024_videoStitching_1 └── Solution.java ├── 1024_videoStitching_2 └── Solution.java ├── 1024_videoStitching_3 └── Solution.java ├── 102_levelOrder └── Solution.java ├── 104_maxDepth └── Solution.java ├── 113_pathSum_1 └── Solution.java ├── 113_pathSum_2 └── Solution.java ├── 114_flatten_1 └── Solution.java ├── 114_flatten_2 └── Solution.java ├── 116_connect └── Solution.java ├── 117_connect_1 └── Solution.java ├── 117_connect_2 └── Solution.java ├── 1207_uniqueOccurrences └── Solution.java ├── 121_maxProfit └── Solution.java ├── 1248_numberOfSubarrays_1 └── Solution.java ├── 131_partition_1 └── Solution.java ├── 131_partition_2 └── Solution.java ├── 1365_smallerNumbersThanCurrent_1 └── Solution.java ├── 1365_smallerNumbersThanCurrent_2 └── Solution.java ├── 1365_smallerNumbersThanCurrent_3 └── Solution.java ├── 136_singleNumber └── Solution.java ├── 139_wordBreak_1 └── Solution.java ├── 139_wordBreak_2 └── Solution.java ├── 139_wordBreak_3 └── Solution.java ├── 139_wordBreak_4 └── Solution.java ├── 1434_numberWays_1 └── Solution.java ├── 1434_numberWays_2 └── Solution.java ├── 1438_longestSubarray └── Solution.java ├── 1439_kthSmallest └── Solution.java ├── 1444_ways └── Solution.java ├── 144_preorderTraversal_1 └── Solution.java ├── 144_preorderTraversal_2 └── Solution.java ├── 145_postorderTraversal_1 └── Solution.java ├── 145_postorderTraversal_2 └── Solution.java ├── 146_LRUCache_1 └── Solution.java ├── 146_LRUCache_2 └── Solution.java ├── 148_sortList_1 └── Solution.java ├── 148_sortList_2 └── Solution.java ├── 151_reverseWords └── Solution.java ├── 152_maxProduct └── Solution.java ├── 155_MinStack_1 └── Solution.java ├── 155_MinStack_2 └── Solution.java ├── 156_canMeasureWater └── Solution.java ├── 160_getIntersectionNode └── Solution.java ├── 1610_visiblePoints └── Solution.java ├── 1611_minimumOneBitOperations_1 └── Solution.java ├── 1611_minimumOneBitOperations_2 └── Solution.java ├── 1616_checkPalindromeFormation_1 └── Solution.java ├── 169_majorityElement └── Solution.java ├── 198_rob_1 └── Solution.java ├── 198_rob_2 └── Solution.java ├── 200_numIslands_1 └── Solution.java ├── 200_numIslands_2 └── Solution.java ├── 204_countPrimes_1 └── Solution.java ├── 204_countPrimes_2 └── Solution.java ├── 204_countPrimes_3 └── Solution.java ├── 206_reverseList └── Solution.java ├── 207_canFinish_1 └── Solution.java ├── 207_canFinish_2 └── Solution.java ├── 207_canFinish_3 └── Solution.java ├── 208_Trie_1 └── Solution.java ├── 208_Trie_2 └── Solution.java ├── 215_findKthLargest_1 └── Solution.java ├── 215_findKthLargest_2 └── Solution.java ├── 215_findKthLargest_3 └── Solution.java ├── 221_maximalSquare_1 └── Solution.java ├── 221_maximalSquare_2 └── Solution.java ├── 221_maximalSquare_3 └── Solution.java ├── 221_maximalSquare_4 └── Solution.java ├── 226_invertTree └── Solution.java ├── 230_kthSmallest_1 └── Solution.java ├── 230_kthSmallest_2 └── Solution.java ├── 234_isPalindrome_1 └── Solution.java ├── 234_isPalindrome_2 └── Solution.java ├── 235_lowestCommonAncestor └── Solution.java ├── 236_lowestCommonAncestor_1 └── Solution.java ├── 236_lowestCommonAncestor_2 └── Solution.java ├── 238_productExceptSelf └── Solution.java ├── 240_searchMatrix_1 └── Solution.java ├── 240_searchMatrix_2 └── Solution.java ├── 240_searchMatrix_3 └── Solution.java ├── 279_numSquares_1 └── Solution.java ├── 279_numSquares_2 └── Solution.java ├── 279_numSquares_3 └── Solution.java ├── 283_moveZeroes └── Solution.java ├── 287_findDuplicate_1 └── Solution.java ├── 287_findDuplicate_2 └── Solution.java ├── 289_gameOfLife └── Solution.java ├── 300_lengthOfLIS_1 └── Solution.java ├── 300_lengthOfLIS_2 └── Solution.java ├── 309_maxProfit_1 └── Solution.java ├── 309_maxProfit_2 └── Solution.java ├── 322_coinChange └── Solution.java ├── 328_oddEvenList └── Solution.java ├── 337_rob_1 └── Solution.java ├── 337_rob_2 └── Solution.java ├── 337_rob_3 └── Solution.java ├── 337_rob_4 └── Solution.java ├── 337_rob_5 └── Solution.java ├── 337_rob_6 └── Solution.java ├── 338_countBits_1 └── Solution.java ├── 338_countBits_2 └── Solution.java ├── 338_countBits_3 └── Solution.java ├── 338_countBits_4 └── Solution.java ├── 341_NestedIterator_1 └── Solution.java ├── 347_topKFrequent_1 └── Solution.java ├── 347_topKFrequent_2 └── Solution.java ├── 378_kthSmallest_1 └── Solution.java ├── 378_kthSmallest_2 └── Solution.java ├── 394_decodeString_1 └── Solution.java ├── 394_decodeString_2 └── Solution.java ├── 399_calcEquation_1 └── Solution.java ├── 399_calcEquation_2 └── Solution.java ├── 404_sumOfLeftLeaves_1 └── Solution.java ├── 404_sumOfLeftLeaves_2 └── Solution.java ├── 404_sumOfLeftLeaves_3 └── Solution.java ├── 406_reconstructQueue └── Solution.java ├── 416_canPartition └── Solution.java ├── 437_pathSum └── Solution.java ├── 438_findAnagrams_1 └── Solution.java ├── 438_findAnagrams_2 └── Solution.java ├── 448_findDisappearedNumbers └── Solution.java ├── 454_fourSumCount └── Solution.java ├── 461_hammingDistance └── Solution.java ├── 494_findTargetSumWays_1 └── Solution.java ├── 494_findTargetSumWays_2 └── Solution.java ├── 494_findTargetSumWays_3 └── Solution.java ├── 501_findMode_1 └── Solution.java ├── 501_findMode_2 └── Solution.java ├── 501_findMode_3 └── Solution.java ├── 538_convertBST └── Solution.java ├── 5411_cherryPickup └── Solution.java ├── 543_diameterOfBinaryTree └── Solution.java ├── 5486_minTime └── Solution.java ├── 5515_ParkingSystem └── ParkingSystem.java ├── 5516_alertNames └── Solution.java ├── 5517_busiestServers └── Solution.java ├── 5518_restoreMatrix └── Solution.java ├── 5531_specialArray └── Solution.java ├── 5532_isEvenOddTree └── Solution.java ├── 5535_maxDepth └── Solution.java ├── 5536_maximalNetworkRank └── Solution.java ├── 5538_countSubgraphsForEachDiameter_1 └── Solution.java ├── 560_subarraySum_1 └── Solution.java ├── 560_subarraySum_2 └── Solution.java ├── 572_isSubtree └── Solution.java ├── 581_findUnsortedSubarray └── Solution.java ├── 617_mergeTrees └── Solution.java ├── 621_leastInterval └── Solution.java ├── 647_countSubstrings_1 └── Solution.java ├── 647_countSubstrings_2 └── Solution.java ├── 647_countSubstrings_3 └── Solution.java ├── 695_maxAreaOfIsland_1 └── Solution.java ├── 695_maxAreaOfIsland_2 └── Solution.java ├── 701_insertIntoBST_1 └── Solution.java ├── 701_insertIntoBST_2 └── Solution.java ├── 739_dailyTemperatures_1 └── Solution.java ├── 739_dailyTemperatures_2 └── Solution.java ├── 739_dailyTemperatures_3 └── Solution.java ├── 739_dailyTemperatures_4 └── Solution.java ├── 771_numJewelsInStones └── Solution.java ├── 898_subarrayBitwiseORs └── Solution.java ├── 945_minIncrementForUnique └── Solution.java ├── 983_mincostTickets_1 └── Solution.java ├── 983_mincostTickets_2 └── Solution.java ├── Ali040 └── Solution.java ├── Ali057 └── Solution.java ├── JZOffer_13_movingCount └── Solution.java ├── LCP_17_minimumOperations_1 └── Solution.java ├── LCP_17_minimumOperations_2 └── Solution.java ├── README.md ├── itv13_movingCount └── Solution.java ├── itv46_translateNum_1 └── Solution.java ├── itv46_translateNum_2 └── Solution.java ├── itv59-II_MaxQueue_1 └── Solution.java ├── itv64_sumNums_1 └── Solution.java └── itv64_sumNums_2 └── Solution.java /001_twoSum/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1: https://leetcode.com/problems/two-sum/ 3 | Author: zhangyixing1007 4 | Idea: save previous value in HashMap 5 | Time: 2 ms, beat 99.58% 6 | Space: 39.1MB, beat 42.80% 7 | */ 8 | 9 | class Solution { 10 | public int[] twoSum(int[] nums, int target) { 11 | Map map=new HashMap<>(); 12 | for(int i=0; i=maxLen){ 18 | maxLen=len;id=i; 19 | } 20 | } 21 | return s.substring(id-(maxLen-1)/2+1, id+maxLen/2); 22 | } 23 | 24 | int expand(String s, int l, int r){ 25 | while(l>=0&&r=2 && p.charAt(1) == '*'){ 15 | return (isMatch(s,p.substring(2))|| 16 | firstMatch&&isMatch(s.substring(1),p)); 17 | } else { 18 | return firstMatch&&isMatch(s.substring(1),p.substring(1)); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /010_isMatch_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.010: https://leetcode.com/problems/regular-expression-matching/ 3 | Author: zhangyixing1007 4 | Idea: recursion + save intermediate result each time 5 | Time: 2 ms, beat 99.94% 6 | Space: 36.4 MB, beat 87.64% 7 | */ 8 | 9 | class Solution { 10 | enum Result{ 11 | TRUE, FALSE 12 | } 13 | Result[][] memo; 14 | 15 | public boolean isMatch(String s, String p) { 16 | memo = new Result[s.length()+1][p.length()+1]; 17 | 18 | return dp(0,0,s,p); 19 | } 20 | 21 | private boolean dp (int i, int j, String s, String p){ 22 | if (memo[i][j]!=null) 23 | return memo[i][j] == Result.TRUE; 24 | if (j == p.length()) 25 | return i == s.length(); 26 | boolean firstMatch = (i!=s.length()) && 27 | (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'); 28 | boolean ans; 29 | if (p.length() - j >= 2 && p.charAt(j+1) == '*'){ 30 | ans = dp(i,j+2,s,p) || (firstMatch&&dp(i+1,j,s,p)); 31 | 32 | } else { 33 | ans = firstMatch&&dp(i+1,j+1,s,p); 34 | } 35 | memo[i][j] = ans == true? Result.TRUE:Result.FALSE; 36 | return ans; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /010_isMatch_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.010: https://leetcode.com/problems/regular-expression-matching/ 3 | Author: zhangyixing1007 4 | Idea: recursion + save intermediate result each time 5 | Time: 2 ms, beat 99.94% 6 | Space: 36.6 MB, beat 86.35% 7 | */ 8 | 9 | class Solution { 10 | Boolean[][] memo; 11 | 12 | public boolean isMatch(String s, String p) { 13 | memo = new Boolean[s.length()+1][p.length()+1]; 14 | return dp(0,0,s,p); 15 | } 16 | 17 | 18 | private Boolean dp (int i, int j, String s, String p){ 19 | if (memo[i][j]!=null) 20 | return memo[i][j] == true; 21 | if (j == p.length()) 22 | return i == s.length(); 23 | boolean firstMatch = (i!=s.length()) && 24 | (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'); 25 | if (p.length() - j >= 2 && p.charAt(j+1) == '*'){ 26 | memo[i][j] = dp(i,j+2,s,p) || (firstMatch&&dp(i+1,j,s,p)); 27 | 28 | 29 | } else { 30 | memo[i][j] = firstMatch&&dp(i+1,j+1,s,p); 31 | } 32 | return memo[i][j]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /011_maxArea/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.011: https://leetcode.com/problems/container-with-most-water/ 3 | Author: zhangyixing1007 4 | Idea: two pointers, move the short one 5 | Time: 4 ms, beat 75.18% 6 | Space: 39.7 MB, beat 46.26% 7 | */ 8 | 9 | class Solution { 10 | public int maxArea(int[] height) { 11 | int l=0, r=height.length-1; 12 | int max=0; 13 | while (l neighbors; 14 | 15 | public Node() { 16 | val = 0; 17 | neighbors = new ArrayList(); 18 | } 19 | 20 | public Node(int _val) { 21 | val = _val; 22 | neighbors = new ArrayList(); 23 | } 24 | 25 | public Node(int _val, ArrayList _neighbors) { 26 | val = _val; 27 | neighbors = _neighbors; 28 | } 29 | } 30 | */ 31 | class Solution { 32 | public Node cloneGraph(Node node) { 33 | if(node==null || visited.containsKey(node)) return visited.get(node); 34 | Node curr=new Node(node.val); 35 | visited.put(node, curr); 36 | for(Node neighbor:node.neighbors){ 37 | curr.neighbors.add(cloneGraph(neighbor)); 38 | } 39 | return curr; 40 | } 41 | Map visited=new HashMap<>(); 42 | } 43 | -------------------------------------------------------------------------------- /0141_hasCycle/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0141: https://leetcode.com/problems/linked-list-cycle/ 3 | Author: zhangyixing1007 4 | Idea: two pointers, fast and slow 5 | Time: 0 ms, beat 100% 6 | Space: 34.8MB, beat 34.47% 7 | */ 8 | 9 | /** 10 | * Definition for singly-linked list. 11 | * class ListNode { 12 | * int val; 13 | * ListNode next; 14 | * ListNode(int x) { 15 | * val = x; 16 | * next = null; 17 | * } 18 | * } 19 | */ 20 | 21 | public class Solution { 22 | public boolean hasCycle(ListNode head) { 23 | if(head==null||head.next==null) return false; 24 | ListNode fast=head; 25 | while(fast!=null&&fast.next!=null){ 26 | head=head.next; 27 | fast=fast.next.next; 28 | if(head==fast) return true; 29 | } 30 | return false; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0142_detectCycle_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0142: https://leetcode.com/problems/linked-list-cycle-ii/ 3 | Author: zhangyixing1007 4 | Idea: HashSet, brute force 5 | Time: 8 ms, beat 31.11% 6 | Space: 36.2 MB, beat 5.03% 7 | */ 8 | 9 | /** 10 | * Definition for singly-linked list. 11 | * class ListNode { 12 | * int val; 13 | * ListNode next; 14 | * ListNode(int x) { 15 | * val = x; 16 | * next = null; 17 | * } 18 | * } 19 | */ 20 | 21 | public class Solution{ 22 | public ListNode detectCycle(ListNode head){ 23 | Set set = new HashSet<>(); 24 | while(head!=null&&(set.isEmpty()||!set.contains(head))){ 25 | set.add(head); 26 | head = head.next; 27 | } 28 | return head; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0142_detectCycle_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0142: https://leetcode.com/problems/linked-list-cycle-ii/ 3 | Author: zhangyixing1007 4 | Idea: two pointers to check loop, and then Floyd Algorithm 5 | Time: 0 ms, beat 100% 6 | Space: 36.1 MB, beat 5.03% 7 | */ 8 | 9 | /** 10 | * Definition for singly-linked list. 11 | * class ListNode { 12 | * int val; 13 | * ListNode next; 14 | * ListNode(int x) { 15 | * val = x; 16 | * next = null; 17 | * } 18 | * } 19 | */ 20 | 21 | public class Solution{ 22 | public ListNode detectCycle(ListNode head){ 23 | ListNode fast = head; 24 | ListNode slow = head; 25 | while(fast!=null&&fast.next!=null){ 26 | fast = fast.next.next; 27 | slow = slow.next; 28 | if (fast == slow) break; 29 | } 30 | if (fast==null||fast.next==null) return null; 31 | slow = head; 32 | while (fast!=slow){ 33 | fast = fast.next; 34 | slow = slow.next; 35 | } 36 | return slow; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /019_removeNthFromEnd/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.019: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3 | Author: zhangyixing1007 4 | Idea: fast/slow pointers 5 | Time: 1 ms, beat 56.92% 6 | Space: 35.1 MB, beat 11.27% 7 | */ 8 | 9 | /** 10 | * Definition for singly-linked list. 11 | * public class ListNode { 12 | * int val; 13 | * ListNode next; 14 | * ListNode(int x) { val = x; } 15 | * } 16 | */ 17 | 18 | class Solution { 19 | public ListNode removeNthFromEnd(ListNode head, int n) { 20 | ListNode prev=new ListNode(0); 21 | prev.next=head; 22 | ListNode slow=prev, fast=prev; 23 | while (n>-1){ 24 | fast=fast.next;n--; 25 | } 26 | while (fast!=null){ 27 | fast=fast.next; slow=slow.next; 28 | } 29 | slow.next=slow.next.next; 30 | return prev.next; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /020_isValid/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.20: https://leetcode.com/problems/valid-parentheses/ 3 | Author: zhangyixing1007 4 | Idea: push "(" "{" "[" into stack , and while ")" "}" "]", check if top is "(" "{" "[" correspondingly 5 | Time: 2 ms, beat 97.54% 6 | Space: 34.1MB, beat 87.48% 7 | */ 8 | 9 | class Solution { 10 | public boolean isValid(String s) { 11 | Stack stack=new Stack<>(); 12 | for(int i=0; i generateParenthesis(int n) { 11 | add("", 0, 0, n); 12 | return list; 13 | } 14 | List list=new ArrayList<>(); 15 | void add(String s, int count1, int count2, int n){ 16 | if(count1==count2&&count1=0 && nums[i]>=nums[i+1]) i--; 14 | this.nums = nums; 15 | if (i!=-1) { 16 | int j = nums.length-1; 17 | while (j-1>=0 && nums[j]<=nums[i]) j--; 18 | swap(i,j); 19 | } 20 | reverse(i+1); 21 | } 22 | int[] nums; 23 | void reverse (int start){ 24 | int end = nums.length-1; 25 | while (start -1; i--){ 20 | if (s.charAt(i)=='(') left++; 21 | else right++; 22 | if (left == right) ans = Math.max(ans, 2*right); 23 | else if (right < left) {left = 0; right = 0;} 24 | } 25 | return ans; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /032_longestValidParentheses_4/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | LeetCode Problem No.032: https://leetcode.com/problems/longest-valid-parentheses/ 4 | Author: zhangyixing1007 5 | Idea: stack 6 | Time: 9 ms, beat 39.29% 7 | Space: 37.9 MB, beat 79.91% 8 | */ 9 | 10 | class Solution { 11 | public int longestValidParentheses(String s) { 12 | int ans = 0; 13 | Stack stack = new Stack(); 14 | stack.push(-1); 15 | for (int i = 0; i < s.length(); i++){ 16 | char curr = s.charAt(i); 17 | if (curr == '('){ 18 | stack.push(i); 19 | } else { 20 | int start = stack.pop(); 21 | if (stack.isEmpty()) 22 | stack.push(i); 23 | else 24 | ans = Math.max(ans, i-stack.peek()); 25 | } 26 | } 27 | return ans; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /032_longestValidParentheses_5/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.032: https://leetcode.com/problems/longest-valid-parentheses/ 3 | Author: zhangyixing1007 4 | Idea: scan from left and right respectivly 5 | Time: 2 ms, beat 99.83% 6 | Space: 37.6 MB, beat 81.71% 7 | */ 8 | 9 | class Solution { 10 | public int longestValidParentheses(String s) { 11 | if (s.length() < 2) 12 | return 0; 13 | int ans = 0; 14 | int[] dp = new int[s.length()+1]; 15 | for (int i = 2; i < dp.length; i++){ 16 | if (s.charAt(i-1) == ')'){ 17 | if (s.charAt(i-2) == '('){ 18 | dp[i] = dp[i-2] + 2; 19 | } else if (i-1-dp[i-1] > 0 && s.charAt(i-2-dp[i-1]) == '('){ 20 | dp[i] = dp[i-1] + dp[i-2-dp[i-1]]+2; 21 | } 22 | ans = Math.max(ans, dp[i]); 23 | } 24 | } 25 | return ans; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0344_reverseString/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0344: https://leetcode-cn.com/problems/reverse-string/ 3 | Author: zhangyixing1007 4 | Idea: one loop 5 | Time: 1 ms, beat 99.98% 6 | Space: 45.2 MB, beat 90.35% 7 | */ 8 | 9 | class Solution { 10 | public void reverseString(char[] s) { 11 | for(int i=0; i 12 | List l1 = Arrays.stream(nums1).boxed().collect(Collectors.toList()); 13 | // 过滤,只保留交集元素,去重,自动装箱,转化为List 14 | List l2 = Arrays.stream(nums2).filter(n -> l1.contains(n)).distinct() 15 | .boxed().collect(Collectors.toList()); 16 | // List 转为 int[] 17 | return l2.stream().mapToInt(Integer::valueOf).toArray(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /042_trap_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.042: https://leetcode.com/problems/trapping-rain-water/ 3 | Author: zhangyixing1007 4 | Idea: sum all the min(left_max,right_max) - height 5 | Time: 1 ms, beat 100% 6 | Space: 37.6 MB, beat 82.74% 7 | */ 8 | 9 | class Solution { 10 | public int trap(int[] height){ 11 | int len = height.length; 12 | if (len < 2) return 0; 13 | int[] lmax = new int[len]; lmax[0] = height[0]; 14 | int[] rmax = new int[len]; rmax[len-1] =height[len-1]; 15 | 16 | for (int i = 1; i < len; i++){ 17 | lmax[i] = Math.max(lmax[i-1],height[i]); 18 | rmax[len-i-1] = Math.max(rmax[len-i],height[len-i-1]); 19 | } 20 | 21 | int ans = 0; 22 | for (int i = 0; i < len; i++){ 23 | ans = ans + Math.min(lmax[i], rmax[i]) - height[i]; 24 | } 25 | return ans; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /042_trap_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.042: https://leetcode.com/problems/trapping-rain-water/ 3 | Author: zhangyixing1007 4 | Idea: scan only once, start from left and right together 5 | Time: 1 ms, beat 100% 6 | Space: 37.6 MB, beat 82.74% 7 | */ 8 | 9 | class Solution { 10 | public int trap(int[] height) { 11 | int left = 0; 12 | int right = height.length-1; 13 | int leftmax = 0; 14 | int rightmax = 0; 15 | int ans = 0; 16 | while (left < right){ 17 | if (height[left] < height[right]){ 18 | if (height[left]>leftmax) 19 | leftmax = height[left++]; 20 | else 21 | ans += leftmax - height[left++]; 22 | } else { 23 | if (height[right]>rightmax) 24 | rightmax = height[right--]; 25 | else 26 | ans += rightmax - height[right--]; 27 | } 28 | } 29 | return ans; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0459_repeatedSubstringPattern_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0459: https://leetcode-cn.com/problems/repeated-substring-pattern/ 3 | Author: zhangyixing1007 4 | Idea: indexOf 5 | Time: 95 ms, beat 49.16% 6 | Space: 39.2 MB, beat 66.47% 7 | */ 8 | 9 | class Solution { 10 | public boolean repeatedSubstringPattern(String s) { 11 | String t=s+s; 12 | return t.indexOf(s, 1)0){ 16 | for(int i=0; i=lastPos){ 18 | lastPos=i; steps++; 19 | } 20 | } 21 | } 22 | return steps; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /045_jump_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.045: https://leetcode.com/problems/jump-game-ii/ 3 | Author: zhangyixing1007 4 | Idea: greedy algorithm, from right to left 5 | Time: 2 ms, beat 94.93% 6 | Space: 41.7 MB, beat 5.00% 7 | */ 8 | 9 | class Solution { 10 | public int jump(int[] nums) { 11 | if(nums==null||nums.length<2) return 0; 12 | int n=nums.length; 13 | int steps=0; 14 | int maxPos=0; 15 | int end=0; 16 | for(int i=0; i= n || ty < 0 || ty >= m || grid[tx][ty] == 0) { 25 | cnt += 1; 26 | } 27 | } 28 | ans += cnt; 29 | } 30 | } 31 | } 32 | return ans; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /046_permute_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.46: https://leetcode.com/problems/permutations/ 3 | Author: zhangyixing1007 4 | Idea: enumerate all the permutation, apply +1-1 = 0 to save space 5 | Time: 1 ms, beat 100% 6 | Space: 38.3MB, beat 73.77% 7 | */ 8 | 9 | 10 | class Solution { 11 | 12 | public List> permute(int[] nums) { 13 | List> output = new ArrayList<>(); 14 | List numsList = new ArrayList(); 15 | for (Integer num : nums) 16 | numsList.add(num); 17 | backtrack(numsList,output,0); 18 | return output; 19 | } 20 | 21 | private void backtrack(List nums, List> output, int first) 22 | { 23 | if (first == nums.size()) output.add(new ArrayList(nums)); 24 | 25 | for (int i = first; i < nums.size(); i++) 26 | { 27 | Collections.swap(nums,first,i); 28 | backtrack(nums, output, first+1); 29 | Collections.swap(nums,first,i); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /048_rotate/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.48: https://leetcode.com/problems/rotate-image/ 3 | Author: zhangyixing1007 4 | Idea: cut the matrix into 4 parts, use two layers of loops 5 | Time: 0 ms, beat 100% 6 | Space: 36.2MB, beat 58.92% 7 | */ 8 | 9 | 10 | class Solution { 11 | public void rotate(int[][] matrix) { 12 | int n = matrix.length; 13 | for (int i = 0; i < (n+1)/2; i++){ 14 | for (int j = 0; j < n/2; j++){ 15 | int tmp = matrix[i][j]; 16 | matrix[i][j] = matrix[n-j-1][i]; 17 | matrix[n-j-1][i] = matrix[n-i-1][n-j-1]; 18 | matrix[n-i-1][n-j-1] = matrix[j][n-i-1]; 19 | matrix[j][n-i-1] = tmp; 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /049_groupAnagrams_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.49: https://leetcode.com/problems/group-anagrams/ 3 | Author: zhangyixing1007 4 | Idea: make good use of HashMap, sort, conversion between char[] and String 5 | Time: 16 ms, beat 88.71% 6 | Space: 40.4 MB, beat 99.55% 7 | */ 8 | 9 | class Solution { 10 | public List> groupAnagrams(String[] strs) { 11 | HashMap> map=new HashMap<>(); 12 | for (String str:strs){ 13 | char[] c=str.toCharArray(); 14 | Arrays.sort(c); 15 | String s=String.valueOf(c); 16 | if(map.containsKey(s)) 17 | map.get(s).add(str); 18 | else 19 | map.put(s,new ArrayList(Arrays.asList(str))); 20 | } 21 | return new ArrayList(map.values()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /050_myPow_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.050: https://leetcode-cn.com/problems/powx-n/ 3 | Author: zhangyixing1007 4 | Idea: recursion 5 | Time: 1 ms, beat 94.50% 6 | Space: 37 MB, beat 5.88% 7 | */ 8 | 9 | class Solution { 10 | public double myPow(double x, int n) { 11 | long N=(long) n; 12 | return N>=0? cal(x,N):1.0/cal(x,-N); 13 | } 14 | 15 | double cal(double x, long n){ 16 | if(n==0) return 1.0; 17 | double t=cal(x, n/2); 18 | t*=t; 19 | if(n%2==1) t*=x; 20 | return t; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /050_myPow_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.050: https://leetcode-cn.com/problems/powx-n/ 3 | Author: zhangyixing1007 4 | Idea: iteration 5 | Time: 1 ms, beat 94.50% 6 | Space: 37 MB, beat 5.88% 7 | */ 8 | 9 | class Solution { 10 | public double myPow(double x, int n) { 11 | long N=(long) n; 12 | if(N<0) { x=1/x; N=-N;} 13 | double ans=1.0, t=x; 14 | while(N!=0){ 15 | if(N%2==1){ 16 | ans*=t; 17 | } 18 | t=t*t; 19 | N/=2; 20 | } 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0518_change/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0518: https://leetcode-cn.com/problems/coin-change-2/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming 5 | Time: 4 ms, beat 58.77% 6 | Space: 35.9 MB, beat 98.78% 7 | */ 8 | 9 | class Solution { 10 | public int change(int amount, int[] coins) { 11 | int[] dp=new int[amount+1]; 12 | dp[0]=1; 13 | for(int coin:coins){ 14 | for(int i=0; i<=amount; i++){ 15 | if(i-coin>=0 && dp[i-coin]!=Integer.MAX_VALUE){ 16 | dp[i]+=dp[i-coin]; 17 | } 18 | } 19 | } 20 | return dp[amount]==Integer.MAX_VALUE? -1:dp[amount]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /053_maxSubArray/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.53: https://leetcode.com/problems/maximum-subarray/ 3 | Author: zhangyixing1007 4 | Idea: maxEndingHere, maxSoFar -> compare from the end of subarray instead of beginning 5 | Time: 1 ms, beat 99.96% 6 | Space: 39.5MB, beat 71.04% 7 | */ 8 | 9 | class Solution { 10 | public int maxSubArray(int[] nums) { 11 | if (nums.length==0) return 0; 12 | if (nums.length==1) return nums[0]; 13 | 14 | int maxEndingHere = nums[0]; 15 | int maxSoFar = nums[0]; 16 | for (int i = 1; i= 0; i--){ 13 | if (nums[i]+i>=reachable) reachable = i; 14 | } 15 | return (reachable==0? true:false); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /055_canJump_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.55: https://leetcode.com/problems/jump-game/ 3 | Author: zhangyixing1007 4 | Idea: simplist idea, loop from the end 5 | Time: 1334 ms, beat 5.02% 6 | Space: 39.2 MB, beat 58.34% 7 | */ 8 | 9 | class Solution { 10 | public boolean canJump(int[] nums) { 11 | if(nums==null||nums.length==0) return true; 12 | for(int i=nums.length-2; i>-1; i--){ 13 | for(int j=1; i+j=nums.length-1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /056_merge/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.56: https://leetcode.com/problems/merge-intervals/ 3 | Author: zhangyixing1007 4 | Idea: first sort, then one scan 5 | Time: 8 ms, beat 90.08% 6 | Space: 43.4 MB, beat 68.97% 7 | */ 8 | 9 | class Solution { 10 | public int[][] merge(int[][] intervals) { 11 | if(intervals==null||intervals.length==0) return new int[0][]; 12 | List list=new ArrayList<>(); 13 | Arrays.sort(intervals, new Comparator(){ 14 | @Override 15 | public int compare(int[] o1, int[] o2){ 16 | return o1[0]-o2[0]; 17 | } 18 | }); 19 | int i=0; 20 | while (i=0+index/4; j--, ele++){ 26 | ans[i][j]=ele; 27 | } 28 | i--;j++; 29 | }else if(index%4==0){ 30 | for (; i>=index/4; i--, ele++){ 31 | ans[i][j]=ele; 32 | } 33 | j++;i++; 34 | } 35 | index++; 36 | } 37 | return ans; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /062_uniquePaths_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.62: https://leetcode.com/problems/unique-paths/ 3 | Author: zhangyixing1007 4 | Idea: recursion 5 | Time: overtime 6 | Space: unknown 7 | */ 8 | 9 | class Solution { 10 | public int uniquePaths(int m, int n) { 11 | if(m<=1||n<=1) return 1; 12 | return combination(m-1,m+n-2); 13 | } 14 | int combination(int n, int m){ 15 | if(m==0) return 0; 16 | if(n==0) return 1; 17 | if(n==1) return m; 18 | return combination(n-1, m-1)+combination(n, m-1); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /062_uniquePaths_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.62: https://leetcode.com/problems/unique-paths/ 3 | Author: zhangyixing1007 4 | Idea: simply calculate by formula, use long 5 | Time: 0 ms, beat 100% 6 | Space: 32.8 MB, beat 83.92% 7 | */ 8 | 9 | class Solution { 10 | public int uniquePaths(int m, int n) { 11 | if (m==0||n==0) return 0; 12 | long result = 1; 13 | int count = mx,那么mid一定不是我们所需要的值,r=mid+1是十分保险的 23 | } 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /069_mySqrt_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.069: https://leetcode-cn.com/problems/sqrtx/ 3 | Author: zhangyixing1007 4 | Idea: Newton method 5 | Time: 1 ms, beat 100% 6 | Space: 36.5 MB, beat 5.55% 7 | */ 8 | 9 | class Solution { 10 | public int mySqrt(int x) { 11 | double C=x, y0=0, y1=x; 12 | 13 | while(Math.abs(y0-y1)>=1e-6){ 14 | y0=y1; 15 | y1=0.5*(y0+C/y0); 16 | } 17 | return (int)y1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /070_climbStairs_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.70: https://leetcode.com/problems/climbing-stairs/ 3 | Author: zhangyixing1007 4 | Idea: iteration, save result in array 5 | Time: 0 ms, beat 100% 6 | Space: 32.8MB, beat 74.43% 7 | */ 8 | 9 | 10 | class Solution { 11 | public int climbStairs(int n) { 12 | if (n<4) return n; 13 | 14 | int[] methods = new int[n+1]; 15 | methods[1] = 1; 16 | methods[2] = 2; 17 | for (int i = 3; i=2){ 12 | int l=0, r=nums.length-1; 13 | while(l-1&&nums[r]==2) r--; 15 | for(int i=l; i partitionLabels(String S) { 11 | int[] count=new int[26]; 12 | for(char c:S.toCharArray()){ 13 | count[c-'a']++; 14 | } 15 | List ans=new ArrayList<>(); 16 | Set visited=new HashSet<>(); 17 | int left=0; 18 | for(int i=0; i partitionLabels(String S) { 11 | int[] lastPos=new int[26]; 12 | for(int i=0; i ans=new ArrayList<>(); 17 | while(curr partitionLabels(String S) { 11 | int[] lastPos=new int[26]; 12 | char[] chars=S.toCharArray(); 13 | for(int i=0; i ans=new ArrayList<>(); 18 | while(curr=0) count++; 22 | r++; 23 | while(count==t.length()){ 24 | if(r-l0) count--; 30 | l++; 31 | } 32 | } 33 | return minLen==s.length()+1? "":s.substring(L,R); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /078_subsets_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.78: https://leetcode.com/problems/subsets/ 3 | Author: zhangyixing1007 4 | Idea: queue to keep record 5 | Time: 1 ms, beat 99.83% 6 | Space: 36.1 MB, beat 64.41% 7 | */ 8 | 9 | class Solution{ 10 | public List> subsets(int[] nums){ 11 | Queue> q = new LinkedList<>(); 12 | List l = new ArrayList<>(); 13 | q.add(l); 14 | for (int b : nums){ 15 | while (true){ 16 | List s = q.poll(); 17 | q.add(s); 18 | List t = new ArrayList<>(s); 19 | t.add(b); 20 | q.add(t); 21 | if (q.peek().size()==0) break; 22 | } 23 | } 24 | List> list = new ArrayList<>(q); 25 | return list; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /078_subsets_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.78: https://leetcode.com/problems/subsets/ 3 | Author: zhangyixing1007 4 | Idea: List to keep record 5 | Time: 1 ms, beat 99.83% 6 | Space: 36.7 MB, beat 46.65% 7 | */ 8 | 9 | class Solution{ 10 | public List> subsets(int[] nums){ 11 | List l = new ArrayList<>(); 12 | List> list = new ArrayList<>(); 13 | list.add(l); 14 | for (int b : nums){ 15 | int n = list.size(); 16 | for (int i = 0; i < n; i++){ 17 | List s = list.get(i); 18 | List t = new ArrayList<>(s); 19 | t.add(b); 20 | list.add(t); 21 | } 22 | } 23 | return list; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /078_subsets_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.78: https://leetcode.com/problems/subsets/ 3 | Author: zhangyixing1007 4 | Idea: backtrack, or say simply recursion 5 | Time: 1 ms, beat 99.83% 6 | Space: 35.9 MB, beat 74.86% 7 | */ 8 | 9 | class Solution{ 10 | public List> subsets(int[] nums){ 11 | List l = new ArrayList<>(); 12 | list = new ArrayList<>(); 13 | list.add(l); 14 | backtrack(l,0,nums); 15 | return list; 16 | } 17 | 18 | List> list; 19 | 20 | private void backtrack(List l, int now, int[] nums){ 21 | if (now t = new ArrayList<>(l); 23 | t.add(nums[now]); 24 | list.add(t); 25 | backtrack(l,now+1,nums); 26 | backtrack(t,now+1,nums); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /078_subsets_4/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.78: https://leetcode.com/problems/subsets/ 3 | Author: zhangyixing1007 4 | Idea: bit operation, property of binary 5 | Time: 1 ms, beat 99.81% 6 | Space: 36.9 MB, beat 45.19% 7 | */ 8 | 9 | class Solution { 10 | public List> subsets(int[] nums){ 11 | List> list = new ArrayList<>(); 12 | for (int i = 0; i < 1 << nums.length; i++){ 13 | List subset = new ArrayList<>(); 14 | for (int j = 0; j < nums.length; j++){ 15 | if (((i>>j)&1)==1) 16 | subset.add(nums[j]); 17 | } 18 | list.add(subset); 19 | } 20 | return list; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0844_backspaceCompare/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0844: https://leetcode-cn.com/problems/backspace-string-compare/ 3 | Author: zhangyixing1007 4 | Idea: just char[] 5 | Time: 1 ms, beat 85.73% 6 | Space: 36.8 MB, beat 22.16% 7 | */ 8 | 9 | class Solution { 10 | public boolean backspaceCompare(String S, String T) { 11 | int len=Math.max(S.length(), T.length()); 12 | int[] schar=new int[len]; int i=0; 13 | int[] tchar=new int[len]; 14 | for(int j=0; jA[i+1]){ 25 | p=2; 26 | }else if(i+1= 0; --i) { 21 | right[i] = A[i + 1] < A[i] ? right[i + 1] + 1 : 0; 22 | } 23 | 24 | int ans = 0; 25 | for (int i = 0; i < n; ++i) { 26 | if (left[i] > 0 && right[i] > 0) { 27 | // left[i] + right[i] + 1 是以i为顶峰的最长山脉长度 28 | ans = Math.max(ans, left[i] + right[i] + 1); 29 | } 30 | } 31 | return ans; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /084_largestRectangleArea_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.084: https://leetcode.com/problems/largest-rectangle-in-histogram 3 | Author: zhangyixing1007 4 | Idea: brute force 5 | Time: 705 ms, beat 14.92% 6 | Space: 40.7 MB, beat 90.88% 7 | */ 8 | 9 | class Solution { 10 | public int largestRectangleArea(int[] heights) { 11 | int maxArea = 0; 12 | for (int i = 0; i < heights.length; i++){ 13 | int left = i, right = i; 14 | while (left-1>=0 && heights[left-1]>=heights[i]) left--; 15 | while (right+1=heights[i]) right++; 16 | int area = (right-left+1)*heights[i]; 17 | maxArea = Math.max(maxArea,area); 18 | } 19 | return maxArea; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /084_largestRectangleArea_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.084: https://leetcode.com/problems/largest-rectangle-in-histogram 3 | Author: zhangyixing1007 4 | Idea: divide and conquer 5 | Time: 254 ms, beat 43.65% 6 | Space: 42.3 MB, beat 54.71% 7 | */ 8 | 9 | class Solution { 10 | int maxArea = 0; 11 | public int largestRectangleArea(int[] heights) { 12 | subProblem(heights,0,heights.length-1); 13 | return maxArea; 14 | } 15 | 16 | void subProblem(int[] heights, int left, int right){ 17 | if (left==right){ 18 | maxArea = Math.max(maxArea,heights[left]); 19 | } else if (left < right) { 20 | int min = Integer.MAX_VALUE; 21 | int id = -1; 22 | for (int i = left; i < right+1; i++){ 23 | if (min>heights[i]){ 24 | min = heights[i]; id = i; 25 | } 26 | } 27 | maxArea = Math.max(maxArea, (right-left+1)*min); 28 | subProblem(heights, left, id-1); 29 | subProblem(heights, id+1, right); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /084_largestRectangleArea_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.084: https://leetcode.com/problems/largest-rectangle-in-histogram 3 | Author: zhangyixing1007 4 | Idea: mono stack 5 | Time: 13 ms, beat 82.06% 6 | Space: 40.6 MB, beat 37.91% 7 | */ 8 | 9 | class Solution { 10 | public int largestRectangleArea(int[] heights) { 11 | Deque stack=new ArrayDeque<>(); 12 | stack.push(-1); 13 | int maxArea=0; 14 | for(int i=0; i=heights[i]){ 17 | maxArea=Math.max(maxArea, heights[stack.pop()]*(i-stack.peek()-1)); 18 | } 19 | stack.push(i); 20 | } 21 | while(stack.peek()!=-1){ 22 | maxArea=Math.max(maxArea, heights[stack.pop()]*(heights.length-stack.peek()-1)); 23 | } 24 | return maxArea; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0941_validMountainArray/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.0941: https://leetcode-cn.com/problems/valid-mountain-array/ 3 | Author: zhangyixing1007 4 | Idea: one time linear scan 5 | Time: 1 ms, beat 100% 6 | Space: 39.2 MB, beat 97.75% 7 | */ 8 | 9 | class Solution { 10 | public boolean validMountainArray(int[] A) { 11 | if(A==null || A.length<3) return false; 12 | int l=0, r=A.length-1; 13 | while(l+1=0 && A[r] inorderTraversal(TreeNode root) { 21 | List list=new ArrayList<>(); 22 | Stack stack=new Stack<>(); 23 | TreeNode curr=root; 24 | while(curr!=null||!stack.isEmpty()){ 25 | while(curr!=null){ 26 | stack.push(curr);curr=curr.left; 27 | } 28 | curr=stack.pop(); 29 | list.add(curr.val); 30 | curr=curr.right; 31 | } 32 | return list; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /094_inorderTraversal_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.94: https://leetcode.com/problems/binary-tree-inorder-traversal/ 3 | Author: zhangyixing1007 4 | Idea: recursion, dfs 5 | Time: 1 ms, beat 73.27% 6 | Space: 34.6 MB, beat 72.19% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public List inorderTraversal(TreeNode root) { 21 | list=new ArrayList<>(); 22 | dfs(root); 23 | return list; 24 | } 25 | List list; 26 | void dfs(TreeNode root){ 27 | if(root==null) return; 28 | dfs(root.left); 29 | list.add(root.val); 30 | dfs(root.right); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /096_numTrees_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.96: https://leetcode.com/problems/unique-binary-search-trees/ 3 | Author: zhangyixing1007 4 | Idea: 2-layer loop 5 | Time: 0 ms, beat 100% 6 | Space: 32.8 MB, beat 10.46% 7 | */ 8 | 9 | class Solution { 10 | public int numTrees(int n) { 11 | if (n <= 1) return 1; 12 | else if (n == 2) return 2; 13 | int[] count = new int[n+1]; 14 | count[0] = 1; count[1] = 1; count[2] = 2; 15 | for (int i = 3; i < count.length; i++){ 16 | for (int j = 0; j < i; j++){ 17 | count[i] += count[j] * count[i-j-1]; 18 | } 19 | } 20 | return count[n]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /096_numTrees_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.96: https://leetcode.com/problems/unique-binary-search-trees/ 3 | Author: zhangyixing1007 4 | Idea: recursion, with saving 5 | Time: 0 ms, beat 100% 6 | Space: 32.8 MB, beat 85.45% 7 | */ 8 | 9 | class Solution { 10 | public int numTrees(int n) { 11 | ans=new int[n+1]; 12 | ans[0]=1; 13 | return helper(n); 14 | } 15 | int[] ans; 16 | int helper(int n){ 17 | if (ans[n]!=0) return ans[n]; 18 | int sum=0; 19 | for (int i=0; i=b) return false; 29 | 30 | if (!helper(node.left,a,node.val)) return false; 31 | if (!helper(node.right,node.val,b)) return false; 32 | 33 | return true; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /1002_commonChars_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1002: https://leetcode-cn.com/problems/find-common-characters/ 3 | Author: zhangyixing1007 4 | Idea: HashMap, slow 5 | Time: 25 ms, beat 13.39% 6 | Space: 39 MB, beat 68.35% 7 | */ 8 | 9 | class Solution { 10 | public List commonChars(String[] A) { 11 | Map sum=new HashMap<>(); 12 | Map curr=new HashMap<>(); 13 | for(int i=0; i ans=new ArrayList<>(); 23 | for(char c:sum.keySet()){ 24 | int k=sum.get(c); 25 | while(k-->0){ 26 | ans.add(c+""); 27 | } 28 | } 29 | return ans; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /1002_commonChars_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1002: https://leetcode-cn.com/problems/find-common-characters/ 3 | Author: zhangyixing1007 4 | Idea: int[], fast 5 | Time: 3 ms, beat 98.36% 6 | Space: 38.6 MB, beat 98.67% 7 | */ 8 | 9 | class Solution { 10 | public List commonChars(String[] A) { 11 | int[] sum=new int[26]; Arrays.fill(sum, Integer.MAX_VALUE); 12 | int[] curr=new int[26]; 13 | for(int i=0; i ans=new ArrayList<>(); 23 | for(int j=0; j<26; j++){ 24 | while(sum[j]-->0){ 25 | ans.add(Character.toString(j+'a')); 26 | } 27 | } 28 | return ans; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /101_isSymmetric/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.101: https://leetcode.com/problems/symmetric-tree/ 3 | Author: zhangyixing1007 4 | Idea: recursion 5 | Time: 1 ms, beat 99.31% 6 | Space: 37.5MB, beat 68.40% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public boolean isSymmetric(TreeNode root) { 21 | if (root == null) return true; 22 | return checkChildren (root.left,root.right); 23 | } 24 | 25 | private boolean checkChildren (TreeNode left, TreeNode right) 26 | { 27 | if (left==null||right==null) 28 | return (left==null && right == null); 29 | if (left.val != right.val) return false; 30 | return (checkChildren(left.left,right.right) && checkChildren(left.right,right.left)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /1024_videoStitching_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1024: https://leetcode-cn.com/problems/video-stitching/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming 5 | Time: 2 ms, beat 22.95% 6 | Space: 35.8 MB, beat 98.36% 7 | */ 8 | 9 | class Solution { 10 | public int videoStitching(int[][] clips, int T) { 11 | int[] dp = new int[T + 1]; 12 | Arrays.fill(dp, T+1); 13 | dp[0] = 0; 14 | for (int i = 1; i <= T; i++) { 15 | for (int[] clip : clips) { 16 | if (clip[0] < i && i <= clip[1]) { 17 | dp[i] = Math.min(dp[i], dp[clip[0]] + 1); 18 | } 19 | } 20 | } 21 | return dp[T] == T+1? -1 : dp[T]; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /1024_videoStitching_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1024: https://leetcode-cn.com/problems/video-stitching/ 3 | Author: zhangyixing1007 4 | Idea: sorting + dynamic programming 5 | Time: 2 ms, beat 22.95% 6 | Space: 35.7 MB, beat 98.63% 7 | */ 8 | 9 | class Solution { 10 | public int videoStitching(int[][] clips, int T) { 11 | int[] dp = new int[T + 1]; 12 | Arrays.fill(dp, T+1); 13 | Arrays.sort(clips, (o1, o2)->o1[0]==o2[0]? o1[1]-o2[1]:o1[0]-o2[0]); 14 | dp[0] = 0; 15 | int i=0, prev=-1; 16 | for(; iT) continue; 14 | max[clip[0]]=Math.max(max[clip[0]], clip[1]); 15 | } 16 | int prev=0, last=0, ans=0; 17 | for(int i=0; i() each time 5 | Time: 3 ms, beat 17.98% 6 | Space: 42.9 MB, beat 5.26% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | class Solution { 19 | public List> pathSum(TreeNode root, int sum) { 20 | if(root==null) return ans; 21 | pathSum(root,sum,new ArrayList()); 22 | return ans; 23 | } 24 | List> ans=new ArrayList<>(); 25 | void pathSum(TreeNode root, int sum, List list){ 26 | sum-=root.val; 27 | list.add(root.val); 28 | if(root.left==null&&root.right==null){ 29 | if(sum==0) ans.add(list); 30 | return; 31 | } 32 | if(root.left!=null) pathSum(root.left, sum, new ArrayList(list)); 33 | if(root.right!=null) pathSum(root.right, sum, new ArrayList(list)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /113_pathSum_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.113: https://leetcode-cn.com/problems/path-sum-ii/ 3 | Author: zhangyixing1007 4 | Idea: dfs, add and remove last 5 | Time: 1 ms, beat 100% 6 | Space: 39.5 MB, beat 20.04% 7 | */ 8 | 9 | class Solution { 10 | public List> pathSum(TreeNode root, int sum) { 11 | if(root==null) return ans; 12 | pathSum(root,sum,new ArrayList()); 13 | return ans; 14 | } 15 | List> ans=new ArrayList<>(); 16 | void pathSum(TreeNode root, int sum, List list){ 17 | if(root==null) return; 18 | sum-=root.val; 19 | list.add(root.val); 20 | if(root.left==null&&root.right==null){ 21 | if(sum==0) ans.add(new ArrayList<>(list)); 22 | }else{ 23 | pathSum(root.left, sum, list); 24 | pathSum(root.right, sum, list); 25 | } 26 | list.remove(list.size()-1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /114_flatten_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.114: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ 3 | Author: zhangyixing1007 4 | Idea: recursion, most simple thought 5 | Time: 1 ms, beat 99.38% 6 | Space: 36 MB, beat 81.87% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public void flatten(TreeNode root) { 21 | dfs(root); 22 | } 23 | TreeNode dfs(TreeNode root){ 24 | if (root==null) return null; 25 | if (root.left==null&&root.right==null) return root; 26 | TreeNode tmp=root.right; 27 | root.right=dfs(root.left); 28 | root.left=null; 29 | TreeNode curr=root; 30 | while (curr!=null&&curr.right!=null) curr=curr.right; 31 | curr.right=dfs(tmp); 32 | return root; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /114_flatten_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.114: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ 3 | Author: zhangyixing1007 4 | Idea: recursion, very short version 5 | Time: 1 ms, beat 99.38% 6 | Space: 36 MB, beat 81.11% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution 20 | { 21 | private TreeNode pre = null; 22 | public void flatten(TreeNode root) 23 | { 24 | if (root==null) return; 25 | flatten(root.right); 26 | flatten(root.left); 27 | root.right = pre; 28 | root.left = null; 29 | pre = root; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /116_connect/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.116: https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/ 3 | Author: zhangyixing1007 4 | Idea: iteration 5 | Time: 0 ms, beat 100% 6 | Space: 40 MB, beat 13.3% 7 | */ 8 | 9 | /* 10 | // Definition for a Node. 11 | class Node { 12 | public int val; 13 | public Node left; 14 | public Node right; 15 | public Node next; 16 | 17 | public Node() {} 18 | 19 | public Node(int _val) { 20 | val = _val; 21 | } 22 | 23 | public Node(int _val, Node _left, Node _right, Node _next) { 24 | val = _val; 25 | left = _left; 26 | right = _right; 27 | next = _next; 28 | } 29 | }; 30 | */ 31 | 32 | class Solution { 33 | public Node connect(Node root) { 34 | if(root==null) return root; 35 | Node leftHead=root; 36 | while(leftHead.left!=null){ 37 | Node head=leftHead; 38 | while(head!=null){ 39 | head.left.next=head.right; 40 | if(head.next!=null) head.right.next=head.next.left; 41 | head=head.next; 42 | } 43 | leftHead=leftHead.left; 44 | } 45 | return root; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /1207_uniqueOccurrences/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | LeetCode Problem No.1207: https://leetcode-cn.com/problems/unique-number-of-occurrences/ 4 | Author: zhangyixing1007 5 | Idea: int[] + HashSet 6 | Time: 2 ms, beat 90.97% 7 | Space: 38.1 MB, beat 11.40% 8 | */ 9 | 10 | class Solution { 11 | public boolean uniqueOccurrences(int[] arr) { 12 | int[] count=new int[2001]; 13 | for(int a:arr){ 14 | count[a+1000]++; 15 | } 16 | Set time=new HashSet(); 17 | for(int i=0; i<2001; i++){ 18 | if(count[i]==0) continue; 19 | if(time.contains(count[i])) return false; 20 | time.add(count[i]); 21 | } 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /121_maxProfit/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.121: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 3 | Author: zhangyixing1007 4 | Idea: take difference with so far min, find the max 5 | Time: 1 ms, beat 99.96% 6 | Space: 36.7MB, beat 95.21% 7 | */ 8 | 9 | class Solution { 10 | public int maxProfit(int[] prices) { 11 | int min=Integer.MAX_VALUE, max=0; 12 | for(int price:prices){ 13 | min=Math.min(min,price); 14 | if(price-min>max) max=price-min; 15 | } 16 | return max; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /1248_numberOfSubarrays_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1248: https://leetcode.com/problems/count-number-of-nice-subarrays/ 3 | Author: zhangyixing1007 4 | Idea: list save index of each odd number 5 | Time: 19 ms, beat 17.42% 6 | Space: 48.8 MB, beat 100% 7 | */ 8 | 9 | class Solution { 10 | public int numberOfSubarrays(int[] nums, int k) { 11 | if(nums==null||nums.length==0) return 0; 12 | List list=new ArrayList<>(); 13 | list.add(-1); 14 | for(int i=0; i> partition(String s) { 11 | list=new ArrayList<>(); 12 | find(s, "", 0, new ArrayList()); 13 | return list; 14 | } 15 | List> list; 16 | 17 | void find(String s, String t, int curr, List l){ 18 | if(curr==s.length()) { 19 | if(t.length()==0) list.add(l); 20 | return; 21 | } 22 | t=t+s.substring(curr,curr+1); 23 | List l2=new ArrayList(l); 24 | if(t.length()!=0&&isPalindrome(t)){ 25 | l.add(t); 26 | find(s, "", curr+1, l); 27 | } 28 | find(s, t, curr+1, l2); 29 | } 30 | 31 | boolean isPalindrome(String t){ 32 | int l=0, r=t.length()-1; 33 | while(l<=r&&t.charAt(l)==t.charAt(r)){ 34 | l++; r--; 35 | } 36 | return l>=r; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /131_partition_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.131: https://leetcode-cn.com/problems/palindrome-partitioning/ 3 | Author: zhangyixing1007 4 | Idea: lopping, backtrack 5 | Time: 2 ms, beat 99.13% 6 | Space: 40.7 MB, beat 23.53% 7 | */ 8 | 9 | class Solution { 10 | public List> partition(String s) { 11 | List> list=new ArrayList<>(); 12 | find(s, 0, new ArrayDeque(), list); 13 | return list; 14 | } 15 | 16 | void find(String s, int curr, ArrayDeque l, List> list){ 17 | if(curr==s.length()) { 18 | list.add(new ArrayList(l)); return; 19 | } 20 | for(int j=curr; j=r; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /1365_smallerNumbersThanCurrent_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1365: https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 3 | Author: zhangyixing1007 4 | Idea: sort + binarySearch 5 | Time: 4 ms, beat 73.11% 6 | Space: 38.5 MB, beat 95.35% 7 | */ 8 | 9 | class Solution { 10 | public int[] smallerNumbersThanCurrent(int[] nums) { 11 | int[] tmp=new int[nums.length]; 12 | System.arraycopy(nums, 0, tmp, 0, tmp.length); 13 | Arrays.sort(tmp); 14 | for(int i=0; io1[0]-o2[0]); 18 | int prev=-1; 19 | for(int i=0; i wordDict) 12 | { 13 | this.s = s; 14 | this.wordDict = wordDict; 15 | return backtrack(0); 16 | } 17 | 18 | private String s; 19 | private List wordDict; 20 | 21 | private boolean backtrack(int curr) 22 | { 23 | if (curr == s.length()) return true; 24 | 25 | for (int i = 0; i < wordDict.size(); i++) 26 | { 27 | String tmp = wordDict.get(i); 28 | int len = tmp.length(); 29 | if (len<=s.length()-curr && tmp.equals(s.substring(curr,curr+len)) && backtrack(curr+len)==true) 30 | return true; 31 | 32 | } 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /139_wordBreak_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.139: https://leetcode.com/problems/word-break/ 3 | Author: zhangyixing1007 4 | Idea: recursion, keep the record 5 | Time: 9 ms, beat 74.65% 6 | Space: 36.8 MB, beat 77.49% 7 | */ 8 | 9 | class Solution { 10 | public boolean wordBreak(String s, List wordDict) { 11 | if(s==null||s.length()==0||wordDict==null||wordDict.size()==0) return false; 12 | return helper(s, new HashSet(wordDict), 0, new Boolean[s.length()+1]); 13 | } 14 | private boolean helper(String s, HashSet set, int start, Boolean[] record){ 15 | if(start==s.length()) return true; 16 | if (record[start]!=null) return record[start]; 17 | for (int i=start+1; i<=s.length(); i++){ 18 | if(set.contains(s.substring(start,i))&&helper(s,set,i,record)){ 19 | record[start]=true; 20 | return true; 21 | } 22 | } 23 | record[start]=false; 24 | return false; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /139_wordBreak_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.139: https://leetcode.com/problems/word-break/ 3 | Author: zhangyixing1007 4 | Idea: iteration, keep the record 5 | Time: 8 ms, beat 79.40% 6 | Space: 36.4 MB, beat 80.84% 7 | */ 8 | 9 | class Solution { 10 | public boolean wordBreak(String s, List wordDict) { 11 | if(s==null||s.length()==0||wordDict==null||wordDict.size()==0) 12 | return false; 13 | boolean[] visited=new boolean[s.length()+1]; 14 | HashSet set= new HashSet<>(wordDict); 15 | visited[0]=true; 16 | for (int i=0; i wordDict) 12 | { 13 | Set set = new HashSet(wordDict); 14 | boolean[] visited = new boolean[s.length()]; 15 | Queue q = new LinkedList<>(); 16 | q.add(0); 17 | while (!q.isEmpty()) 18 | { 19 | int start = q.poll(); 20 | if (visited[start] == false) 21 | { 22 | for (int end = start; end <= s.length(); end++) 23 | { 24 | if (set.contains(s.substring(start,end))) 25 | { 26 | q.add(end); 27 | if (end == s.length()) return true; 28 | } 29 | } 30 | visited[start] = true; 31 | } 32 | } 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /1434_numberWays_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1434: https://leetcode-cn.com/problems/number-of-ways-to-wear-different-hats-to-each-other/ 3 | Author: zhangyixing1007 4 | Idea: backtrack, brute force 5 | Time: overtime 6 | Space: unknown 7 | */ 8 | 9 | class Solution { 10 | public int numberWays(List> hats) { 11 | boolean[] visited=new boolean[40]; 12 | back(hats, 0, visited); 13 | return ans; 14 | } 15 | 16 | int ans=0; 17 | 18 | void back(List> hats, int i, boolean[] visited){ 19 | List fav=hats.get(i); 20 | for(int j=0; j preorderTraversal(TreeNode root) { 26 | ans=new ArrayList<>(); 27 | preOrder(root); 28 | return ans; 29 | } 30 | List ans; 31 | void preOrder(TreeNode root){ 32 | if(root==null) return; 33 | ans.add(root.val); 34 | preOrder(root.left); 35 | preOrder(root.right); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /144_preorderTraversal_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.144: https://leetcode-cn.com/problems/binary-tree-preorder-traversal/ 3 | Author: zhangyixing1007 4 | Idea: dfs, iteration 5 | Time: 0 ms, beat 100% 6 | Space: 36.9 MB, beat 77.01% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode() {} 16 | * TreeNode(int val) { this.val = val; } 17 | * TreeNode(int val, TreeNode left, TreeNode right) { 18 | * this.val = val; 19 | * this.left = left; 20 | * this.right = right; 21 | * } 22 | * } 23 | */ 24 | class Solution { 25 | public List preorderTraversal(TreeNode root) { 26 | List ans=new ArrayList<>(); 27 | if(root==null) return ans; 28 | Deque stack=new ArrayDeque<>(); 29 | stack.addLast(root); 30 | while(!stack.isEmpty()){ 31 | TreeNode curr=stack.pollLast(); 32 | ans.add(curr.val); 33 | if(curr.right!=null) stack.addLast(curr.right); 34 | if(curr.left!=null) stack.addLast(curr.left); 35 | } 36 | return ans; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /145_postorderTraversal_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.145: https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ 3 | Author: zhangyixing1007 4 | Idea: dfs, recursion 5 | Time: 0 ms, beat 100% 6 | Space: 39.4 MB, beat 5.09% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode() {} 16 | * TreeNode(int val) { this.val = val; } 17 | * TreeNode(int val, TreeNode left, TreeNode right) { 18 | * this.val = val; 19 | * this.left = left; 20 | * this.right = right; 21 | * } 22 | * } 23 | */ 24 | 25 | class Solution { 26 | public List postorderTraversal(TreeNode root) { 27 | ans=new ArrayList<>(); 28 | postOrder(root); 29 | return ans; 30 | } 31 | List ans; 32 | void postOrder(TreeNode root){ 33 | if(root==null) return; 34 | postOrder(root.left); 35 | postOrder(root.right); 36 | ans.add(root.val); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /146_LRUCache_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.146: https://leetcode.com/problems/lru-cache/ 3 | Author: zhangyixing1007 4 | Idea: LinkedHashMap 5 | Time: 75 ms, beat 97.63% 6 | Space: 50.7 MB, beat 99.67% 7 | */ 8 | 9 | class LRUCache extends LinkedHashMap{ 10 | int capacity; 11 | 12 | public LRUCache(int capacity) { 13 | super(capacity, 0.75F, true); 14 | this.capacity=capacity; 15 | } 16 | 17 | public int get(int key) { 18 | return super.getOrDefault(key,-1); 19 | } 20 | 21 | public void put(int key, int value) { 22 | super.put(key, value); 23 | } 24 | 25 | @Override 26 | protected boolean removeEldestEntry(Map.Entry eldest){ 27 | return capacity list=Arrays.asList(s.split("\\s+")); 12 | Collections.reverse(list); 13 | return String.join(" ",list).trim(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /152_maxProduct/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.152: https://leetcode.com/problems/maximum-product-subarray/ 3 | Author: zhangyixing1007 4 | Idea: similar to 53, mind negative numbers 5 | Time: 2 ms, beat 98.47% 6 | Space: 37 MB, beat 42.42% 7 | */ 8 | 9 | class Solution { 10 | public int maxProduct(int[] nums) { 11 | if(nums==null||nums.length==0) return 0; 12 | if(nums.length==1) return nums[0]; 13 | int max=1; 14 | int min=1; 15 | int maxSoFar=Integer.MIN_VALUE; 16 | for (int i=0; i(); 14 | helper = new Stack<>(); 15 | } 16 | 17 | Stack stack; 18 | Stack helper; 19 | 20 | public void push(int x) { 21 | stack.push(x); 22 | if (helper.isEmpty()) helper.push(x); 23 | else helper.push(Math.min(helper.peek(),x)); 24 | } 25 | 26 | public void pop() { 27 | if (!stack.isEmpty()){ 28 | stack.pop(); 29 | helper.pop(); 30 | } 31 | } 32 | 33 | public int top() { 34 | return stack.peek(); 35 | } 36 | 37 | public int getMin() { 38 | return helper.peek(); 39 | } 40 | } 41 | 42 | /** 43 | * Your MinStack object will be instantiated and called as such: 44 | * MinStack obj = new MinStack(); 45 | * obj.push(x); 46 | * obj.pop(); 47 | * int param_3 = obj.top(); 48 | * int param_4 = obj.getMin(); 49 | */ 50 | -------------------------------------------------------------------------------- /156_canMeasureWater/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.156: https://leetcode-cn.com/problems/water-and-jug-problem/ 3 | Author: zhangyixing1007 4 | Idea: find gcd 5 | Time: 0 ms, beat 100% 6 | Space: 36.3 MB, beat 50% 7 | */ 8 | 9 | class Solution { 10 | public boolean canMeasureWater(int x, int y, int z) { 11 | if(x==0||y==0) return z==0; 12 | if(z==0) return true; 13 | if(x+y B beginning, B end -> A beginning 5 | Time: 1 ms, beat 100% 6 | Space: 38.6MB, beat 84.07% 7 | */ 8 | 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * public class ListNode { 13 | * int val; 14 | * ListNode next; 15 | * ListNode(int x) { 16 | * val = x; 17 | * next = null; 18 | * } 19 | * } 20 | */ 21 | public class Solution { 22 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 23 | if (headA==null||headB==null) return null; 24 | ListNode A=headA, B=headB; 25 | while(A!=B){ 26 | A=A==null? headB:A.next; 27 | B=B==null? headA:B.next; 28 | } 29 | return A; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /1611_minimumOneBitOperations_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1611: https://leetcode-cn.com/problems/minimum-one-bit-operations-to-make-integers-zero/ 3 | Author: zhangyixing1007 4 | Idea: memory recursion 5 | Time: 4 ms, beat 13.44% 6 | Space: 36.8 MB, beat 9,68% 7 | */ 8 | 9 | class Solution { 10 | public int minimumOneBitOperations(int n) { 11 | map=new HashMap<>(); 12 | int i=1; map.put(i,1); map.put(0,0); 13 | while(i map; 20 | int dfs(int n){ 21 | if(map.containsKey(n)) return map.get(n); 22 | int t=n; 23 | t|=t>>1; 24 | t|=t>>2; 25 | t|=t>>4; 26 | t|=t>>8; 27 | t|=t>>16; 28 | t+=1; 29 | t>>=1; 30 | int ans=dfs(t)-dfs(n-t); 31 | map.put(n, ans); 32 | return ans; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /1611_minimumOneBitOperations_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.1611: https://leetcode-cn.com/problems/minimum-one-bit-operations-to-make-integers-zero/ 3 | Author: zhangyixing1007 4 | Idea: math formula 5 | Time: 0 ms, beat 100% 6 | Space: 35.6 MB, beat 81.18% 7 | */ 8 | 9 | class Solution { 10 | public int minimumOneBitOperations(int n) { 11 | int res=0; 12 | while(n!=0){ 13 | res^=n; 14 | n>>=1; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /169_majorityElement/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.169: https://leetcode.com/problems/majority-element/ 3 | Author: zhangyixing1007 4 | Idea: majority and minority commit suicide in pairs 5 | Time: 4 ms, beat 70.12% 6 | Space: 41.9MB, beat 83.65% 7 | */ 8 | 9 | class Solution { 10 | public int majorityElement(int[] nums) { 11 | int count = 0; 12 | int candidate = nums[0]; 13 | for (int num : nums) { 14 | if (count == 0) candidate = num; 15 | count += (num == candidate) ? 1 : -1; 16 | } 17 | return candidate; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /198_rob_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.198: https://leetcode.com/problems/house-robber/ 3 | Author: zhangyixing1007 4 | Idea: interesting loop 5 | Time: 0 ms, beat 100% 6 | Space: 34.1MB, beat 47.64% 7 | */ 8 | 9 | class Solution { 10 | public int rob(int[] nums) { 11 | int first = 0; 12 | int second = 0; 13 | for (int num:nums) { 14 | int tmp = second; 15 | second = Math.max(first+num,second); 16 | first = tmp; 17 | } 18 | return second; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /198_rob_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.198: https://leetcode.com/problems/house-robber/ 3 | Author: zhangyixing1007 4 | Idea: save and recursion 5 | Time: 0 ms, beat 100% 6 | Space: 34.2MB, beat 32.56% 7 | */ 8 | 9 | class Solution { 10 | public int rob(int[] nums) { 11 | int len=nums.length; 12 | if(len==0) return 0; 13 | if(len==1) return nums[0]; 14 | visited=new boolean[len]; 15 | return Math.max(rob(nums,len-1),rob(nums,len-2)); 16 | } 17 | boolean[] visited; 18 | int rob(int[] nums,int len){ 19 | if(len==0) return nums[0]; 20 | if(len==1) return Math.max(nums[0],nums[1]); 21 | if(len==2) return Math.max(nums[0]+nums[2],nums[1]); 22 | if(!visited[len]) { 23 | visited[len]=true; 24 | nums[len]=Math.max(rob(nums,len-2)+nums[len],rob(nums,len-1)); 25 | } 26 | return nums[len]; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /200_numIslands_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.200: https://leetcode.com/problems/number-of-islands/ 3 | Author: zhangyixing1007 4 | Idea: dfs 5 | Time: 2 ms, beat 98.37% 6 | Space: 41.1 MB, beat 85.59% 7 | */ 8 | 9 | class Solution 10 | { 11 | public int numIslands(char[][] grid) 12 | { 13 | int island = 0; 14 | for (int i = 0; i < grid.length; i++) 15 | { 16 | for (int j = 0; j < grid[0].length; j++) 17 | { 18 | if (grid[i][j] == '1') 19 | {dfs(grid,i,j);island++;} 20 | } 21 | } 22 | return island; 23 | } 24 | 25 | private void dfs (char[][] grid, int i, int j) 26 | { 27 | grid[i][j] = 0; 28 | if (i < grid.length-1 && grid[i+1][j] == '1') 29 | dfs(grid,i+1,j); 30 | if (j < grid[0].length-1 && grid[i][j+1] == '1') 31 | dfs(grid,i,j+1); 32 | if (i > 0 && grid[i-1][j] == '1') 33 | dfs(grid,i-1,j); 34 | if (j > 0 && grid[i][j-1] == '1') 35 | dfs(grid,i,j-1); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /204_countPrimes_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.204: https://leetcode-cn.com/problems/count-primes/ 3 | Author: zhangyixing1007 4 | Idea: Sieve of Eratosthenes 5 | Time: 32 ms, beat 30.02% 6 | Space: 50.4 MB, beat 5.72% 7 | */ 8 | 9 | class Solution { 10 | public int countPrimes(int n) { 11 | if(n<2) return 0; 12 | int[] visited=new int[n]; 13 | visited[0]=1; visited[1]=1; 14 | int count=0; 15 | for(int i=2; i2) ans++; 14 | for(int i=3; i1){ 16 | numCourses --; 17 | int delete=-1; 18 | for (int i = 0; i < count.length; i++){ 19 | if (count[i] == 0) {delete = i; break;} 20 | } 21 | if (delete == -1) return false; 22 | count[delete] = -1; 23 | for (int i = 0; i < prerequisites.length; i++){ 24 | if (prerequisites[i][0] == delete){ 25 | count[prerequisites[i][1]]--; 26 | prerequisites[i][0] = -1; 27 | prerequisites[i][1] = -1; 28 | } 29 | } 30 | } 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /207_canFinish_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.207: https://leetcode.com/problems/course-schedule/ 3 | Author: zhangyixing1007 4 | Idea: dfs, find circle 5 | Time: 6 ms, beat 95.72% 6 | Space: 44.3 MB, beat 71.56% 7 | */ 8 | 9 | class Solution { 10 | public boolean canFinish(int numCourses, int[][] prerequisites) { 11 | HashSet[] nexts=new HashSet[numCourses]; 12 | for (int i = 0; i < numCourses; i++){ 13 | nexts[i] = new HashSet(); 14 | } 15 | for (int[] pre:prerequisites){ 16 | nexts[pre[1]].add(pre[0]); 17 | } 18 | int[] visited=new int[numCourses]; 19 | for (int i=0; i[] nexts, int[] visited){ 26 | if(visited[i]==-1) return true; 27 | if(visited[i]==1) return false; 28 | visited[i]=-1; 29 | for(int next:nexts[i]){ 30 | if(hasCircle(next, nexts, visited)) 31 | return true; 32 | } 33 | visited[i]=1; 34 | return false; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /207_canFinish_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.207: https://leetcode.com/problems/course-schedule/ 3 | Author: zhangyixing1007 4 | Idea: topological sorting, based on Version 1 5 | Time: 40 ms, beat 52.1% 6 | Space: 44.8 MB, beat 58.02% 7 | */ 8 | 9 | class Solution{ 10 | public boolean canFinish(int numCourses, int[][] prerequisites){ 11 | int[] count = new int[numCourses]; 12 | for (int i = 0; i < prerequisites.length; i++){ 13 | count[prerequisites[i][1]]++; 14 | } 15 | while (numCourses>1){ 16 | numCourses --; 17 | int delete=-1; 18 | for (int i = 0; i < count.length; i++){ 19 | if (count[i] == 0) {delete = i; break;} 20 | } 21 | if (delete == -1) return false; 22 | count[delete] = -1; 23 | for (int i = 0; i < prerequisites.length; i++){ 24 | if (prerequisites[i][0] == delete && 25 | count[prerequisites[i][1]]>0){ 26 | count[prerequisites[i][1]]--; 27 | } 28 | } 29 | } 30 | return true; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /215_findKthLargest_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.215: https://leetcode.com/problems/kth-largest-element-in-an-array/ 3 | Author: zhangyixing1007 4 | Idea: Array.sort 5 | Time: 4 ms, beat 89.98% 6 | Space: 95.89 MB, beat 94.67% 7 | */ 8 | 9 | class Solution{ 10 | public int findKthLargest(int[] nums, int k){ 11 | Arrays.sort(nums); 12 | return nums[nums.length-k]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /215_findKthLargest_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.215: https://leetcode.com/problems/kth-largest-element-in-an-array/ 3 | Author: zhangyixing1007 4 | Idea: use PriorityQueue 5 | Time: 6 ms, beat 29.16% 6 | Space: 88.65 MB, beat 94.18% 7 | */ 8 | 9 | class Solution { 10 | public int findKthLargest(int[] nums, int k) { 11 | PriorityQueue q = new PriorityQueue<>((n1,n2)->n1-n2); 12 | for (int num:nums){ 13 | q.add(num); 14 | if (q.size()>k) q.poll(); 15 | } 16 | return q.poll(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /215_findKthLargest_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.215: https://leetcode.com/problems/kth-largest-element-in-an-array/ 3 | Author: zhangyixing1007 4 | Idea: quickSort 5 | Time: 2 ms, beat 99.22% 6 | Space: 32.7 MB, beat 94.63% 7 | */ 8 | 9 | class Solution { 10 | public int findKthLargest(int[] nums, int k) { 11 | this.nums=nums; 12 | return find(k-1, 0, nums.length-1); 13 | } 14 | int[] nums; 15 | void swap(int i, int j){ 16 | int t=nums[i]; 17 | nums[i]=nums[j]; 18 | nums[j]=t; 19 | } 20 | int find(int k, int left, int right){ 21 | if(left==right) return nums[left]; 22 | Random random = new Random(); 23 | int mid=left+random.nextInt(right-left+1); 24 | swap(mid,right); 25 | int j=left; 26 | for(int i=left; inums[right]){ 28 | swap(i,j); 29 | j++; 30 | } 31 | } 32 | swap(j,right); 33 | if(j==k) return nums[k]; 34 | else if (jmatrix.length||j+edge>matrix[0].length) return; 26 | if (matrix[i+edge-1][j+edge-1]=='0') return; 27 | for (int p = 0; p < edge; p++) 28 | { 29 | if (matrix[i+p][j+edge-1]=='0'||matrix[i+edge-1][j+p]=='0') return; 30 | } 31 | maxEdge = Math.max(maxEdge,edge); 32 | maxArea(i,j,edge+1,matrix); 33 | } 34 | 35 | int maxEdge = 0; 36 | } 37 | -------------------------------------------------------------------------------- /221_maximalSquare_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.221: https://leetcode.com/problems/maximal-square/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming, 2-d array as saving 5 | Time: 6 ms, beat 95.16% 6 | Space: 40.9 MB, beat 88.65% 7 | */ 8 | 9 | class Solution 10 | { 11 | public int maximalSquare(char[][] matrix) 12 | { 13 | int row = matrix.length; 14 | int col = row >0 ? matrix[0].length:0; 15 | int[][] dp = new int[row+1][col+1]; 16 | int maxEdge = 0; 17 | for (int i = 1; i <= row; i++) 18 | { 19 | for (int j = 1; j <= col; j++) 20 | { 21 | if (matrix[i-1][j-1]=='1') 22 | { 23 | dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i-1][j]),dp[i][j-1])+1; 24 | maxEdge = Math.max(maxEdge,dp[i][j]); 25 | } 26 | } 27 | } 28 | return maxEdge*maxEdge; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /221_maximalSquare_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.221: https://leetcode.com/problems/maximal-square/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming, 2-d array as saving 5 | Time: 6 ms, beat 95.16% 6 | Space: 40.9 MB, beat 88.65% 7 | */ 8 | 9 | class Solution { 10 | public int maximalSquare(char[][] matrix) { 11 | if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0; 12 | int[][] dp = new int[matrix.length+1][matrix[0].length+1]; 13 | int ans = 0; 14 | for (int i = 1; i < dp.length; i++){ 15 | for (int j = 1; j < dp[0].length; j++){ 16 | if (matrix[i-1][j-1] == '1'){ 17 | dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i][j-1]),dp[i-1][j])+1; 18 | ans = Math.max(ans,dp[i][j]); 19 | } 20 | } 21 | } 22 | return ans*ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /221_maximalSquare_4/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.221: https://leetcode.com/problems/maximal-square/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming, 1-d array as saving 5 | Time: 6 ms, beat 87.83% 6 | Space: 41.1 MB, beat 89.62% 7 | */ 8 | 9 | class Solution { 10 | public int maximalSquare(char[][] matrix) { 11 | int row = matrix.length; 12 | int col = matrix.length==0? 0:matrix[0].length; 13 | int[] dp = new int[col+1]; 14 | int ans = 0; 15 | for (int i = 1; i < row+1; i++){ 16 | int prev = 0; 17 | for (int j = 1; j < col+1; j++){ 18 | int temp = dp[j]; 19 | if (matrix[i-1][j-1]=='1'){ 20 | dp[j] = Math.min(Math.min(dp[j-1],dp[j]),prev)+1; 21 | ans = Math.max(ans,dp[j]); 22 | } else {dp[j] = 0;} 23 | prev = temp; 24 | } 25 | } 26 | return ans*ans; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /226_invertTree/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.226: https://leetcode.com/problems/invert-binary-tree/ 3 | Author: zhangyixing1007 4 | Idea: recursion 5 | Time: 0 ms, beat 100% 6 | Space: 34.3MB, beat 35.04% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 |     public TreeNode invertTree(TreeNode root) { 21 |         if (root==null) return null; 22 |          23 |         TreeNode tmp = root.left; 24 |         root.left = root.right; 25 |         root.right = tmp; 26 |         root.left = invertTree(root.left); 27 |         root.right = invertTree(root.right); 28 |          29 |         return root; 30 |     } 31 | } 32 | -------------------------------------------------------------------------------- /230_kthSmallest_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.230: https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 3 | Author: zhangyixing1007 4 | Idea: inorder traversal by stack, iteration 5 | Time: 1 ms, beat 61.55% 6 | Space: 39.9 MB, beat 8.7% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | class Solution { 19 | public int kthSmallest(TreeNode root, int k) { 20 | Stack stack=new Stack<>(); 21 | TreeNode curr=root; 22 | while(!stack.isEmpty()||curr!=null){ 23 | while(curr!=null){ 24 | stack.push(curr); 25 | curr=curr.left; 26 | } 27 | curr=stack.pop(); 28 | k--; 29 | if(k==0) break; 30 | curr=curr.right; 31 | } 32 | return curr.val; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /230_kthSmallest_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.230: https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 3 | Author: zhangyixing1007 4 | Idea: inorder traversal, recursion 5 | Time: 0 ms, beat 100% 6 | Space: 39 MB, beat 8.7% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode() {} 16 | * TreeNode(int val) { this.val = val; } 17 | * TreeNode(int val, TreeNode left, TreeNode right) { 18 | * this.val = val; 19 | * this.left = left; 20 | * this.right = right; 21 | * } 22 | * } 23 | */ 24 | class Solution { 25 | public int kthSmallest(TreeNode root, int k) { 26 | this.k=k; 27 | bfs(root); 28 | return Kth; 29 | } 30 | 31 | int Kth=0; 32 | int k; 33 | 34 | void bfs(TreeNode root){ 35 | if(root==null) return; 36 | bfs(root.left); 37 | if(--k==0) Kth=root.val; 38 | bfs(root.right); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /234_isPalindrome_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.234: https://leetcode.com/problems/palindrome-linked-list/ 3 | Author: zhangyixing1007 4 | Idea: fast/slow pointers, reverse the rest 1/2 5 | Time: 1 ms, beat 99.89% 6 | Space: 40.4MB, beat 97.76% 7 | */ 8 | 9 | class Solution { 10 | public boolean isPalindrome(ListNode head) { 11 | if(head==null) return true; 12 | ListNode prev=null; 13 | ListNode slow=head, fast=head; 14 | while(fast!=null&&fast.next!=null){ 15 | slow=slow.next; 16 | fast=fast.next.next; 17 | } 18 | 19 | if (fast!=null) slow=slow.next; 20 | while(slow!=null){ 21 | ListNode tmp=slow; 22 | slow=slow.next; 23 | tmp.next=prev; 24 | prev=tmp; 25 | } 26 | slow=prev;fast=head; 27 | while(slow!=null){ 28 | if(slow.val==fast.val){slow=slow.next; fast=fast.next;} 29 | else return false; 30 | } 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /235_lowestCommonAncestor/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.235: https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3 | Author: zhangyixing1007 4 | Idea: trinary search 5 | Time: 6 ms, beat 99.70% 6 | Space: 39.7 MB, beat 36.8% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 21 | if(root==p||root==q) return root; 22 | if(p.val>q.val){ 23 | TreeNode t=p; 24 | p=q; 25 | q=t; 26 | } 27 | if(root.valp.val) return root; 28 | else if(root.val>q.val) return lowestCommonAncestor(root.left, p, q); 29 | else return lowestCommonAncestor(root.right, p, q); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /236_lowestCommonAncestor_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.236: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 3 | Author: zhangyixing1007 4 | Idea: dfs, recursion 5 | Time: 9 ms, beat 97.34% 6 | Space: 35.3 MB, beat 82.06% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution{ 20 | TreeNode ancestor = null; 21 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){ 22 | dfs(root,p,q); 23 | return ancestor; 24 | } 25 | 26 | private int dfs(TreeNode root, TreeNode p, TreeNode q){ 27 | if (root==null) return 0; 28 | int left = dfs(root.left,p,q); 29 | int right = dfs(root.right,p,q); 30 | int mid = root == p||root == q? 1:0; 31 | if (left+right+mid>1) ancestor = root; 32 | return left+right+mid>0? 1:0; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /238_productExceptSelf/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.238: https://leetcode.com/problems/product-of-array-except-self/ 3 | Author: zhangyixing1007 4 | Idea: all left*all right 5 | Time: 1 ms, beat 100% 6 | Space: 43.1 MB, beat 96.2% 7 | */ 8 | 9 | class Solution { 10 | public int[] productExceptSelf(int[] nums) { 11 | int[] ans=new int[nums.length]; 12 | ans[0]=1; 13 | for (int i=0; i0; i--){ 17 | ans[i]*=ans[0]; 18 | ans[0]*=nums[i]; 19 | } 20 | return ans; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /240_searchMatrix_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.240: https://leetcode.com/problems/search-a-2d-matrix-ii/ 3 | Author: zhangyixing1007 4 | Idea: start from the leftest and downest corner, if larger than target, move to right, otherwise, up 5 | Time: 7 s, beat 98.74% 6 | Space: 42.7 MB, beat 97.30% 7 | */ 8 | 9 | class Solution { 10 | public boolean searchMatrix(int[][] matrix, int target) { 11 | if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false; 12 | if (matrix[0][0] > target || matrix[matrix.length-1][matrix[0].length-1] < target) return false; 13 | int row = matrix.length-1, col = 0; 14 | while (true){ 15 | if (row < 0 || col >= matrix[0].length) return false; 16 | if (matrix[row][col] > target) row--; 17 | else if (matrix[row][col] < target) col++; 18 | else return true; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /279_numSquares_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.279: https://leetcode.com/problems/perfect-squares/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming 5 | Time: 35 ms, beat 84.33% 6 | Space: 35.4 MB, beat 41.69% 7 | */ 8 | 9 | class Solution { 10 | public int numSquares(int n) { 11 | if (n==0||n==1) return n; 12 | int[] record=new int[n+1]; 13 | for (int i=1; i q=new LinkedList<>(); 13 | q.add(n); 14 | while (!q.isEmpty()){ 15 | int curr=q.poll(); 16 | for (int i=1; i*i<=curr; i++){ 17 | if(i*i==curr) return ans[curr]+1; 18 | if (ans[curr-i*i]==0){ 19 | ans[curr-i*i]=ans[curr]+1; 20 | q.add(curr-i*i); 21 | } 22 | } 23 | } 24 | return 0; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /279_numSquares_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.279: https://leetcode.com/problems/perfect-squares/ 3 | Author: zhangyixing1007 4 | Idea: (brute force) dynamic programming, basis of Version 1 5 | Time: 47 ms, beat 34.17% 6 | Space: 35.5 MB, beat 50.18% 7 | */ 8 | 9 | class Solution { 10 | public int numSquares(int n) { 11 | if (n==0||n==1) return n; 12 | int[] record=new int[n+1]; 13 | for (int i=1; i*i<=n; i++){ 14 | record[i*i]=1; 15 | } 16 | for (int i=2; icoin) 19 | dp[i] = Math.min(dp[i],dp[i - coin]+1); 20 | } 21 | } 22 | return dp[amount]==amount+1? -1:dp[amount]; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /328_oddEvenList/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.328: https://leetcode-cn.com/problems/odd-even-linked-list/ 3 | Author: zhangyixing1007 4 | Idea: simple iteration 5 | Time: 0 ms, beat 100% 6 | Space: 38.8 MB, beat 8.70% 7 | */ 8 | 9 | /** 10 | * Definition for singly-linked list. 11 | * public class ListNode { 12 | * int val; 13 | * ListNode next; 14 | * ListNode() {} 15 | * ListNode(int val) { this.val = val; } 16 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 17 | * } 18 | */ 19 | class Solution { 20 | public ListNode oddEvenList(ListNode head) { 21 | if(head==null||head.next==null) return head; 22 | ListNode l1=head, l2=head.next, head2=l2; 23 | while(true){; 24 | l1.next=l2.next; if(l2.next!=null) l1=l1.next; else break; 25 | l2.next=l1.next; if(l1.next!=null) l2=l2.next; else break; 26 | } 27 | l1.next=head2; 28 | return head; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /337_rob_4/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.337: https://leetcode.com/problems/house-robber-iii 3 | Author: zhangyixing1007 4 | Idea: recursion, not any repeated calculation, much faster than Verison 3 5 | Time: 1 ms, beat 100% 6 | Space: 38.5 MB, beat 66.12% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public int rob(TreeNode root) { 21 | int[] ans = robOrNot(root); 22 | return Math.max(ans[0],ans[1]); 23 | } 24 | 25 | private int[] robOrNot(TreeNode root){ 26 | int[] ans = new int[2]; 27 | if (root == null) return ans; 28 | 29 | int[] left = robOrNot(root.left); 30 | int[] right = robOrNot(root.right); 31 | 32 | ans[0] = Math.max(ans[0],left[1]+right[1]); 33 | ans[0] = Math.max(ans[0],left[1]+right[0]); 34 | ans[0] = Math.max(ans[0],left[0]+right[1]); 35 | ans[0] = Math.max(ans[0],left[0]+right[0]); 36 | ans[1] = left[0]+right[0]+root.val; 37 | 38 | return ans; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /337_rob_5/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.337: https://leetcode.com/problems/house-robber-iii 3 | Author: zhangyixing1007 4 | Idea: recursion, based on Version 4, simply calculation for ans[0]; 5 | Time: 1 ms, beat 100% 6 | Space: 36.4 MB, beat 96.71% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public int rob(TreeNode root) { 21 | int[] ans=robOrNot(root); 22 | return Math.max(ans[0], ans[1]); 23 | } 24 | int[] robOrNot(TreeNode root){ 25 | int[] ans=new int[2]; 26 | if (root==null) return ans; 27 | int[] left=robOrNot(root.left); 28 | int[] right=robOrNot(root.right); 29 | ans[0]=Math.max(left[0],left[1])+Math.max(right[0],right[1]); 30 | ans[1]=root.val+left[0]+right[0]; 31 | return ans; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /338_countBits_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.338: https://leetcode.com/problems/counting-bits 3 | Author: zhangyixing1007 4 | Idea: iteration, dynamic programming 5 | Time: 2 ms, beat 94.45% 6 | Space: 38.1 MB, beat 86.52% 7 | */ 8 | 9 | class Solution { 10 | public int[] countBits(int num) { 11 | int[] ans = new int[num+1]; 12 | int power = 1; 13 | for (int i = 1; i < ans.length; i++){ 14 | if (i==power){ 15 | ans[i] = 1; power*=2; 16 | } 17 | else{ 18 | ans[i] = ans[i-power/2]+1; 19 | } 20 | } 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /338_countBits_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.338: https://leetcode.com/problems/counting-bits 3 | Author: zhangyixing1007 4 | Idea: iteration, /, % 5 | Time: 2 ms, beat 94.32% 6 | Space: 38.8 MB, beat 86.33% 7 | */ 8 | 9 | class Solution { 10 | public int[] countBits(int num) { 11 | int[] ans = new int[num+1]; 12 | for (int i = 0; i < num+1; i++) { 13 | ans[i] = ans[i/2] + i%2; 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /338_countBits_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.338: https://leetcode.com/problems/counting-bits 3 | Author: zhangyixing1007 4 | Idea: iteration, >>, &, faster than Version 2 5 | Time: 1 ms, beat 99.96% 6 | Space: 38.4 MB, beat 86.52% 7 | */ 8 | 9 | class Solution { 10 | public int[] countBits(int num) { 11 | int[] ans = new int[num+1]; 12 | for (int i = 0; i < num+1; i++) { 13 | ans[i] = ans[i>>1] + (i&1); 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /338_countBits_4/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.338: https://leetcode.com/problems/counting-bits 3 | Author: zhangyixing1007 4 | Idea: iteration, cancel the last "1" on the right 5 | Time: 1 ms, beat 99.96% 6 | Space: 38.4 MB, beat 86.52% 7 | */ 8 | 9 | class Solution { 10 | public int[] countBits(int num) { 11 | int[] ans = new int[num+1]; 12 | for (int i = 1; i < num+1; i++) { 13 | ans[i] = ans[i&i-1] + 1; 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /347_topKFrequent_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.347: https://leetcode.com/problems/top-k-frequent-elements/ 3 | Author: zhangyixing1007 4 | Idea: bucket sort 5 | Time: 15 ms, beat 99.49% 6 | Space: 40.9 MB, beat 95.62% 7 | */ 8 | 9 | class Solution { 10 | public List topKFrequent(int[] nums, int k) { 11 | HashMap map=new HashMap<>(); 12 | for (int num:nums){ 13 | map.put(num, map.getOrDefault(num,0)+1); 14 | } 15 | List[] list=new List[nums.length+1]; 16 | for (int key:map.keySet()){ 17 | int count=map.get(key); 18 | if(list[count]==null) 19 | list[count]=new ArrayList<>(); 20 | list[count].add(key); 21 | } 22 | List ans=new ArrayList<>(); 23 | for (int i=list.length-1; i>-1; i--){ 24 | if(list[i]!=null&&list[i].size()<=k){ 25 | ans.addAll(list[i]); 26 | k-=list[i].size(); 27 | } 28 | } 29 | return ans; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /347_topKFrequent_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.347: https://leetcode.com/problems/top-k-frequent-elements/ 3 | Author: zhangyixing1007 4 | Idea: PriorityQueue 5 | Time: 69 ms, beat 30.78% 6 | Space: 40.6 MB, beat 96.12% 7 | */ 8 | 9 | class Solution { 10 | public List topKFrequent(int[] nums, int k) { 11 | HashMap map=new HashMap<>(); 12 | for (int num:nums){ 13 | map.put(num, map.getOrDefault(num,0)+1); 14 | } 15 | Queue q=new PriorityQueue<>((n1,n2)->map.get(n1)-map.get(n2)); 16 | for (int key:map.keySet()){ 17 | q.add(key); 18 | if(q.size()>k) q.poll(); 19 | } 20 | List ans=new ArrayList<>(); 21 | while (!q.isEmpty()){ 22 | ans.add(q.poll()); 23 | } 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /378_kthSmallest_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.378: https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/ 3 | Author: zhangyixing1007 4 | Idea: PriorityQueue 5 | Time: 19 ms, beat 38.51% 6 | Space: 44.7 MB, beat 15.38% 7 | */ 8 | 9 | class Solution { 10 | public int kthSmallest(int[][] matrix, int k) { 11 | if(matrix==null||matrix.length==0||matrix[0].length==0) return 0; 12 | PriorityQueue pq=new PriorityQueue<>((o1, o2)-> o2-o1); 13 | for(int i=0; i=pq.peek()) break; 16 | pq.add(matrix[i][j]); 17 | if(pq.size()>k) pq.poll(); 18 | } 19 | } 20 | return pq.peek(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /378_kthSmallest_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.378: https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/ 3 | Author: zhangyixing1007 4 | Idea: range binary search 5 | Time: 0 ms, beat 100% 6 | Space: 45.1 MB, beat 7.69% 7 | */ 8 | 9 | class Solution{ 10 | public int kthSmallest(int[][] matrix, int k) { 11 | int n = matrix.length - 1; 12 | int left = matrix[0][0], right = matrix[n][n]; 13 | while(left < right){ 14 | int mid = left + (right - left) / 2; 15 | int count = countNotMoreThanMid(matrix, mid, n); 16 | if(count < k){ 17 | left = mid + 1; 18 | }else{ 19 | right = mid; 20 | } 21 | } 22 | return left; 23 | } 24 | 25 | private int countNotMoreThanMid(int[][] matrix, int mid, int n){ 26 | int count = 0; 27 | int x = 0, y = n; 28 | while(x <= n && y >= 0){ 29 | if(matrix[y][x] <= mid){ 30 | count += y + 1; 31 | x++; 32 | }else{ 33 | y--; 34 | } 35 | } 36 | return count; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /394_decodeString_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.394: https://leetcode.com/problems/decode-string/ 3 | Author: zhangyixing1007 4 | Idea: two Stacks 5 | Time: 1 ms, beat 93.55% 6 | Space: 34.5 MB, beat 48.33% 7 | */ 8 | 9 | class Solution { 10 | public String decodeString(String s) { 11 | StringBuilder ans=new StringBuilder(); 12 | Stack muls=new Stack<>(); 13 | Stack strs=new Stack<>(); 14 | int mul=0; 15 | for (char c:s.toCharArray()){ 16 | if(c>='0'&&c<='9'){ 17 | mul=mul*10+Integer.parseInt(c+""); 18 | } else if (c=='['){ 19 | muls.push(mul);mul=0; 20 | strs.push(ans.toString()); 21 | ans=new StringBuilder(); 22 | } else if (c==']'){ 23 | StringBuilder tmp=new StringBuilder(); 24 | mul=muls.pop(); 25 | while(mul>0){ 26 | tmp.append(ans); 27 | mul--; 28 | } 29 | ans=new StringBuilder(strs.pop()+tmp.toString()); 30 | } else { 31 | ans.append(c); 32 | } 33 | } 34 | return ans.toString(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /404_sumOfLeftLeaves_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.404: https://leetcode-cn.com/problems/sum-of-left-leaves/ 3 | Author: zhangyixing1007 4 | Idea: dfs recursion 5 | Time: 0 ms, beat 100% 6 | Space: 36.7 MB, beat 70% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public int sumOfLeftLeaves(TreeNode root) { 21 | dfs(root); 22 | return ans; 23 | } 24 | int ans=0; 25 | void dfs(TreeNode root){ 26 | if(root==null) return; 27 | if(root.left!=null){ 28 | if(root.left.left==null&&root.left.right==null) ans+=root.left.val; 29 | else dfs(root.left); 30 | } 31 | dfs(root.right); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /404_sumOfLeftLeaves_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.404: https://leetcode-cn.com/problems/sum-of-left-leaves/ 3 | Author: zhangyixing1007 4 | Idea: dfs recursion + signal 5 | Time: 0 ms, beat 100% 6 | Space: 36.9 MB, beat 46.64% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public int sumOfLeftLeaves(TreeNode root) { 21 | dfs(root, false); 22 | return ans; 23 | } 24 | int ans=0; 25 | void dfs(TreeNode root, boolean isLeftNode){ 26 | if(root==null) return; 27 | if(isLeftNode && root.left==null && root.right==null){ 28 | ans+=root.val; 29 | }else{ 30 | dfs(root.left, true); 31 | dfs(root.right, false); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /404_sumOfLeftLeaves_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.404: https://leetcode-cn.com/problems/sum-of-left-leaves/ 3 | Author: zhangyixing1007 4 | Idea: bfs+Queue+Pair 5 | Time: 1 ms, beat 9.49% 6 | Space: 37.2 MB, beat 5.27% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public int sumOfLeftLeaves(TreeNode root) { 21 | if(root==null) return 0; 22 | Queue> q=new LinkedList<>(); 23 | q.add(new Pair(root,false)); 24 | int ans=0; 25 | while(!q.isEmpty()){ 26 | Pair curr=q.poll(); 27 | TreeNode key=curr.getKey(); 28 | Boolean value=curr.getValue(); 29 | if(value && key.left==null && key.right==null){ 30 | ans+=key.val; 31 | }else{ 32 | if(key.left!=null) q.add(new Pair(key.left,true)); 33 | if(key.right!=null) q.add(new Pair(key.right,false)); 34 | } 35 | } 36 | return ans; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /406_reconstructQueue/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.406: https://leetcode.com/problems/queue-reconstruction-by-height/ 3 | Author: zhangyixing1007 4 | Idea: sort by defined way, and insert from high to low 5 | Time: 8 ms, beat 89.28% 6 | Space: 38.9 MB, beat 92.45% 7 | */ 8 | 9 | class Solution { 10 | public int[][] reconstructQueue(int[][] people) { 11 | Arrays.sort(people,(o1,o2) -> o1[0] == o2[0]? o1[1] - o2[1] : o2[0] - o1[0] ); 12 | List list = new ArrayList<>(); 13 | for (int i = 0; i < people.length; i++){ 14 | list.add(people[i][1],people[i]); 15 | } 16 | return list.toArray(new int[list.size()][2]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /416_canPartition/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.416: https://leetcode.com/problems/partition-equal-subset-sum/ 3 | Author: zhangyixing1007 4 | Idea: similar as 494 Version 2 5 | Time: 4 ms, beat 96.57% 6 | Space: 35.7 MB, beat 92.15% 7 | */ 8 | 9 | class Solution { 10 | public boolean canPartition(int[] nums) { 11 | int sum = 0; 12 | for (int i = 0; i < nums.length; i++){ 13 | sum += nums[i]; 14 | } 15 | if (sum%2 == 1) return false; 16 | sum = sum/2; 17 | int[] dp = new int[sum+1]; 18 | dp[0] = 1; 19 | for (int num : nums){ 20 | for (int i = sum; i >= num; i--){ 21 | dp[i] += dp[i-num]; 22 | } 23 | if (dp[sum] != 0 ) return true; 24 | } 25 | return nums.length==0; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /437_pathSum/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.437: https://leetcode.com/problems/path-sum-iii/ 3 | Author: zhangyixing1007 4 | Idea: tree = root+left-subtree+right-subtree 5 | Time: 19 ms, beat 58.67% 6 | Space: 39.8MB, beat 60.16% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | class Solution { 19 | public int pathSum(TreeNode root, int sum) { 20 | if (root==null) return 0; 21 | return p(root,sum)+pathSum(root.left,sum)+pathSum(root.right,sum); 22 | } 23 | int p(TreeNode root, int sum){ 24 | if (root==null) return 0; 25 | int ans = 0; 26 | if (sum==root.val) ans++; 27 | ans+=p(root.left, sum-root.val); 28 | ans+=p(root.right, sum-root.val); 29 | return ans; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /448_findDisappearedNumbers/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.448: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 3 | Author: zhangyixing1007 4 | Idea: put nums[i] to where it should be and then count 5 | Time: 7 ms, beat 98.42% 6 | Space: 47.3MB, beat 95.66% 7 | */ 8 | 9 | class Solution { 10 | public List findDisappearedNumbers(int[] nums) { 11 | for (int i=0; i list = new ArrayList<>(); 18 | for (int i=0; i map=new HashMap<>(); 12 | int ans=0; 13 | for(int a:A){ 14 | for(int b:B){ 15 | map.put(a+b, map.getOrDefault(a+b, 0)+1); 16 | } 17 | } 18 | for(int c:C){ 19 | for(int d:D){ 20 | ans+=map.getOrDefault(-c-d, 0); 21 | } 22 | } 23 | return ans; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /461_hammingDistance/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.461: https://leetcode.com/problems/hamming-distance/ 3 | Author: zhangyixing1007 4 | Idea: make good use of %, / or >>=1 5 | Time: 1 ms, beat 96.69% 6 | Space: 33.4 MB, beat 77.38% 7 | */ 8 | 9 | class Solution { 10 | public int hammingDistance(int x, int y) { 11 | int ans=0; 12 | while(x!=0||y!=0){ 13 | ans+=(x%2)^(y%2); 14 | x>>=1;y>>=1; 15 | } 16 | return ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /494_findTargetSumWays_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.494: https://leetcode.com/problems/target-sum/ 3 | Author: zhangyixing1007 4 | Idea: recursion, very simple 5 | Time: 498 ms, beat 35.72% 6 | Space: 34.7 MB, beat 84.86% 7 | */ 8 | 9 | class Solution { 10 | public int findTargetSumWays(int[] nums, int S) { 11 | return helper(nums, S, 0); 12 | } 13 | 14 | private int helper(int[] nums, int S, int start){ 15 | if (start == nums.length) 16 | return S == 0? 1:0; 17 | return helper(nums, S + nums[start], start+1) + helper(nums, S - nums[start], start+1); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /494_findTargetSumWays_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.494: https://leetcode.com/problems/target-sum/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming, similar to Problem 416 5 | Time: 2 ms, beat 99.88% 6 | Space: 34.3 MB, beat 85.80% 7 | */ 8 | 9 | class Solution { 10 | public int findTargetSumWays(int[] nums, int S) { 11 | int sum=0; 12 | for(int num:nums) sum+=num; 13 | if (sum=num; i--){ 19 | dp[i]+=dp[i-num]; 20 | } 21 | } 22 | return dp[target]; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /494_findTargetSumWays_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.494: https://leetcode.com/problems/target-sum/ 3 | Author: zhangyixing1007 4 | Idea: brute force 5 | Time: over time 6 | Space: unknown 7 | */ 8 | 9 | class Solution { 10 | public int findTargetSumWays(int[] nums, int S) { 11 | int ans = 0; 12 | for (int i = 0; i < 1<>1; 22 | } 23 | if (sum == S) ans++; 24 | } 25 | return ans; 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /538_convertBST/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.538: https://leetcode.com/problems/convert-bst-to-greater-tree/ 3 | Author: zhangyixing1007 4 | Idea: dfs, right-root-left, one shared int 5 | Time: 1 ms, beat 100% 6 | Space: 37.8MB, beat 96.85% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public TreeNode convertBST(TreeNode root) { 21 | dfs(root); 22 | return root; 23 | } 24 | int sum = 0; 25 | void dfs(TreeNode root){ 26 | if (root==null) return; 27 | dfs(root.right); 28 | root.val+=sum; 29 | sum=root.val; 30 | dfs(root.left); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /543_diameterOfBinaryTree/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.543: https://leetcode.com/problems/diameter-of-binary-tree/ 3 | Author: zhangyixing1007 4 | Idea: dfs to calculate length of node and compare max diameter each time 5 | Time: 1 ms, beat 99.85% 6 | Space: 37.2MB, beat 75.63% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public int diameterOfBinaryTree(TreeNode root) { 21 | if (root==null) return 0; 22 | len(root); 23 | return max; 24 | } 25 | 26 | private int max = 0; 27 | 28 | private int len(TreeNode root) { 29 | if (root == null) return 0; 30 | int l = len(root.left); 31 | int r = len(root.right); 32 | max = Math.max(max,l+r); 33 | return Math.max(l,r)+1; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /5486_minTime/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.5406: https://leetcode-cn.com/problems/minimum-time-to-collect-all-apples-in-a-tree/ 3 | Author: zhangyixing1007 4 | Idea: loop from larger nodes to smaller nodes 5 | Time: 3 ms, beat 100% 6 | Space: 74.1 MB, beat 100% 7 | */ 8 | 9 | class Solution { 10 | public int minTime(int n, int[][] edges, List hasApple) { 11 | int[] back=new int[n]; 12 | int ans=0; 13 | for(int i=0; i0; i--){ 17 | if(hasApple.get(i)) { 18 | ans++; 19 | hasApple.set(back[i],true); 20 | } 21 | } 22 | return ans*2; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /5515_ParkingSystem/ParkingSystem.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.5515: https://leetcode-cn.com/problems/design-parking-system/ 3 | Author: zhangyixing1007 4 | Idea: simple syntax 5 | Time: 14 ms 6 | Space: 39.7 MB 7 | */ 8 | 9 | class ParkingSystem { 10 | 11 | int big, medium, small; 12 | public ParkingSystem(int big, int medium, int small) { 13 | this.big=big; 14 | this.medium=medium; 15 | this.small=small; 16 | } 17 | 18 | public boolean addCar(int carType) { 19 | if(carType==1) { 20 | big--; 21 | return big>=0; 22 | }else if(carType==2) { 23 | medium--; 24 | return medium>=0; 25 | }else { 26 | small--; 27 | return small>=0; 28 | } 29 | } 30 | } 31 | 32 | /** 33 | * Your ParkingSystem object will be instantiated and called as such: 34 | * ParkingSystem obj = new ParkingSystem(big, medium, small); 35 | * boolean param_1 = obj.addCar(carType); 36 | */ 37 | -------------------------------------------------------------------------------- /5518_restoreMatrix/Solution.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | LeetCode Problem No.5518: https://leetcode-cn.com/problems/find-valid-matrix-given-row-and-column-sums/ 4 | Author: zhangyixing1007 5 | Idea: Math.min(rowSum[i], colSum[j]) 6 | Time: 10 ms 7 | Space: 46.6 MB 8 | */ 9 | 10 | class Solution { 11 | public int[][] restoreMatrix(int[] rowSum, int[] colSum) { 12 | int n=rowSum.length; 13 | int m=colSum.length; 14 | int[][] ans=new int[n][m]; 15 | for(int i=0; i=nums.length) return nums.length; 14 | for(int i=1; i=i) 16 | return i; 17 | } 18 | return -1; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /5535_maxDepth/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.5535: https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/ 3 | Author: zhangyixing1007 4 | Idea: +1-1 5 | Time: 1 ms 6 | Space: 36.8 MB 7 | */ 8 | 9 | class Solution { 10 | public int maxDepth(String s) { 11 | int ans=0, curr=0; 12 | for(char c:s.toCharArray()){ 13 | if(c=='(') curr++; 14 | else if(c==')') curr--; 15 | ans=Math.max(ans, curr); 16 | } 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /5536_maximalNetworkRank/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.5536: https://leetcode-cn.com/problems/maximal-network-rank/ 3 | Author: zhangyixing1007 4 | Idea: A+B(-1) 5 | Time: 39 ms 6 | Space: 39.4 MB 7 | */ 8 | 9 | class Solution { 10 | public int maximalNetworkRank(int n, int[][] roads) { 11 | List[] list=new ArrayList[n]; 12 | for(int i=0; i(); 14 | } 15 | for(int[] road:roads){ 16 | list[road[0]].add(road[1]); 17 | list[road[1]].add(road[0]); 18 | } 19 | int ans=0, curr=0; 20 | for(int i=0; i map=new HashMap<>(); 12 | map.put(0,1); 13 | int ans=0, sum=0; 14 | for (int num:nums){ 15 | sum+=num; 16 | ans+=map.getOrDefault(sum-k,0); 17 | map.put(sum, map.getOrDefault(sum,0)+1); 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /581_findUnsortedSubarray/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.581: https://leetcode.com/problems/shortest-unsorted-continuous-subarray/ 3 | Author: zhangyixing1007 4 | Idea: first find the min and max of unsorted subarray, then find the left and right boundaries 5 | Time: 4 ms, beat 94.61% 6 | Space: 39.3 MB, beat 97.75% 7 | */ 8 | 9 | class Solution { 10 | public int findUnsortedSubarray(int[] nums) { 11 | if (nums.length<2) return 0; 12 | boolean flag = false; 13 | int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; 14 | for (int i = 1; i < nums.length; i++) 15 | { 16 | if (nums[i] 0; i--) 21 | { 22 | if (nums[i]=max) r--; 28 | return r-l+1; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /617_mergeTrees/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.617: https://leetcode.com/problems/merge-two-binary-trees/ 3 | Author: zhangyixing1007 4 | Idea: recursion, tree=root+left-subtree+right-subtree 5 | Time: 1 ms, beat 99.91% 6 | Space: 40.3 MB, beat 95.37% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode(int x) { val = x; } 16 | * } 17 | */ 18 | class Solution { 19 | public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { 20 | if(t1==null&&t2==null) return null; 21 | if(t1==null) return t2; 22 | if(t2==null) return t1; 23 | t1.val+=t2.val; 24 | t1.left=mergeTrees(t1.left,t2.left); 25 | t1.right=mergeTrees(t1.right, t2.right); 26 | return t1; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /621_leastInterval/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.621: https://leetcode.com/problems/task-scheduler/ 3 | Author: zhangyixing1007 4 | Idea: put most frequent one first 5 | Time: 3 ms, beat 88.10% 6 | Space: 38.2 MB, beat 93.89% 7 | */ 8 | 9 | class Solution { 10 | public int leastInterval(char[] tasks, int n) { 11 | int[] record=new int[26]; 12 | for(char c:tasks) record[c-'A']++; 13 | Arrays.sort(record); 14 | int ans=(record[25]-1)*(n+1)+1; 15 | int i=24; 16 | while(record[i--]==record[25]) ans++; 17 | return Math.max(ans,tasks.length); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /647_countSubstrings_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.647: https://leetcode.com/problems/palindromic-substrings/ 3 | Author: zhangyixing1007 4 | Idea: trivial trick in panlindrome, insert '#' 5 | Time: 3 ms, beat 84.48% 6 | Space: 34.5 MB, beat 91.44% 7 | */ 8 | 9 | class Solution { 10 | public int countSubstrings(String s) { 11 | if(s == null || s.length()==0){ 12 | return 0; 13 | } 14 | int len = s.length(); 15 | char[] cs = new char[2*len+1]; 16 | Arrays.fill(cs,'#'); 17 | for (int i = 0; i< len; i++){ 18 | cs[2*i+1] = s.charAt(i); 19 | } 20 | int ans = len; 21 | for (int i = 1; i <= cs.length-1; i++){ 22 | int d = 1; 23 | if (i%2 == 1) d++; 24 | while (i-d>=0&&i+d= 0 && right < s.length() && s.charAt(left--) == s.charAt(right++)){ 22 | count++; 23 | } 24 | return count; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /647_countSubstrings_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.647: https://leetcode.com/problems/palindromic-substrings/ 3 | Author: zhangyixing1007 4 | Idea: subfunction for separate cases+recursion 5 | Time: 3 ms, beat 84.48% 6 | Space: 34.6 MB, beat 53.46% 7 | */ 8 | 9 | class Solution { 10 | public int countSubstrings(String s) { 11 | char[] ch=s.toCharArray(); 12 | int sum=0; 13 | for(int i=0; i=ch.length) return 0; 22 | if(ch[l]==ch[r]) return 1+isPalindrome(ch, l-1, r+1); 23 | return 0; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /695_maxAreaOfIsland_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.695: https://leetcode-cn.com/problems/max-area-of-island/ 3 | Author: zhangyixing1007 4 | Idea: dfs 5 | Time: 2 ms, beat 100% 6 | Space: 40.1 MB, beat 91.67% 7 | */ 8 | 9 | class Solution { 10 | public int maxAreaOfIsland(int[][] grid) { 11 | if(grid==null||grid.length==0||grid[0].length==0) return 0; 12 | int n=grid.length; 13 | int m=grid[0].length; 14 | int ans=0; 15 | this.grid=grid; 16 | for(int i=0; i=grid.length||j<0||j>=grid[0].length||grid[i][j]==0)return 0; 30 | grid[i][j]=0; 31 | int ans=1; 32 | ans+=dfs(i-1,j); 33 | ans+=dfs(i+1,j); 34 | ans+=dfs(i,j-1); 35 | ans+=dfs(i,j+1); 36 | return ans; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /701_insertIntoBST_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.701: https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/ 3 | Author: zhangyixing1007 4 | Idea: recursion 5 | Time: 0 ms, beat 100% 6 | Space: 39.4 MB, beat 65.69% 7 | */ 8 | 9 | /** 10 | * Definition for a binary tree node. 11 | * public class TreeNode { 12 | * int val; 13 | * TreeNode left; 14 | * TreeNode right; 15 | * TreeNode() {} 16 | * TreeNode(int val) { this.val = val; } 17 | * TreeNode(int val, TreeNode left, TreeNode right) { 18 | * this.val = val; 19 | * this.left = left; 20 | * this.right = right; 21 | * } 22 | * } 23 | */ 24 | class Solution { 25 | public TreeNode insertIntoBST(TreeNode root, int val) { 26 | if(root==null) return new TreeNode(val); 27 | if(root.val= T[i+d]){d++;} 15 | if ((i+d) -1; i--){ 13 | int d = 1; 14 | if (T[i]=T[i+d]&&ans[i+d]!=0){ 17 | d += ans[i+d]; 18 | } 19 | if (i+d < T.length) 20 | ans[i] = ans[i+d]==0&&T[i]>=T[i+d]? 0:d; 21 | } 22 | } 23 | return ans; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /739_dailyTemperatures_3/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.739: https://leetcode.com/problems/daily-temperatures/ 3 | Author: zhangyixing1007 4 | Idea: start from the right, skip smaller elements, clearer than Version 2 5 | Time: 5 ms, beat 98.27% 6 | Space: 42.5 MB, beat 94.09% 7 | */ 8 | 9 | class Solution { 10 | public int[] dailyTemperatures(int[] T) { 11 | int[] ans = new int[T.length]; 12 | for (int i = T.length-2; i > -1; i--){ 13 | int j = 0; 14 | for (j = i+1; j gems=new HashSet<>(); 12 | int ans=0; 13 | for(char c:J.toCharArray()){ 14 | gems.add(c); 15 | } 16 | for(char c:S.toCharArray()){ 17 | if(gems.contains(c)) ans++; 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /898_subarrayBitwiseORs/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.898: https://leetcode.com/problems/bitwise-ors-of-subarrays/ 3 | Author: zhangyixing1007 4 | Idea: 3 HashSet 5 | Time: 500 ms, beat 28.86% 6 | Space: 67.3MB, beat 100% 7 | */ 8 | 9 | class Solution { 10 | public int subarrayBitwiseORs(int[] nums) { 11 | Set ans=new HashSet<>(); 12 | Set set=new HashSet<>(); 13 | for(int num:nums){ 14 | Set set2=new HashSet<>(); 15 | for(int i:set) set2.add(i|num); 16 | set2.add(num); 17 | set=set2; 18 | for(int i:set) ans.add(i); 19 | } 20 | return ans.size(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /945_minIncrementForUnique/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.945: https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique/ 3 | Author: zhangyixing1007 4 | Idea: +1-1 5 | Time: 10 ms, beat 85.26% 6 | Space: 43.9 MB, beat 100% 7 | */ 8 | 9 | class Solution { 10 | public int minIncrementForUnique(int[] A) { 11 | int[] count=new int[80000]; 12 | for(int i:A) count[i]++; 13 | int ans=0, taken=0; 14 | for(int i=0; i<80000; i++){ 15 | if(count[i]>1){ 16 | taken+=count[i]-1; 17 | ans-=(count[i]-1)*i; 18 | }else if(taken>0&&count[i]==0){ 19 | taken--; ans+=i; 20 | } 21 | } 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /983_mincostTickets_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.983: https://leetcode-cn.com/problems/minimum-cost-for-tickets/ 3 | Author: zhangyixing1007 4 | Idea: backtrack, brute force 5 | Time: timeout 6 | Space: unknown 7 | */ 8 | 9 | class Solution { 10 | public int mincostTickets(int[] days, int[] costs) { 11 | this.days=days; 12 | this.costs=costs; 13 | return backtrack(0,-1,0); 14 | } 15 | 16 | int[] days; 17 | int[] costs; 18 | 19 | int backtrack(int index, int lastDay, int money){ 20 | if(index==days.length) return money; 21 | if(days[index]<=lastDay){ 22 | return backtrack(index+1,lastDay,money); 23 | } 24 | if(days[days.length-1]<=lastDay) return money; 25 | //3 choices, we go through them 26 | int cost0=backtrack(index,days[index],money+costs[0]); 27 | int cost1=backtrack(index,days[index]+6,money+costs[1]); 28 | int cost2=backtrack(index,days[index]+29,money+costs[2]); 29 | return Math.min(Math.min(cost0,cost1),cost2); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /983_mincostTickets_2/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem No.983: https://leetcode-cn.com/problems/minimum-cost-for-tickets/ 3 | Author: zhangyixing1007 4 | Idea: dp 5 | Time: 1 ms, beat 97.22% 6 | Space: 38.2 MB, beat 100% 7 | */ 8 | 9 | class Solution { 10 | public int mincostTickets(int[] days, int[] costs) { 11 | this.days=days; 12 | this.costs=costs; 13 | memo=new Integer[days.length]; 14 | return dp(0); 15 | } 16 | 17 | int[] durations=new int[]{1,7,30}; 18 | Integer[] memo; 19 | int[] days; 20 | int[] costs; 21 | 22 | int dp(int i){ 23 | if(i>=memo.length) return 0; 24 | if(memo[i]!=null) return memo[i]; 25 | memo[i]=Integer.MAX_VALUE; 26 | for(int k=0; k<3; k++){ 27 | int j=i; 28 | while(j=k) { 22 | ans+=n-i+1; 23 | break; 24 | } 25 | } 26 | } 27 | return ans; 28 | } 29 | 30 | public static void main(String[] args){ 31 | Solution s=new Solution(); 32 | System.out.println(s.solution(3,5,new int[]{2,3,5})); 33 | System.out.println(s.solution(4,5,new int[]{2,3,4,5})); 34 | System.out.println(s.solution(6,5,new int[]{1,2,3,4,5,6})); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /JZOffer_13_movingCount/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | JZOffer Problem No.13: https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/submissions/ 3 | Author: zhangyixing1007 4 | Idea: dfs 5 | Time: 1 ms, beat 85.32% 6 | Space: 35.3 MB, beat 99.51% 7 | */ 8 | 9 | class Solution { 10 | public int movingCount(int m, int n, int k) { 11 | visited=new boolean[m][n]; 12 | this.m=m; this.n=n; 13 | return dfs(0, 0, k); 14 | } 15 | int dfs(int x, int y, int k){ 16 | if(!canVisit(x, y, k) || visited[x][y]) return 0; 17 | int ans=1; 18 | visited[x][y]=true; 19 | ans+=dfs(x+1, y, k); 20 | ans+=dfs(x, y+1, k); 21 | return ans; 22 | } 23 | boolean[][] visited; 24 | int m, n; 25 | boolean canVisit(int x, int y, int k){ 26 | if(x<0 || x>=m || y<0 || y>=n) return false; 27 | int sum=0; 28 | while(x!=0){ 29 | sum+=x%10; 30 | x/=10; 31 | } 32 | while(y!=0){ 33 | sum+=y%10; 34 | y/=10; 35 | } 36 | return sum<=k; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LCP_17_minimumOperations_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem LCP 17: https://leetcode-cn.com/problems/UlBDOe/ 3 | Author: zhangyixing1007 4 | Idea: dynamic programming, 3 states 5 | Time: 19 ms, beat 83.90% 6 | Space: 39.3 MB, beat 94.76% 7 | */ 8 | 9 | class Solution { 10 | public int minimumOperations(String leaves) { 11 | int state0=leaves.charAt(0)=='y'? 1:0; 12 | int state1=leaves.length(); 13 | int state2=leaves.length(); 14 | for(int i=1; i((o1,o2)->o2-o1); 13 | q=new LinkedList(); 14 | } 15 | 16 | PriorityQueue pq; 17 | Queue q; 18 | 19 | public int max_value() { 20 | if(pq.isEmpty()) return -1; 21 | return pq.peek(); 22 | } 23 | 24 | public void push_back(int value) { 25 | pq.add(value); 26 | q.add(value); 27 | } 28 | 29 | public int pop_front() { 30 | if(pq.isEmpty()) return -1; 31 | int ans=q.poll(); 32 | pq.remove(ans); 33 | return ans; 34 | } 35 | } 36 | 37 | /** 38 | * Your MaxQueue object will be instantiated and called as such: 39 | * MaxQueue obj = new MaxQueue(); 40 | * int param_1 = obj.max_value(); 41 | * obj.push_back(value); 42 | * int param_3 = obj.pop_front(); 43 | */ 44 | -------------------------------------------------------------------------------- /itv64_sumNums_1/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | LeetCode Problem itv64: https://leetcode-cn.com/problems/qiu-12n-lcof/ 3 | Author: zhangyixing1007 4 | Idea: && 5 | Time: 1 ms, beat 66.05% 6 | Space: 36.7 MB, beat 100% 7 | */ 8 | 9 | class Solution { 10 | public int sumNums(int n) { 11 | boolean flag=n>0 && (n+=sumNums(n-1))>0; 12 | return n; 13 | } 14 | } 15 | --------------------------------------------------------------------------------