├── README.md └── src └── com └── cannedmitang └── hot100 ├── BFS.java ├── DFS.java ├── ListNode.java ├── MonotoneStack.java ├── Node.java ├── TreeNode.java ├── _0001_TwoSum.java ├── _0002_AddTwoNumbers.java ├── _0003_LengthOfLongestSubstring.java ├── _0004_FindMedianSortedArrays.java ├── _0005_LongestPalindrome.java ├── _0011_MaxArea.java ├── _0015_ThreeSum.java ├── _0017_LetterCombinations.java ├── _0019_RemoveNthFromEnd.java ├── _0020_IsValid.java ├── _0021_MergeTwoLists.java ├── _0022_GenerateParenthesis.java ├── _0023_MergeKLists.java ├── _0024_SwapNodesInPairs.java ├── _0025_ReverseNodesInKGroup.java ├── _0032_LongestValidParentheses.java ├── _0033_Search.java ├── _0034_FindFirstAndLastPositionOfElementInSortedArray.java ├── _0035_SearchInsertPosition.java ├── _0039_CombinationSum.java ├── _0041_FirstMissingPositive.java ├── _0042_Trap.java ├── _0045_JumpGameII.java ├── _0046_Permute.java ├── _0048_Rotate.java ├── _0049_GroupAnagrams.java ├── _0051_NQueens.java ├── _0053_MaxSubArray.java ├── _0054_spiralMatrix.java ├── _0055_CanJump.java ├── _0056_Merge.java ├── _0062_UniquePaths.java ├── _0064_MinPathSum.java ├── _0070_ClimbStairs.java ├── _0072_MinDistance.java ├── _0073_SetMatrixZeroes.java ├── _0074_SearchA2dMatrix.java ├── _0076_MinWindow.java ├── _0078_Subsets.java ├── _0079_Exist.java ├── _0084_LargestRectangleArea.java ├── _0094_InorderTraversal.java ├── _0098_IsValidBST.java ├── _0101_IsSymmetric.java ├── _0102_LevelOrder.java ├── _0104_MaxDepth.java ├── _0105_BuildTree.java ├── _0108_ConvertSortedArrayToBinary.java ├── _0114_Flatten.java ├── _0118_Generate.java ├── _0121_MaxProfit.java ├── _0124_MaxPathSum.java ├── _0128_LongestConsecutive.java ├── _0131_PalindromePartitioning.java ├── _0138_CopyListWithRandomPointer.java ├── _0139_WordBreak.java ├── _0141_HasCycle.java ├── _0142_DetectCycle.java ├── _0146_LRUCache.java ├── _0148_SortList.java ├── _0152_MaxProduct.java ├── _0153_FindMinimumInRotatedSortedArray.java ├── _0155_MinStack.java ├── _0160_GetIntersectionNode.java ├── _0189_rotateArray.java ├── _0198_Rob.java ├── _0199_BinaryTreeRightSideView.java ├── _0200_NumIslands.java ├── _0207_CanFinish.java ├── _0208_Trie.java ├── _0215_FindKthLargest.java ├── _0226_InvertTree.java ├── _0230_KthSmallestElementInABst.java ├── _0234_IsPalindrome.java ├── _0236_LowestCommonAncestor.java ├── _0238_ProductExceptSelf.java ├── _0239_MaxSlidingWindow.java ├── _0240_SearchMatrix.java ├── _0279_NumSquares.java ├── _0283_MoveZeroes.java ├── _0295_FindMedianFromDataStream.java ├── _0300_LengthOfLIS.java ├── _0322_CoinChange.java ├── _0347_TopKFrequent.java ├── _0394_DecodeString.java ├── _0416_CanPartition.java ├── _0437_PathSum.java ├── _0438_FindAnagrams.java ├── _0543_DiameterOfBinary.java ├── _0560_SubarraySum.java ├── _0739_DailyTemperatures.java ├── _0763_PartitionLabels.java ├── _0994_RottingOranges.java ├── _1143_LongestCommonSubsequence.java ├── _206_ReverseLinkedList.java ├── backTrackingTemplate.java └── binarySearchTemplate.java /README.md: -------------------------------------------------------------------------------- 1 | # leetcode com.cannedmitang.hot100 2 | Code with bilibili tv show : https://space.bilibili.com/444731546/channel/collectiondetail -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/BFS.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayDeque; 4 | import java.util.HashMap; 5 | import java.util.Queue; 6 | 7 | /** 8 | * @author : loso 9 | * @version : v1.0 10 | * @date : Created in 2024/5/18 22:15 11 | * @description : 12 | * @modified By : loso 13 | */ 14 | public class BFS { 15 | 16 | private void bfs(Node node) { 17 | Queue queue = new ArrayDeque<>(); 18 | HashMap distance = new HashMap<>(); 19 | // step 1: 初始化 20 | // 把初始化节点放入,有多个就都放入 21 | // 标记初始距离为0,记录在distance中 22 | // distance的作用:1. 作为记录是否访问;2. 记录到起点的距离 23 | queue.offer(node); 24 | distance.put(node, 0); 25 | 26 | // step 2: 循环访问队列 27 | // while 循环每次pop 队列中的一个点出来 28 | while (!queue.isEmpty()) { 29 | node = queue.poll(); 30 | // step 3: 拓展相邻节点 31 | // pop出的节点的相邻节点,加入队列并在distance中存储距离 32 | for (Node neighbor : node.getNeighbors()) { 33 | if (distance.containsKey(neighbor)) { 34 | continue; 35 | } 36 | distance.put(neighbor, distance.get(node) + 1); 37 | queue.offer(neighbor); 38 | } 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/DFS.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.List; 4 | import java.util.Set; 5 | 6 | /** 7 | * @author : loso 8 | * @version : v1.0 9 | * @date : Created in 2024/5/18 23:14 10 | * @description : 11 | * @modified By : loso 12 | */ 13 | 14 | @SuppressWarnings("all") 15 | public class DFS { 16 | 17 | private void dfs(int n, Set visited, List path) { 18 | if (path.size() == n) { 19 | //do something 20 | return; 21 | } 22 | for (int i = 1; i <= n; i++) { 23 | if (!visited.contains(i)) { 24 | path.add(i); 25 | visited.add(i); 26 | dfs(n, visited, path); 27 | path.remove(path.size() - 1); 28 | visited.remove(i); 29 | } 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/ListNode.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | /** 4 | * @author : loso 5 | * @version : 6 | * @date : Created in 2021/11/5 9:47 上午 7 | * @description : 8 | * @modified By : loso 9 | */ 10 | public class ListNode { 11 | int val; 12 | ListNode next; 13 | ListNode() {} 14 | ListNode(int val) { this.val = val; } 15 | ListNode(int val, ListNode next) { this.val = val; this.next = next; } 16 | } 17 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/MonotoneStack.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class MonotoneStack { 7 | 8 | public static void main(String[] args) { 9 | int[] test = new int[]{2, 4, 5, 3, 6, 7, 10, 1}; 10 | monotoneStack(test); 11 | } 12 | 13 | public static void monotoneStack(int[] nums) { 14 | Deque stack = new LinkedList<>(); 15 | for (int i = 0; i < nums.length; i++) { 16 | while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) { 17 | int currIndex = stack.pop(); 18 | System.out.println(nums[currIndex]); 19 | } 20 | stack.push(i); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/Node.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author : loso 8 | * @version : v1.0 9 | * @date : Created in 2023/11/12 21:53 10 | * @description : 11 | * @modified By : loso 12 | */ 13 | public class Node { 14 | int val; 15 | Node next; 16 | Node random; 17 | 18 | public List getNeighbors(){ 19 | return new ArrayList<>(); 20 | } 21 | public Node(int val) { 22 | this.val = val; 23 | this.next = null; 24 | this.random = null; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/TreeNode.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | /** 4 | * @author : loso 5 | * @version : 6 | * @date : Created in 2021/11/12 2:45 下午 7 | * @description : 8 | * @modified By : loso 9 | */ 10 | public class TreeNode { 11 | 12 | public int val; 13 | public TreeNode left; 14 | public TreeNode right; 15 | public TreeNode() {} 16 | public TreeNode(int val) { this.val = val; } 17 | public TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0001_TwoSum.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.HashMap; 4 | 5 | public class _0001_TwoSum { 6 | 7 | public int[] twoSum(int[] nums, int target) { 8 | HashMap map = new HashMap<>(); 9 | for (int i = 0; i < nums.length; i++) { 10 | int choose = nums[i]; 11 | int diff = target - choose; 12 | if (map.containsKey(diff)) { 13 | return new int[]{map.get(diff), i}; 14 | } 15 | map.put(choose, i); 16 | } 17 | return null; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0002_AddTwoNumbers.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0002_AddTwoNumbers { 4 | 5 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 6 | ListNode root = new ListNode (); 7 | ListNode curr = root; 8 | int adds = 0; 9 | while(l1 != null || l2 != null || adds != 0) { 10 | int currV = (l1 == null ? 0 : l1.val) + 11 | (l2 == null ? 0 : l2.val) + adds; 12 | if (currV > 9) { 13 | currV -= 10; 14 | adds = 1; 15 | } else { 16 | adds = 0; 17 | } 18 | curr.next = new ListNode(currV); 19 | curr = curr.next; 20 | if (l1 != null){ 21 | l1 = l1.next; 22 | } 23 | if (l2 != null) { 24 | l2 = l2.next; 25 | } 26 | } 27 | return root.next; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0003_LengthOfLongestSubstring.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.HashMap; 4 | 5 | public class _0003_LengthOfLongestSubstring { 6 | 7 | public int lengthOfLongestSubstring(String s) { 8 | HashMap keyIndex = new HashMap<>(); 9 | int length = s.length(); 10 | int maxLength = 0; 11 | int lashIndex = 0; 12 | for (int i = 0; i < length ; i++) { 13 | final char c = s.charAt(i); 14 | if (keyIndex.containsKey(c)) { 15 | lashIndex = Math.max(keyIndex.get(c) + 1, lashIndex); 16 | } 17 | keyIndex.put(c, i); 18 | int currLength = i - lashIndex + 1; 19 | if (maxLength < currLength) { 20 | maxLength = currLength; 21 | } 22 | } 23 | return maxLength; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0004_FindMedianSortedArrays.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0004_FindMedianSortedArrays { 4 | 5 | public double findMedianSortedArrays(int[] nums1, int[] nums2) { 6 | int n1 = nums1.length; 7 | int n2 = nums2.length; 8 | int n = n1 + n2; 9 | if (n%2 == 1) { 10 | return findK(nums1, 0 , nums2, 0, n / 2 + 1); 11 | } else { 12 | return (findK(nums1, 0 , nums2, 0 ,n/2 + 1) + findK(nums1, 0, nums2, 0, n/2))/2; 13 | } 14 | } 15 | 16 | private double findK(int[] nums1, int i1, int[] nums2, int i2, int k) { 17 | if( i1 >= nums1.length) { 18 | return nums2[i2 + k - 1]; 19 | } 20 | if (i2 >= nums2.length) { 21 | return nums1[i1 + k -1]; 22 | } 23 | if (k == 1){ 24 | return Math.min(nums1[i1] , nums2[i2]); 25 | } 26 | int mid = k >> 1; 27 | int move1 = i1 + mid >= nums1.length ? nums1.length - 1 - i1 : mid - 1; 28 | int move2 = i2 + mid >= nums2.length ? nums2.length - 1 - i2 : mid - 1; 29 | if ( nums1[i1 + move1] < nums2[i2 + move2]) { 30 | return findK(nums1, i1 + move1 + 1, nums2, i2, k - (move1 + 1)); 31 | } else { 32 | return findK(nums1, i1, nums2, i2 + move2 + 1, k - (move2 + 1)); 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0005_LongestPalindrome.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0005_LongestPalindrome { 4 | 5 | public String longestPalindrome(String s) { 6 | final char[] chars = s.toCharArray(); 7 | int l = 0; 8 | int r = 0; 9 | for (int i = 0; i < chars.length; i++) { 10 | final int[] len1 = isPalindrome(chars, i ,i); 11 | final int[] len2 = isPalindrome(chars, i , i+1); 12 | int[] len; 13 | if (len2 == null) { 14 | len = len1; 15 | } else { 16 | len = len1[1] - len1[0] > len2[1] - len2[0] ? len1 : len2; 17 | } 18 | if (len[1] - len[0] > r - l) { 19 | r = len[1]; 20 | l = len[0]; 21 | } 22 | } 23 | return s.substring(l , r + 1); 24 | } 25 | 26 | public int[] isPalindrome(char[] chars, int l , int r) { 27 | if (l < 0 || r >= chars.length || chars[l] != chars[r]) { 28 | return null; 29 | } 30 | while (l - 1>= 0 && r + 1< chars.length && chars[l - 1] == chars[r + 1]) { 31 | l--; 32 | r++; 33 | } 34 | return new int[]{l , r}; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0011_MaxArea.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0011_MaxArea { 4 | 5 | public int maxArea(int[] height) { 6 | int l = 0; 7 | int r = height.length - 1; 8 | int res = 0; 9 | int area; 10 | while(l < r) { 11 | area = Math.min(height[l], height[r]) * (r - l); 12 | res = Math.max(area, res); 13 | if (height[l] < height[r]) { 14 | l ++; 15 | } else { 16 | r --; 17 | } 18 | } 19 | return res; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0015_ThreeSum.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class _0015_ThreeSum { 8 | 9 | public List> threeSum(int[] nums) { 10 | int n = nums.length; 11 | List> res = new ArrayList<>(); 12 | Arrays.sort(nums); 13 | for (int i = 0; i < n; i++) { 14 | 15 | if (i > 0 && nums[i] == nums[i-1]) { 16 | continue; 17 | } 18 | 19 | int r = n - 1; 20 | int target = -nums[i]; 21 | for (int l = i + 1; l < r; l++) { 22 | if (l != i + 1 && nums[l] == nums[l-1]) { 23 | continue; 24 | } 25 | while (nums[l] + nums[r] > target && r > l) { 26 | r--; 27 | } 28 | if (l == r) { 29 | break; 30 | } 31 | if (nums[l] + nums[r] == target) { 32 | List list = new ArrayList<>(); 33 | list.add(nums[i]); 34 | list.add(nums[l]); 35 | list.add(nums[r]); 36 | res.add(list); 37 | } 38 | } 39 | } 40 | return res; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0017_LetterCombinations.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class _0017_LetterCombinations { 7 | 8 | String[] m = new String[]{"","","abc","def","ghi","jkl","mon","pqrs","tuv","wxyz"}; 9 | 10 | public List letterCombinations(String digits) { 11 | if(digits.length() == 0) { 12 | return new ArrayList<>(); 13 | } 14 | final char[] chars = digits.toCharArray(); 15 | return process(chars, 0); 16 | } 17 | 18 | List process(char[] chars, int index){ 19 | if ( chars.length == index) { 20 | List line = new ArrayList<>(); 21 | line.add(""); 22 | return line; 23 | } 24 | final int aChar = chars[index] - '0'; 25 | final String s = m[aChar]; 26 | final char[] chars1 = s.toCharArray(); 27 | final List process = process(chars, index + 1); 28 | List res = new ArrayList<>(); 29 | for (int i = 0; i < chars1.length; i++) { 30 | for (int j = 0; j < process.size(); j++) { 31 | res.add(chars1[i] + process.get(j)); 32 | } 33 | } 34 | return res; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0019_RemoveNthFromEnd.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0019_RemoveNthFromEnd { 4 | 5 | public ListNode removeNthFromEnd(ListNode head, int n) { 6 | ListNode root = new ListNode(); 7 | root.next = head; 8 | int count = n + 1; 9 | head = root; 10 | ListNode res = head; 11 | while (head != null) { 12 | if (count > 0) { 13 | count --; 14 | } else { 15 | res = res.next; 16 | } 17 | head = head.next; 18 | } 19 | res.next = res.next.next; 20 | return root.next; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0020_IsValid.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class _0020_IsValid { 7 | 8 | public boolean isValid(String s) { 9 | if ( s.length() % 2 == 1) { 10 | return false; 11 | } 12 | final char[] chars = s.toCharArray(); 13 | Deque stack = new LinkedList<>(); 14 | Character t; 15 | for (char c : chars) { 16 | switch (c) { 17 | case '(': 18 | case '[': 19 | case '{': 20 | stack.addFirst(c);; 21 | break; 22 | case ')': 23 | if (stack.isEmpty()) { 24 | return false; 25 | } 26 | t = stack.pollFirst(); 27 | if (t != '(') { 28 | return false; 29 | } 30 | break; 31 | case ']': 32 | if (stack.isEmpty()) { 33 | return false; 34 | } 35 | t = stack.pollFirst(); 36 | if (t != '[') { 37 | return false; 38 | } 39 | break; 40 | case '}': 41 | if (stack.isEmpty()) { 42 | return false; 43 | } 44 | t = stack.pollFirst(); 45 | if (t != '{') { 46 | return false; 47 | } 48 | break; 49 | } 50 | } 51 | if (!stack.isEmpty()) { 52 | return false; 53 | } 54 | return true; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0021_MergeTwoLists.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0021_MergeTwoLists { 4 | 5 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 6 | if ( l1 == null) { 7 | return l2; 8 | } 9 | if (l2 == null) { 10 | return l1; 11 | } 12 | ListNode root = l1.val < l2.val ? l1 : l2; 13 | ListNode curr = root; 14 | if (l1.val < l2.val) { 15 | l1 = l1.next; 16 | } else { 17 | l2 = l2.next; 18 | } 19 | while (l1 != null && l2 != null) { 20 | if (l1.val < l2.val) { 21 | curr.next = l1; 22 | curr = l1; 23 | l1 = l1.next; 24 | } else { 25 | curr.next = l2; 26 | curr = l2; 27 | l2 = l2.next; 28 | } 29 | } 30 | if (l1 == null) { 31 | curr.next = l2; 32 | } 33 | if (l2 == null) { 34 | curr.next = l1; 35 | } 36 | return root; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0022_GenerateParenthesis.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class _0022_GenerateParenthesis { 7 | 8 | public List generateParenthesis(int n){ 9 | char[] line = new char[n*2]; 10 | process(line, 0, 0, 0, n); 11 | return res; 12 | } 13 | 14 | List res = new ArrayList<>(); 15 | 16 | private void process(char[] line, int index, int leftCount, int rightCount, int n) { 17 | if (index == n*2) { 18 | res.add(new String(line)); 19 | return; 20 | } 21 | if (leftCount == rightCount) { 22 | line[index] = '('; 23 | process(line, index + 1, leftCount + 1, rightCount, n); 24 | return; 25 | } 26 | if (leftCount< n) { 27 | line[index] = '('; 28 | process(line , index + 1, leftCount + 1, rightCount, n); 29 | } 30 | if (rightCount < n) { 31 | line[index] = ')'; 32 | process(line, index + 1, leftCount, rightCount + 1, n); 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0023_MergeKLists.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0023_MergeKLists { 4 | 5 | public ListNode mergeKLists(ListNode[] lists) { 6 | if (lists == null || lists.length == 0) { 7 | return null; 8 | } 9 | return mergeK(lists, 0, lists.length - 1); 10 | } 11 | 12 | private ListNode mergeK (ListNode[] list, int l, int r) { 13 | if (l == r) { 14 | return list [l]; 15 | } 16 | int mid = l + (( r - l) >> 1); 17 | ListNode listL = mergeK(list, l , mid); 18 | ListNode listR = mergeK(list, mid + 1, r); 19 | return merge (listL, listR); 20 | } 21 | 22 | private ListNode merge( ListNode l , ListNode r ) { 23 | ListNode root = new ListNode(); 24 | ListNode curr = root; 25 | 26 | while (l != null && r != null) { 27 | if (l.val < r.val) { 28 | curr.next = l; 29 | l = l.next; 30 | } else { 31 | curr.next = r; 32 | r = r.next; 33 | } 34 | curr = curr.next; 35 | } 36 | if (l == null) { 37 | curr.next = r; 38 | } else { 39 | curr.next = l; 40 | } 41 | return root.next; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0024_SwapNodesInPairs.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0024_SwapNodesInPairs { 4 | 5 | public ListNode swapPairs(ListNode head) { 6 | ListNode root = new ListNode(); 7 | root.next = head; 8 | ListNode curr = root; 9 | while (curr.next != null && curr.next.next != null) { 10 | ListNode next = curr.next; 11 | ListNode nextnext = curr.next.next; 12 | curr.next = nextnext; 13 | next.next = nextnext.next; 14 | nextnext.next = next; 15 | curr = next; 16 | } 17 | return root.next; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0025_ReverseNodesInKGroup.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0025_ReverseNodesInKGroup { 4 | 5 | public ListNode reverseKGroup(ListNode head, int k) { 6 | ListNode root = new ListNode(); 7 | root.next = head; 8 | ListNode pre = root; 9 | while (true) { 10 | ListNode t = nextTail(pre, k); 11 | if (t == null) { 12 | break; 13 | } 14 | ListNode next = t.next; 15 | ListNode tail = pre.next; 16 | t.next = null; 17 | ListNode newHead = reverse(pre.next); 18 | pre.next = newHead; 19 | tail.next = next; 20 | pre = tail; 21 | } 22 | return root.next; 23 | } 24 | 25 | public ListNode nextTail(ListNode head, int k) { 26 | while(k > 0) { 27 | if (head != null) { 28 | k--; 29 | head = head.next; 30 | } else { 31 | return null; 32 | } 33 | } 34 | return head; 35 | } 36 | 37 | private ListNode reverse(ListNode head) { 38 | ListNode pre = null; 39 | ListNode curr = head; 40 | while ( curr != null) { 41 | ListNode next = curr.next; 42 | curr.next = pre; 43 | pre = curr; 44 | curr = next; 45 | } 46 | return pre; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0032_LongestValidParentheses.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0032_LongestValidParentheses { 4 | 5 | public int longestValidParentheses(String s) { 6 | final char[] arr = s.toCharArray(); 7 | int maxLength = 0; 8 | 9 | int left = 0; 10 | int right = 0; 11 | for (int i = 0; i < arr.length; i++) { 12 | if (arr[i] == '(') { 13 | left ++; 14 | } else { 15 | right ++; 16 | } 17 | if (left == right) { 18 | maxLength = Math.max(maxLength, left); 19 | } else if (right > left ) { 20 | left = 0; 21 | right = 0; 22 | } 23 | } 24 | left = 0; 25 | right = 0; 26 | for (int i = arr.length - 1; i >= 0 ; i--) { 27 | if (arr[i] == '(') { 28 | left++; 29 | } else { 30 | right++; 31 | } 32 | if (left == right) { 33 | maxLength = Math.max(maxLength, left); 34 | } else if (left > right) { 35 | left = 0; 36 | right = 0; 37 | } 38 | } 39 | return maxLength * 2; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0033_Search.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0033_Search { 4 | 5 | public int search(int[] nums, int target) { 6 | int n = nums.length; 7 | int l = 0; 8 | int r = n - 1; 9 | while (l <= r) { 10 | int mid = l + ((r - l) >> 1); 11 | if (nums[mid] == target) { 12 | return mid; 13 | } 14 | if (nums[mid] < nums[r]) { 15 | if ( nums[mid] < target && target <= nums[r]) { 16 | l = mid + 1; 17 | } else { 18 | r = mid - 1; 19 | } 20 | } else { 21 | if (target < nums[mid] && nums[l] <= target) { 22 | r = mid - 1; 23 | } else { 24 | l = mid + 1; 25 | } 26 | } 27 | } 28 | return -1; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0034_FindFirstAndLastPositionOfElementInSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0034_FindFirstAndLastPositionOfElementInSortedArray { 4 | 5 | public int[] searchRange(int[] nums, int target) { 6 | if ( nums.length == 0) { 7 | return new int[]{-1 , -1}; 8 | } 9 | int l = 0; 10 | int r = nums.length - 1; 11 | int m = 0; 12 | int res = -1; 13 | while ( l <= r) { 14 | m = l + ((r - l) >> 1); 15 | if (nums[m] == target) { 16 | res = m; 17 | r = m - 1; 18 | } else if ( nums[m] > target) { 19 | res = m; 20 | r = m - 1; 21 | } else if ( nums[m] < target) { 22 | l = m + 1; 23 | } 24 | } 25 | int left = res; 26 | l = 0; 27 | r = nums.length - 1; 28 | m = 0; 29 | res = -1; 30 | while (l <= r) { 31 | m = l + ((r - l)>> 1); 32 | if (nums[m] == target) { 33 | res = m; 34 | l = m + 1; 35 | } else if (nums[m] > target) { 36 | r = m -1; 37 | } else if (nums[m] < target) { 38 | res = m; 39 | l = m + 1; 40 | } 41 | } 42 | int right = res; 43 | if (left <= right && left != -1 && right != -1 && nums[left] == target && nums[right] == target) { 44 | return new int[]{left, right}; 45 | } 46 | return new int[]{-1 , -1}; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0035_SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0035_SearchInsertPosition { 4 | 5 | public int searchInsert(int[] nums, int target) { 6 | int l = 0; 7 | int r = nums.length - 1; 8 | int m = 0; 9 | while (l <= r) { 10 | m = l + ((r - l) >> 1); 11 | if (nums[m] == target) { 12 | return m; 13 | } else if (nums[m] < target) { 14 | l = m + 1; 15 | } else { 16 | r = m - 1; 17 | } 18 | } 19 | return l; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0039_CombinationSum.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class _0039_CombinationSum { 8 | 9 | public List> combinationSum(int[] candidates, int target) { 10 | Arrays.sort(candidates); 11 | int[] choose = new int[candidates.length]; 12 | res = new ArrayList<>(); 13 | process(candidates, target, candidates.length - 1, choose); 14 | return res; 15 | } 16 | 17 | List> res; 18 | 19 | private void process(int[] candidates, int target, int index, int[] choose) { 20 | if(target == 0) { 21 | res.add(getRes(candidates, choose)); 22 | return; 23 | } 24 | if (index < 0) { 25 | return; 26 | } 27 | for(int i = 0; i * candidates[index] <= target ; i++) { 28 | choose[index] = i; 29 | process(candidates, target - candidates[index] * i, index - 1, choose); 30 | } 31 | choose[index] = 0; 32 | } 33 | 34 | private List getRes(int[] candidates, int[] choose) { 35 | List res = new ArrayList<>(); 36 | for (int i = 0; i < candidates.length; i++) { 37 | if(choose[i] > 0) { 38 | for (int j = 0; j < choose[i]; j++) { 39 | res.add(candidates[i]); 40 | } 41 | } 42 | } 43 | return res; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0041_FirstMissingPositive.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | class _0041_FirstMissingPositive { 4 | 5 | public int firstMissingPositive(int[] nums) { 6 | int n = nums.length; 7 | for (int i = 0; i < n; i++) { 8 | if (nums[i] <= 0) { 9 | nums[i] = n + 1; 10 | } 11 | } 12 | for (int i = 0; i < n; i++) { 13 | int num = Math.abs(nums[i]); 14 | if ( num <= n) { 15 | nums[num - 1] = -Math.abs(nums[num - 1]); 16 | } 17 | } 18 | for (int i = 0; i < n; i++) { 19 | if (nums[i] > 0) { 20 | return i + 1; 21 | } 22 | } 23 | return n + 1; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0042_Trap.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class _0042_Trap { 7 | 8 | public int trap(int[] height) { 9 | int n = height.length; 10 | int[] plus = new int[n+1]; 11 | 12 | for (int i = 0; i < n; i++) { 13 | plus[i] = height[i]; 14 | } 15 | Deque stack = new LinkedList<>(); 16 | int sum = 0; 17 | for (int i = 0; i < n + 1; i++) { 18 | while (!stack.isEmpty() && plus[stack.peek()] < plus[i]) { 19 | int currIndex = stack.pop(); 20 | if (!stack.isEmpty()) { 21 | int w = i - stack.peek() - 1; 22 | int h = Math.min(plus[i], plus[stack.peek()]) - plus[currIndex]; 23 | sum += w * h; 24 | } 25 | } 26 | stack.push(i); 27 | } 28 | return sum; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0045_JumpGameII.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0045_JumpGameII { 4 | 5 | public int jump(int[] nums) { 6 | int n = nums.length; 7 | int index = 0; 8 | int step = 0; 9 | while (index < n - 1) { 10 | step ++; 11 | int length = nums[index]; 12 | int max = 0; 13 | int next = index; 14 | for(int i = length; i >= 1; i--) { 15 | if (index + i >= n - 1) { 16 | return step; 17 | } 18 | if (nums[index + i] + i > max) { 19 | max = nums[index + i] + i; 20 | next = index + i; 21 | } 22 | } 23 | index = next; 24 | } 25 | return step; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0046_Permute.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class _0046_Permute { 7 | 8 | public List> permute(int[] nums) { 9 | List> res = new ArrayList<>(); 10 | process(nums,0, res); 11 | return res; 12 | } 13 | 14 | void process(int[] nums, int index, List> res) { 15 | if (index == nums.length) { 16 | save(nums, res); 17 | return; 18 | } 19 | for (int i = index; i < nums.length; i++) { 20 | swap(nums, index, i); 21 | process(nums, index + 1, res); 22 | swap(nums, index, i); 23 | } 24 | } 25 | 26 | void swap(int[] nums, int a, int b) { 27 | int t = nums[a]; 28 | nums[a] = nums[b]; 29 | nums[b] = t; 30 | } 31 | 32 | void save(int[] nums, List> res) { 33 | List line = new ArrayList<>(nums.length * 2); 34 | for(int n : nums){ 35 | line.add(n); 36 | } 37 | res.add(line); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0048_Rotate.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0048_Rotate { 4 | 5 | public void rotate(int[][] matrix) { 6 | int n = matrix.length; 7 | for (int i = 0; i < n / 2; i++) { 8 | for (int j = 0; j < n - i * 2 - 1; j++) { 9 | swap(matrix, i , j); 10 | } 11 | } 12 | } 13 | 14 | public void swap(int[][] matrix, int layer, int step) { 15 | int n = matrix.length; 16 | int x1 = layer + step; 17 | int y1 = layer; 18 | 19 | int x2 = (n - 1) - layer; 20 | int y2 = layer + step; 21 | 22 | int x3 = (n - 1) - (layer + step); 23 | int y3 = (n - 1) - layer; 24 | 25 | int x4 = layer; 26 | int y4 = (n - 1) - (layer + step); 27 | swap(matrix, x1, y1, x2, y2, x3, y3, x4, y4); 28 | 29 | } 30 | 31 | public void swap(int[][] matrix, int x1, int y1, 32 | int x2, int y2, 33 | int x3, int y3, 34 | int x4, int y4 35 | ) { 36 | int t = matrix[y1][x1]; 37 | matrix[y1][x1] = matrix[y4][x4]; 38 | matrix[y4][x4] = matrix[y3][x3]; 39 | matrix[y3][x3] = matrix[y2][x2]; 40 | matrix[y2][x2] = t; 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0049_GroupAnagrams.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | 8 | public class _0049_GroupAnagrams { 9 | 10 | public List> groupAnagrams(String[] strs) { 11 | HashMap> map = new HashMap<>(); 12 | for (String key : strs) { 13 | Word w = new Word(key); 14 | if (map.containsKey(w)) { 15 | map.get(w).add(key); 16 | } else { 17 | List s = new ArrayList<>(); 18 | s.add(key); 19 | map.put(w, s); 20 | } 21 | } 22 | return new ArrayList<>(map.values()); 23 | } 24 | 25 | class Word { 26 | int[] counts; 27 | Word(String s) { 28 | final char[] chars = s.toCharArray(); 29 | counts = new int[26]; 30 | for (char c : chars) { 31 | int index = c - 'a'; 32 | counts[index]++; 33 | } 34 | } 35 | 36 | public boolean equals(Object o) { 37 | return Arrays.equals(counts, ((Word)o).counts); 38 | } 39 | 40 | public int hashCode() { 41 | return Arrays.hashCode(counts); 42 | } 43 | 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0051_NQueens.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class _0051_NQueens { 7 | 8 | public List> solveNQueens(int n) { 9 | int[] table = new int[n]; 10 | process(0 ,0,0,0,table); 11 | return res; 12 | } 13 | 14 | List> res = new ArrayList<>(); 15 | 16 | private void process(int index, int l, int c, int r , int[] table) { 17 | int n = table.length; 18 | if (index == n) { 19 | save(table); 20 | return; 21 | } 22 | for (int i = 0; i < n; i++) { 23 | int target = 1 << i; 24 | int empty = (l & target) + (c & target) + (r & target); 25 | if (empty == 0) { 26 | table[index] = target; 27 | int newL = (l | target) << 1; 28 | int newC = c | target; 29 | int newR = (r | target) >> 1; 30 | process(index + 1, newL , newC, newR, table); 31 | } 32 | } 33 | } 34 | 35 | private void save(int[] table) { 36 | int n = table.length; 37 | List lines = new ArrayList<>(); 38 | for (int i = 0; i < n; i++) { 39 | int target = table[i]; 40 | StringBuilder sb = new StringBuilder(); 41 | for (int j = 0; j < n ; j++) { 42 | if ((target >> j) % 2 == 1) { 43 | sb.append('Q'); 44 | } else { 45 | sb.append('.'); 46 | } 47 | } 48 | lines.add(sb.toString()); 49 | } 50 | res.add(lines); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0053_MaxSubArray.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0053_MaxSubArray { 4 | 5 | public int maxSubArray(int[] nums) { 6 | int max = Integer.MIN_VALUE; 7 | int sum = 0; 8 | for (int num: nums) { 9 | sum = sum > 0 ? sum + num : num; 10 | max = Math.max(max, sum); 11 | } 12 | return max; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0054_spiralMatrix.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class _0054_spiralMatrix { 7 | 8 | public List spiralOrder(int[][] matrix) { 9 | int col = matrix[0].length; 10 | int row = matrix.length; 11 | 12 | int size = col * row; 13 | final ArrayList res = new ArrayList<>(); 14 | int step = 0; 15 | while(res.size() < size) { 16 | process(matrix, step++, res); 17 | } 18 | return res; 19 | } 20 | 21 | private void process(int[][] matrix, int step, List res) { 22 | int m = matrix.length; 23 | int n = matrix[0].length; 24 | int top = step; 25 | int bottom = m - step - 1; 26 | int left = step; 27 | int right = n - step - 1; 28 | 29 | for (int i = left; i <= right && res.size()< n * m ; i++) { 30 | res.add(matrix[top][i]); 31 | } 32 | for (int i = top + 1; i <= bottom && res.size() < n * m; i++) { 33 | res.add(matrix[i][right]); 34 | } 35 | for (int i = right - 1; i >= left && res.size() < n * m ; i--) { 36 | res.add(matrix[bottom][i]); 37 | } 38 | for (int i = bottom - 1; i > top && res.size() < n * m; i--) { 39 | res.add(matrix[i][left]); 40 | } 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0055_CanJump.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0055_CanJump { 4 | 5 | public boolean canJump(int[] nums) { 6 | int index = 0; 7 | int n = nums.length; 8 | while (true) { 9 | int length = nums[index]; 10 | int max = 0; 11 | int maxIndex = 0; 12 | for (int i = index + 1; i <= index + length && i < n; i++) { 13 | if (i + nums[i] >= max) { 14 | max = i + nums[i]; 15 | maxIndex = i; 16 | } 17 | } 18 | if (nums[maxIndex] + maxIndex >= nums.length - 1) { 19 | return true; 20 | } 21 | if (maxIndex == 0) { 22 | return false; 23 | } 24 | index = maxIndex; 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0056_Merge.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | 8 | public class _0056_Merge { 9 | 10 | public int[][] merge(int[][] intervals) { 11 | Arrays.sort(intervals, Comparator.comparingInt(o -> o[0])); 12 | List resList = new ArrayList<>(); 13 | int l = intervals[0][0]; 14 | int r = intervals[0][1]; 15 | 16 | for (int i = 0; i < intervals.length; i++) { 17 | int currL = intervals[i][0]; 18 | int currR = intervals[i][1]; 19 | if (currL <= r) { 20 | r = Math.max(r, currR); 21 | } else { 22 | int[] save = new int[2]; 23 | save[0] = l; 24 | save[1] = r; 25 | resList.add(save); 26 | l = currL; 27 | r = currR; 28 | } 29 | } 30 | int[] save = new int[2]; 31 | save[0] = l; 32 | save[1] = r; 33 | resList.add(save); 34 | 35 | int resLength = resList.size(); 36 | int[][] res = new int[resLength][]; 37 | for (int i = 0; i < resLength; i++) { 38 | res[i] = resList.get(i); 39 | } 40 | return res; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0062_UniquePaths.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0062_UniquePaths { 4 | 5 | public int uniquePaths(int m, int n) { 6 | int[][] dp = new int[m][n]; 7 | 8 | for (int i = 0; i < n; i++) { 9 | dp[m-1][i] = 1; 10 | } 11 | for (int i = 0; i < m; i++) { 12 | dp[i][n-1] = 1; 13 | } 14 | for (int i = m-2; i >= 0; i--) { 15 | for (int j = n -2 ; j >= 0; j--) { 16 | dp[i][j] = dp[i+1][j] + dp[i][j+1]; 17 | } 18 | } 19 | return dp[0][0]; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0064_MinPathSum.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0064_MinPathSum { 4 | 5 | public int minPathSum(int[][] grid) { 6 | int m = grid.length; 7 | int n = grid[0].length; 8 | 9 | int[][] dp = new int[m][n]; 10 | dp[m-1][n-1] = grid[m - 1][n - 1]; 11 | for (int i = m - 2; i >= 0; i--) { 12 | dp[i][n-1] = dp[i+1][n-1] + grid[i][n-1]; 13 | } 14 | for (int i = n - 2; i >= 0; i--) { 15 | dp[m-1][i] = dp[m-1][i+1] + grid[m-1][i]; 16 | } 17 | for (int i = m - 2; i >= 0; i--) { 18 | for (int j = n -2 ; j >= 0; j--) { 19 | dp[i][j] = grid[i][j] + Math.min(dp[i+1][j], dp[i][j+1]); 20 | } 21 | } 22 | return dp[0][0]; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0070_ClimbStairs.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.HashMap; 4 | 5 | public class _0070_ClimbStairs { 6 | 7 | public int climbStairs(int n) { 8 | if (n == 1) { 9 | return 1; 10 | } 11 | int[] dp = new int[n+1]; 12 | dp[0] = 1; 13 | dp[1] = 1; 14 | for (int i = 2; i <= n; i++) { 15 | dp[i] = dp[i-1] + dp[i-2]; 16 | } 17 | 18 | return dp[n]; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0072_MinDistance.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0072_MinDistance { 4 | 5 | public int minDistance(String word1, String word2) { 6 | int l1 = word1.length(); 7 | int l2 = word2.length(); 8 | 9 | int[][] dp = new int[l1 + 1][l2 + 1]; 10 | return process(word1, word2, 0, 0, dp); 11 | } 12 | 13 | int process(String word1, String word2, int index1, int index2, int[][] dp) { 14 | int l1 = word1.length(); 15 | int l2 = word2.length(); 16 | 17 | if(index1 == l1) { 18 | return l2 - index2; 19 | } 20 | if (index2 == l2) { 21 | return l1 - index1; 22 | } 23 | if (dp[index1][index2] != 0) { 24 | return dp[index1][index2] - 1; 25 | } 26 | int res; 27 | if (word1.charAt(index1) == word2.charAt(index2)) { 28 | res = process(word1, word2, index1 + 1, index2 + 1, dp); 29 | } else { 30 | int p1 = process(word1, word2, index1 + 1, index2 + 1, dp) + 1; 31 | int p2 = process(word1, word2, index1 + 1, index2, dp) + 1; 32 | int p3 = process(word1, word2, index1, index2 + 1, dp) + 1; 33 | 34 | res = Math.min(p1, Math.min(p2, p3)); 35 | } 36 | dp[index1][index2] = res + 1; 37 | return res; 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0073_SetMatrixZeroes.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0073_SetMatrixZeroes { 4 | 5 | public void setZeroes(int[][] matrix) { 6 | int col = matrix.length; 7 | int row = matrix[0].length; 8 | 9 | boolean isFirstColZero = false; 10 | for (int y = 0; y < row ; y++) { 11 | if (matrix[0][y] == 0) { 12 | isFirstColZero = true; 13 | } 14 | } 15 | boolean isFirstRowZero = false; 16 | for ( int x = 0; x < col; x++) { 17 | if (matrix[x][0] == 0) { 18 | isFirstRowZero = true; 19 | } 20 | } 21 | for (int y = 1; y < row ; y++) { 22 | for (int x = 1; x < col; x++) { 23 | if(matrix[x][y] == 0) { 24 | matrix[x][0] = 0; 25 | matrix[0][y] = 0; 26 | } 27 | } 28 | } 29 | for (int y = 1; y < row ; y++) { 30 | for (int x = 1; x < col; x++) { 31 | if (matrix[x][0] == 0 || matrix[0][y] == 0) { 32 | matrix[x][y] = 0; 33 | } 34 | } 35 | } 36 | if (isFirstColZero) { 37 | for (int y = 0; y < row; y++) { 38 | matrix[0][y] = 0; 39 | } 40 | } 41 | if (isFirstRowZero) { 42 | for (int x = 0; x < col; x++) { 43 | matrix[x][0] = 0; 44 | } 45 | } 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0074_SearchA2dMatrix.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0074_SearchA2dMatrix { 4 | 5 | public boolean searchMatrix(int[][] matrix, int target) { 6 | int l = 0; 7 | int r = matrix.length - 1; 8 | int res = -1; 9 | while ( l <= r) { 10 | int m = l +(( r - l) >> 1); 11 | if (matrix[m][0] == target) { 12 | res = m; 13 | l = m+1; 14 | } else if (matrix[m][0] > target) { 15 | r = m - 1; 16 | } else if (matrix[m][0] < target) { 17 | res = m; 18 | l = m + 1; 19 | } 20 | } 21 | if (res < 0) { 22 | return false; 23 | } 24 | int[] line = matrix[res]; 25 | l = 0; 26 | r = line.length - 1; 27 | while (l <= r) { 28 | int mid = l + ((r - l) >> 1); 29 | if (line[mid] == target) { 30 | return true; 31 | } else if (line[mid] > target) { 32 | r = mid - 1; 33 | } else { 34 | l = mid + 1; 35 | } 36 | } 37 | return false; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0076_MinWindow.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0076_MinWindow { 4 | 5 | public String minWindow(String s, String t) { 6 | int[] counts = new int[256]; 7 | int[] target = count(t); 8 | int l = 0; 9 | int r = -1; 10 | 11 | int maxL = 0; 12 | int maxR = Integer.MAX_VALUE; 13 | 14 | while(l < s.length()) { 15 | if (has(counts, target)) { 16 | if ( r - l < maxR - maxL) { 17 | maxR = r; 18 | maxL = l; 19 | } 20 | counts[s.charAt(l++)]--; 21 | } else { 22 | if (r == s.length() - 1) { 23 | break; 24 | } 25 | counts[s.charAt(++r)]++; 26 | } 27 | } 28 | return maxR == Integer.MAX_VALUE ? "" : s.substring(maxL, maxR + 1); 29 | 30 | } 31 | 32 | int[] count(String t) { 33 | int[] counts = new int[256]; 34 | final char[] chars = t.toCharArray(); 35 | for (int c : chars) { 36 | counts[c]++; 37 | } 38 | return counts; 39 | } 40 | 41 | boolean has(int[] s, int[] t) { 42 | for (int i = 0; i < 256; i++) { 43 | if (s[i] < t[i]) { 44 | return false; 45 | } 46 | } 47 | return true; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0078_Subsets.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class _0078_Subsets { 7 | 8 | public List> subsets(int[] nums) { 9 | List> res = new ArrayList<>(); 10 | process(nums, 0, res, new ArrayList<>()); 11 | return res; 12 | } 13 | 14 | public void process(int[] nums, int index, List> res , List line) { 15 | if (index == nums.length) { 16 | res.add(new ArrayList<>(line)); 17 | return; 18 | } 19 | process(nums,index + 1, res, line); 20 | line.add(nums[index]); 21 | process(nums, index + 1, res, line); 22 | line.remove(line.size() -1); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0079_Exist.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0079_Exist { 4 | 5 | public boolean exist(char[][] board, String word) { 6 | int m = board.length; 7 | int n = board[0].length; 8 | final char[] chars = word.toCharArray(); 9 | for ( int i = 0; i < m; i++) { 10 | for (int j = 0; j < n; j++) { 11 | final boolean process = process(board, chars, i , j , 0); 12 | if(process) { 13 | return true; 14 | } 15 | } 16 | } 17 | return false; 18 | } 19 | 20 | private boolean process(char[][] board, char[] word, int i, int j , int index) { 21 | if(index == word.length) { 22 | return true; 23 | } 24 | if ( i < 0 || i >= board.length || j < 0 || j >= board[0].length) { 25 | return false; 26 | } 27 | if (board[i][j] == word[index]) { 28 | board[i][j] = (char)(-board[i][j]); 29 | boolean p1 = process(board,word,i-1,j,index+1); 30 | boolean p2 = process(board,word,i+1,j,index+1); 31 | boolean p3 = process(board,word,i,j-1,index+1); 32 | boolean p4 = process(board,word,i,j+1,index+1); 33 | board[i][j] = (char)(-board[i][j]); 34 | return p1 || p2 || p3 || p4; 35 | } else { 36 | return false; 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0084_LargestRectangleArea.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class _0084_LargestRectangleArea { 7 | 8 | public int largestRectangleArea(int[] heights) { 9 | int[] plus = new int[heights.length + 2]; 10 | for (int i = 0; i < heights.length; i++) { 11 | plus[i+1] = heights[i]; 12 | } 13 | Deque deque = new LinkedList<>(); 14 | int max = 0; 15 | int n = plus.length; 16 | for (int i = 0; i < n; i++) { 17 | while ( !deque.isEmpty() && plus[deque.peek()] > plus[i]) { 18 | int curr = deque.pop(); 19 | int h = plus[curr]; 20 | int w = deque.isEmpty() ? i - 1 : i - deque.peek() - 1; 21 | max = Math.max(max, h*w); 22 | } 23 | deque.push(i); 24 | } 25 | return max; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0094_InorderTraversal.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Deque; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | 8 | public class _0094_InorderTraversal { 9 | 10 | public List inorderTraversal(TreeNode root) { 11 | List res = new ArrayList<>(); 12 | Deque stack = new LinkedList<>(); 13 | while ( root != null || !stack.isEmpty()) { 14 | while (root != null) { 15 | stack.push(root); 16 | root = root.left; 17 | } 18 | root = stack.pop(); 19 | res.add(root.val); 20 | root = root.right; 21 | } 22 | return res; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0098_IsValidBST.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0098_IsValidBST { 4 | 5 | class NodeRes { 6 | public int min; 7 | public int max; 8 | public boolean valid; 9 | 10 | NodeRes (boolean v) { 11 | valid = v; 12 | } 13 | } 14 | 15 | public boolean isValidBST(TreeNode root) { 16 | if (root == null) { 17 | return true; 18 | } 19 | return process(root).valid; 20 | } 21 | 22 | private NodeRes process( TreeNode root) { 23 | if (root == null) { 24 | return null; 25 | } 26 | final NodeRes left = process(root.left); 27 | final NodeRes right = process(root.right); 28 | int min = root.val; 29 | int max = root.val; 30 | if (left != null) { 31 | if (!left.valid) { 32 | return left; 33 | } 34 | if (left.max >= root.val) { 35 | return new NodeRes(false); 36 | } 37 | min = Math.min(min, left.min); 38 | } 39 | if (right != null) { 40 | if (!right.valid) { 41 | return right; 42 | } 43 | if (right.min <= root.val) { 44 | return new NodeRes(false); 45 | } 46 | max = Math.max(max, right.max); 47 | } 48 | NodeRes res = new NodeRes(true); 49 | res.max = max; 50 | res.min = min; 51 | return res; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0101_IsSymmetric.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0101_IsSymmetric { 4 | 5 | public boolean isSymmetric(TreeNode root) { 6 | if (root == null) { 7 | return true; 8 | } 9 | return isSymmetric(root.left, root.right); 10 | } 11 | 12 | public boolean isSymmetric(TreeNode l , TreeNode r) { 13 | if (l == null && r == null) { 14 | return true; 15 | } 16 | if (l == null || r == null) { 17 | return false; 18 | } 19 | if (l.val != r.val) { 20 | return false; 21 | } 22 | return isSymmetric(l.left, r.right) && isSymmetric(l.right, r.left); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0102_LevelOrder.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Deque; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | 8 | public class _0102_LevelOrder { 9 | 10 | public List> levelOrder(TreeNode root) { 11 | List> res = new ArrayList<>(); 12 | if (root == null) { 13 | return res; 14 | } 15 | Deque deque = new LinkedList<>(); 16 | TreeNode nextLast = root; 17 | TreeNode currLast = root; 18 | deque.addLast(root); 19 | List line = new ArrayList<>(); 20 | while (!deque.isEmpty()) { 21 | final TreeNode curr = deque.pollFirst(); 22 | line.add(curr.val); 23 | if (curr.left != null){ 24 | deque.addLast(curr.left); 25 | nextLast = curr.left; 26 | } 27 | if (curr.right != null) { 28 | deque.addLast(curr.right); 29 | nextLast = curr.right; 30 | } 31 | if (curr == currLast) { 32 | res.add(line); 33 | line = new ArrayList<>(); 34 | currLast = nextLast; 35 | } 36 | } 37 | return res; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0104_MaxDepth.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0104_MaxDepth { 4 | 5 | public int maxDepth(TreeNode root) { 6 | return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0105_BuildTree.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Arrays; 4 | 5 | public class _0105_BuildTree { 6 | 7 | public TreeNode buildTree(int[] preorder, int[] inorder) { 8 | if ( preorder == null || preorder.length == 0) { 9 | return null; 10 | } 11 | if (preorder.length == 1) { 12 | return new TreeNode(preorder[0]); 13 | } 14 | int root = preorder[0]; 15 | final int rootOf = indexOf(inorder, root); 16 | final int[] preorderLeft = Arrays.copyOfRange(preorder, 1, rootOf + 1); 17 | final int[] inorderLeft = Arrays.copyOfRange(inorder, 0, rootOf); 18 | TreeNode left = buildTree(preorderLeft, inorderLeft); 19 | final int[] preorderRight = Arrays.copyOfRange(preorder, rootOf+1, preorder.length); 20 | final int[] inorderRight = Arrays.copyOfRange(inorder, rootOf + 1, inorder.length); 21 | final TreeNode right = buildTree(preorderRight, inorderRight); 22 | return new TreeNode( preorder[0], left, right); 23 | } 24 | 25 | public int indexOf(int[] nums, int target) { 26 | for (int i = 0; i < nums.length; i++) { 27 | if (nums[i] == target) { 28 | return i; 29 | } 30 | } 31 | return -1; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0108_ConvertSortedArrayToBinary.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0108_ConvertSortedArrayToBinary { 4 | 5 | public TreeNode sortedArrayToBST(int[] nums) { 6 | return process(nums,0, nums.length - 1); 7 | } 8 | 9 | private TreeNode process(int[] nums, int l , int r) { 10 | if (l > r ) { 11 | return null; 12 | } 13 | int mid = ((r + l)>> 1); 14 | final TreeNode treeNode = new TreeNode(nums[mid]); 15 | final TreeNode left = process(nums, l , mid -1); 16 | final TreeNode right = process(nums, mid+1,r); 17 | treeNode.left = left; 18 | treeNode.right = right; 19 | return treeNode; 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0114_Flatten.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0114_Flatten { 4 | 5 | public void flatten(TreeNode root) { 6 | if (root == null) { 7 | return; 8 | } 9 | flatten(root.left); 10 | flatten(root.right); 11 | TreeNode r = root.right; 12 | root.right = root.left; 13 | root.left = null; 14 | while (root.right != null) { 15 | root = root.right; 16 | } 17 | root.right = r; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0118_Generate.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class _0118_Generate { 7 | 8 | public List> generate(int numRows) { 9 | List> res = new ArrayList<>(); 10 | List one = new ArrayList<>(); 11 | one.add(1); 12 | res.add(one); 13 | if (numRows == 1) { 14 | return res; 15 | } 16 | List two = new ArrayList<>(); 17 | two.add(1); 18 | two.add(1); 19 | res.add(two); 20 | if (numRows == 2) { 21 | return res; 22 | } 23 | for (int i = 2 ; i < numRows ; i++) { 24 | List line = new ArrayList<>(); 25 | line.add(1); 26 | List last = res.get(i - 1); 27 | for (int j = 0; j < i - 1; j++) { 28 | line.add(last.get(j) + last.get(j+1)); 29 | } 30 | line.add(1); 31 | res.add(line); 32 | } 33 | return res; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0121_MaxProfit.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0121_MaxProfit { 4 | 5 | public int maxProfit(int[] prices) { 6 | int n = prices.length; 7 | int res = 0; 8 | int h = Integer.MIN_VALUE; 9 | for (int i = n - 1; i >= 0 ; i--) { 10 | if (prices[i] > h) { 11 | h = prices[i]; 12 | } 13 | res = Math.max(res, h - prices[i]); 14 | } 15 | return res; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0124_MaxPathSum.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0124_MaxPathSum { 4 | 5 | public int maxPathSum(TreeNode root) { 6 | return process(root).maxSum; 7 | } 8 | 9 | class Info { 10 | int maxEdge; 11 | int maxSum; 12 | } 13 | 14 | private Info process(TreeNode root) { 15 | if (root == null) { 16 | return null; 17 | } 18 | Info left = process(root.left); 19 | Info right = process(root.right); 20 | 21 | int maxEdge = root.val; 22 | int sum = root.val; 23 | int maxSum = root.val; 24 | if (left != null) { 25 | maxEdge = Math.max(maxEdge, root.val + left.maxEdge); 26 | maxSum = left.maxSum; 27 | sum = Math.max(sum, sum + left.maxEdge); 28 | } 29 | if (right != null) { 30 | maxEdge = Math.max(maxEdge, root.val + right.maxEdge); 31 | maxSum = Math.max(maxSum, right.maxSum); 32 | sum = Math.max(sum, sum + right.maxEdge); 33 | } 34 | maxSum = Math.max(maxSum, sum); 35 | Info res = new Info(); 36 | res.maxEdge = maxEdge; 37 | res.maxSum = maxSum; 38 | return res; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0128_LongestConsecutive.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.HashSet; 4 | 5 | public class _0128_LongestConsecutive { 6 | 7 | public int longestConsecutive(int[] nums) { 8 | HashSet set = new HashSet<>(); 9 | for (int n : nums) { 10 | set.add(n); 11 | } 12 | int count = 0; 13 | int res = 0; 14 | for (int n : set) { 15 | if (!set.contains(n - 1)) { 16 | //说明这部分是开始的数据:头 17 | count = 1; 18 | int curr = n; 19 | while (set.contains(curr + 1)) { 20 | //如果存在说明是:中 21 | count++; 22 | curr++; 23 | } 24 | //结束了不满足条件说明到了:尾 25 | res = Math.max(res, count); 26 | } 27 | } 28 | return res; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0131_PalindromePartitioning.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class _0131_PalindromePartitioning { 8 | 9 | public List> partition(String s) { 10 | process(s.toCharArray(), 0, new ArrayList<>()); 11 | return res; 12 | } 13 | 14 | List> res = new ArrayList<>(); 15 | 16 | private void process (char[] chars, int index, List line) { 17 | if (index == chars.length) { 18 | res.add(new ArrayList<>(line)); 19 | return; 20 | } 21 | for (int i = index; i< chars.length; i++) { 22 | if (isPalindrome(chars,index,i)) { 23 | line.add(new String(Arrays.copyOfRange(chars, index, i+1))); 24 | process(chars, i+1, line); 25 | line.remove(line.size() - 1); 26 | } 27 | } 28 | } 29 | 30 | private boolean isPalindrome(char[] chars, int l ,int r) { 31 | if (l == r) { 32 | return true; 33 | } 34 | while (l wordDict) { 8 | Tree dic = new Tree(); 9 | for (String word : wordDict) { 10 | dic.add(word); 11 | } 12 | int[] dp = new int[s.length()]; 13 | return process(s.toCharArray(), 0 ,dic, dp); 14 | } 15 | 16 | private boolean process(char[] chars, int index, Tree tree, int[] dp) { 17 | if (index == chars.length) { 18 | return true; 19 | } 20 | if (dp[index] != 0){ 21 | return dp[index] == 1; 22 | } 23 | boolean res = false; 24 | Node curr = tree.root; 25 | for (int i = index; i < chars.length ; i++ ) { 26 | curr = curr.path[chars[i] - 'a']; 27 | if (curr == null) { 28 | break; 29 | } 30 | if (curr.end) { 31 | res = res || process(chars, i+ 1, tree , dp); 32 | } 33 | } 34 | dp[index] = res ? 1: -1; 35 | return res; 36 | 37 | } 38 | 39 | public class Node { 40 | Node[] path; 41 | 42 | boolean pass; 43 | 44 | boolean end; 45 | 46 | Node() { 47 | path = new Node[26]; 48 | } 49 | } 50 | 51 | public class Tree { 52 | Node root = new Node(); 53 | 54 | private void add(String s) { 55 | final char[] chars = s.toCharArray(); 56 | Node curr = root; 57 | for (char aChar : chars) { 58 | if (curr.path[aChar - 'a'] == null) { 59 | curr.path[aChar - 'a'] = new Node(); 60 | } 61 | final Node next = curr.path[aChar - 'a']; 62 | next.pass = true; 63 | curr = next; 64 | } 65 | curr.end = true; 66 | } 67 | } 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0141_HasCycle.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0141_HasCycle { 4 | 5 | public boolean hasCycle(ListNode head) { 6 | if (head == null) { 7 | return false; 8 | } 9 | ListNode fastNode = head; 10 | ListNode slowNode = head; 11 | while ( fastNode.next != null && fastNode.next.next != null) { 12 | fastNode = fastNode.next.next; 13 | slowNode = slowNode.next; 14 | if (fastNode == slowNode) { 15 | return true; 16 | } 17 | } 18 | return false; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0142_DetectCycle.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0142_DetectCycle { 4 | 5 | public ListNode detectCycle(ListNode head) { 6 | if ( head == null) { 7 | return null; 8 | } 9 | ListNode fastNode = head; 10 | ListNode slowNode = head; 11 | while (true) { 12 | if (fastNode.next == null || fastNode.next.next == null) { 13 | return null; 14 | } 15 | fastNode = fastNode.next.next; 16 | slowNode = slowNode.next; 17 | if (fastNode == slowNode) { 18 | break; 19 | } 20 | } 21 | slowNode = head; 22 | while (true) { 23 | if (slowNode == fastNode) { 24 | return fastNode; 25 | } 26 | slowNode = slowNode.next; 27 | fastNode = fastNode.next; 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0146_LRUCache.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.HashMap; 4 | 5 | public class _0146_LRUCache { 6 | 7 | class LRUCache { 8 | 9 | private int size; 10 | 11 | private int capacity; 12 | 13 | private Node head; 14 | 15 | private Node tail; 16 | 17 | private HashMap map; 18 | 19 | public class Node { 20 | 21 | public Node (int key, int val) { 22 | this.key = key; 23 | value = val; 24 | } 25 | 26 | public int key; 27 | public int value; 28 | public Node next; 29 | public Node pre; 30 | 31 | } 32 | 33 | public LRUCache(int capacity) { 34 | size = 0; 35 | this.capacity = capacity; 36 | head = new Node(0,0); 37 | tail = new Node(0,0); 38 | head.next = tail; 39 | tail.pre = head; 40 | map = new HashMap<>(); 41 | } 42 | 43 | public int get(int key) { 44 | if (map.containsKey(key)) { 45 | final Node node = map.get(key); 46 | remove(node); 47 | put(node); 48 | return node.value; 49 | } else { 50 | return -1; 51 | } 52 | 53 | } 54 | 55 | public void put(int key, int value) { 56 | if (map.containsKey(key)) { 57 | final Node node = map.get(key); 58 | remove(node); 59 | node.value = value; 60 | put(node); 61 | } else { 62 | size++; 63 | final Node node = new Node(key, value); 64 | put(node); 65 | if (size > capacity) { 66 | remove(tail.pre); 67 | size--; 68 | } 69 | } 70 | } 71 | 72 | void remove(Node node) { 73 | node.next.pre = node.pre; 74 | node.pre.next = node.next; 75 | map.remove(node.key); 76 | } 77 | 78 | void put(Node node) { 79 | final Node next = head.next; 80 | map.put(node.key, node); 81 | head.next = node; 82 | node.next = next; 83 | next.pre = node; 84 | node.pre = head; 85 | } 86 | 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0148_SortList.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0148_SortList { 4 | 5 | public ListNode sortList(ListNode head) { 6 | if (head == null) { 7 | return null; 8 | } 9 | if (head.next == null) { 10 | return head; 11 | } 12 | ListNode root = new ListNode(); 13 | root.next = head; 14 | ListNode slowNode = root; 15 | ListNode fastNode = root; 16 | while (fastNode.next != null && fastNode.next.next != null) { 17 | slowNode = slowNode.next; 18 | fastNode = fastNode.next.next; 19 | } 20 | ListNode l = root.next; 21 | ListNode r = slowNode.next; 22 | slowNode.next = null; 23 | return merge(sortList(l), sortList(r)); 24 | } 25 | 26 | public ListNode merge(ListNode l, ListNode r) { 27 | ListNode root = new ListNode(); 28 | ListNode curr = root; 29 | while (l != null && r != null) { 30 | if (l.val < r.val) { 31 | curr.next = l; 32 | l = l.next; 33 | } else { 34 | curr.next = r; 35 | r = r.next; 36 | } 37 | curr = curr.next; 38 | } 39 | if (l == null) { 40 | curr.next = r; 41 | } 42 | if (r == null) { 43 | curr.next = l; 44 | } 45 | return root.next; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0152_MaxProduct.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0152_MaxProduct { 4 | 5 | public int maxProduct(int[] nums) { 6 | int n = nums.length; 7 | int[][] dp = new int[n+1][2]; 8 | for ( int i = 0; i < n ; i++) { 9 | dp[i][0] = nums[i]; 10 | dp[i][1] = nums[i]; 11 | } 12 | for (int j = 1; j < n ; j++) { 13 | int num = nums[j]; 14 | int max = Math.max(num, Math.max(dp[j-1][0] * num , dp [j-1][1] * num)); 15 | int min = Math.min(num, Math.min(dp[j-1][0] * num , dp [j-1][1] * num)); 16 | dp[j][0] = max; 17 | dp[j][1] = min; 18 | } 19 | int res = dp[0][0]; 20 | for (int i = 1; i < n; i++) { 21 | res = Math.max(res, dp[i][0]); 22 | } 23 | return res; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0153_FindMinimumInRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0153_FindMinimumInRotatedSortedArray { 4 | 5 | public int findMin(int[] nums) { 6 | int l = 0; 7 | int r = nums.length - 1; 8 | while ( l < r) { 9 | int mid = l + ((r - l) >> 1); 10 | if (nums[mid] <= nums[r]) { 11 | r = mid; 12 | } else { 13 | l = mid + 1; 14 | } 15 | } 16 | return nums[l]; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0155_MinStack.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class _0155_MinStack { 7 | 8 | class MinStack { 9 | 10 | Deque stack; 11 | 12 | Deque min; 13 | 14 | public MinStack() { 15 | stack = new LinkedList<>(); 16 | min = new LinkedList<>(); 17 | } 18 | 19 | public void push(int val) { 20 | stack.push(val); 21 | if (min.isEmpty()){ 22 | min.push(val); 23 | } else { 24 | final Integer peek = min.peek(); 25 | if (peek < val) { 26 | min.push(peek); 27 | } else { 28 | min.push(val); 29 | } 30 | } 31 | } 32 | 33 | public void pop() { 34 | stack.pop(); 35 | min.pop(); 36 | } 37 | 38 | public int top() { 39 | return stack.peek(); 40 | } 41 | 42 | public int getMin() { 43 | return min.peek(); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0160_GetIntersectionNode.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0160_GetIntersectionNode { 4 | 5 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 6 | ListNode currA = headA; 7 | ListNode currB = headB; 8 | 9 | int countA = 0; 10 | while (currA.next != null) { 11 | currA = currA.next; 12 | countA++; 13 | } 14 | int countB = 0; 15 | while (currB.next != null) { 16 | currB = currB.next; 17 | countB++; 18 | } 19 | if (currA != currB) { 20 | return null; 21 | } 22 | int step = Math.abs(countA - countB); 23 | if (countA > countB) { 24 | while (step > 0) { 25 | headA = headA.next; 26 | step--; 27 | } 28 | } else if (countB > countA) { 29 | while (step > 0) { 30 | headB = headB.next; 31 | step--; 32 | } 33 | } 34 | while (headA != headB) { 35 | headA = headA.next; 36 | headB = headB.next; 37 | } 38 | return headA; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0189_rotateArray.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0189_rotateArray { 4 | 5 | public void rotate(int[] nums, int k) { 6 | k %= nums.length; 7 | reverse(nums, 0, nums.length - 1); 8 | reverse(nums, 0, k - 1); 9 | reverse(nums, k , nums.length - 1); 10 | } 11 | 12 | private void reverse (int[] nums, int s , int e) { 13 | while(s < e) { 14 | int t = nums[s]; 15 | nums[s] = nums[e]; 16 | nums[e] = t; 17 | s++; 18 | e--; 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0198_Rob.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0198_Rob { 4 | 5 | 6 | public int rob(int[] nums){ 7 | if (nums.length == 0) { 8 | return 0; 9 | } 10 | if (nums.length == 1) { 11 | return nums[0]; 12 | } 13 | int n = nums.length; 14 | int[] dp = new int[n]; 15 | dp[0] = nums[0]; 16 | dp[1] = Math.max(nums[1], nums[0]); 17 | 18 | for (int i = 2 ; i < n ; i++) { 19 | dp[i] = Math.max(dp[i - 2] + nums[i], dp[i -1]); 20 | } 21 | return dp[n-1]; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0199_BinaryTreeRightSideView.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Deque; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | 8 | public class _0199_BinaryTreeRightSideView { 9 | 10 | public List rightSideView(TreeNode root) { 11 | List res = new ArrayList<>(); 12 | if (root == null) { 13 | return res; 14 | } 15 | final LinkedList deque = new LinkedList<>(); 16 | TreeNode nextLast = root; 17 | TreeNode currLast = root; 18 | deque.addLast(root); 19 | while (!deque.isEmpty()) { 20 | final TreeNode node = deque.pollFirst(); 21 | if (node.left != null) { 22 | deque.addLast(node.left); 23 | nextLast = node.left; 24 | } 25 | if (node.right != null) { 26 | deque.addLast(node.right); 27 | nextLast = node.right; 28 | } 29 | if (node == currLast) { 30 | res.add(node.val); 31 | currLast = nextLast; 32 | } 33 | } 34 | return res; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0200_NumIslands.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0200_NumIslands { 4 | 5 | public int numIslands(char[][] grid) { 6 | int res = 0; 7 | int m = grid.length; 8 | int n = grid[0].length; 9 | for (int y = 0; y < m; y++) { 10 | for ( int x = 0; x < n ; x++) { 11 | if ( grid[y][x] == '1') { 12 | res++; 13 | infect(grid, x , y); 14 | } 15 | } 16 | } 17 | return res; 18 | } 19 | 20 | public void infect(char[][] grid, int x , int y) { 21 | int m = grid.length; 22 | int n = grid[0].length; 23 | if ( x< 0 || x >= n) { 24 | return ; 25 | } 26 | if (y < 0 || y >= m) { 27 | return; 28 | } 29 | if ( grid[y][x] == '1') { 30 | grid[y][x] = '.'; 31 | } else { 32 | return; 33 | } 34 | infect(grid, x + 1, y); 35 | infect(grid, x - 1, y); 36 | infect(grid, x, y + 1); 37 | infect(grid, x, y - 1); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0207_CanFinish.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.*; 4 | 5 | public class _0207_CanFinish { 6 | 7 | public boolean canFinish(int numCourses, int[][] prerequisites) { 8 | int[] courses = new int[numCourses]; 9 | Arrays.fill(courses, 0); 10 | HashMap> needUnlock = new HashMap<>(); 11 | for(int[] pair : prerequisites) { 12 | int target = pair[0]; 13 | int need = pair[1]; 14 | final List orDefault = needUnlock.getOrDefault(need, new ArrayList<>()); 15 | orDefault.add(target); 16 | needUnlock.put(need,orDefault); 17 | courses[target]++; 18 | } 19 | Deque deque = new LinkedList<>(); 20 | for (int i = 0; i < numCourses; i++) { 21 | if (courses[i] == 0) { 22 | deque.addLast(i); 23 | } 24 | } 25 | while(!deque.isEmpty()) { 26 | final Integer c = deque.pollFirst(); 27 | if ( needUnlock.containsKey(c)) { 28 | final List integers = needUnlock.get(c); 29 | for (int i : integers) { 30 | if (courses[i] > 0) { 31 | courses[i] --; 32 | } 33 | if (courses[i] == 0) { 34 | deque.addLast(i); 35 | } 36 | } 37 | } 38 | needUnlock.remove(c); 39 | } 40 | for (int c : courses) { 41 | if (c > 0) { 42 | return false; 43 | } 44 | } 45 | return true; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0208_Trie.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0208_Trie { 4 | 5 | class Trie { 6 | 7 | class Node{ 8 | Node[] path; 9 | boolean hasEnd; 10 | 11 | Node() { 12 | path = new Node[26]; 13 | } 14 | } 15 | 16 | Node root; 17 | 18 | public Trie() { 19 | root = new Node(); 20 | } 21 | 22 | public void insert(String word) { 23 | final char[] chars = word.toCharArray(); 24 | Node curr = root; 25 | for (int i = 0; i < chars.length; i++) { 26 | final int aChar = chars[i] - 'a'; 27 | if ( curr.path[aChar] == null) { 28 | curr.path[aChar] = new Node(); 29 | } 30 | curr = curr.path[aChar]; 31 | } 32 | curr.hasEnd = true; 33 | } 34 | 35 | public boolean search(String word) { 36 | final char[] chars = word.toCharArray(); 37 | Node curr = root; 38 | for ( char c : chars) { 39 | final int aChar = c - 'a'; 40 | if (curr.path[aChar] == null) { 41 | return false; 42 | } 43 | curr = curr.path[aChar]; 44 | } 45 | return curr.hasEnd; 46 | } 47 | 48 | public boolean startsWith(String prefix) { 49 | final char[] chars = prefix.toCharArray(); 50 | Node curr = root; 51 | for ( char c : chars) { 52 | final int aChar = c - 'a'; 53 | if (curr.path[aChar] == null) { 54 | return false; 55 | } 56 | curr = curr.path[aChar]; 57 | } 58 | return true; 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0215_FindKthLargest.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0215_FindKthLargest { 4 | 5 | public int findKthLargest(int[] nums, int k) { 6 | int n = nums.length; 7 | int l = 0; 8 | int r = n - 1; 9 | k = n -k; 10 | 11 | while (l <= r) { 12 | final int[] range = partition(nums, l , r); 13 | if (range[0] <= k && range[1] >= k) { 14 | return nums[range[0]]; 15 | } else if (range[0] > k) { 16 | r = range[0] - 1; 17 | } else if (range[1] < k) { 18 | l = range[1] + 1; 19 | } 20 | } 21 | return 0; 22 | } 23 | 24 | public int[] partition(int[] nums,int l ,int r) { 25 | int chooseIndex = l + (int)(Math.random() * (r - l)); 26 | int target = nums[chooseIndex]; 27 | int index = l; 28 | int start = l - 1; 29 | int end = r; 30 | while(index <= end) { 31 | if (nums[index] > target) { 32 | swap(nums, end-- ,index); 33 | } else if (nums[index] < target) { 34 | swap(nums, ++ start, index++); 35 | } else { 36 | index ++; 37 | } 38 | } 39 | return new int[] {start + 1, end}; 40 | } 41 | 42 | private void swap (int [] nums, int a , int b) { 43 | int t = nums[a]; 44 | nums[a] = nums[b]; 45 | nums[b] = t; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0226_InvertTree.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Arrays; 4 | 5 | public class _0226_InvertTree { 6 | 7 | public TreeNode invertTree(TreeNode root) { 8 | if (root == null) { 9 | return null; 10 | } 11 | final TreeNode r = invertTree(root.right); 12 | final TreeNode l = invertTree(root.left); 13 | root.right = l; 14 | root.left = r; 15 | Arrays.hashCode(new int[1]); 16 | return root; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0230_KthSmallestElementInABst.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | 4 | public class _0230_KthSmallestElementInABst { 5 | 6 | class NodeRes { 7 | public boolean found; 8 | public int sumCount; 9 | 10 | public int target; 11 | } 12 | 13 | public int kthSmallest(TreeNode root, int k) { 14 | return process(root, k).target; 15 | } 16 | 17 | private NodeRes process(TreeNode root, int k) { 18 | int sumCount = 1; 19 | if (root.left != null) { 20 | final NodeRes left = process(root.left, k); 21 | if (left.found) { 22 | return left; 23 | } 24 | sumCount += left.sumCount; 25 | } 26 | if (sumCount == k) { 27 | final NodeRes nodeRes = new NodeRes(); 28 | nodeRes.found = true; 29 | nodeRes.target = root.val; 30 | return nodeRes; 31 | } 32 | if (root.right != null) { 33 | final NodeRes right = process(root.right, k - sumCount); 34 | if ( right.found) { 35 | return right; 36 | } 37 | sumCount += right.sumCount; 38 | } 39 | final NodeRes nodeRes = new NodeRes(); 40 | nodeRes.found = false; 41 | nodeRes.sumCount = sumCount; 42 | return nodeRes; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0234_IsPalindrome.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0234_IsPalindrome { 4 | 5 | public boolean isPalindrome(ListNode head) { 6 | int count = 0; 7 | ListNode curr = head; 8 | while(curr != null) { 9 | count++; 10 | curr = curr.next; 11 | } 12 | count = count / 2; 13 | curr = head; 14 | while(count > 0) { 15 | count--; 16 | curr = curr.next; 17 | } 18 | ListNode reverse = reverse(curr); 19 | while (reverse != null) { 20 | if (reverse.val != head.val) { 21 | return false; 22 | } 23 | reverse = reverse.next; 24 | head = head.next; 25 | } 26 | return true; 27 | } 28 | 29 | private ListNode reverse(ListNode head) { 30 | ListNode t; 31 | ListNode lastNode = null; 32 | while (head != null) { 33 | t = head.next; 34 | head.next = lastNode; 35 | lastNode = head; 36 | head = t; 37 | } 38 | return lastNode; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0236_LowestCommonAncestor.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0236_LowestCommonAncestor { 4 | 5 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 6 | return process(root, p, q).target; 7 | } 8 | 9 | class Info { 10 | public boolean foundQ; 11 | public boolean foundP; 12 | 13 | TreeNode target; 14 | 15 | Info(){ 16 | foundP = false; 17 | foundQ = false; 18 | target = null; 19 | } 20 | } 21 | 22 | public Info process( TreeNode root, TreeNode p , TreeNode q) { 23 | if (root == null) { 24 | return new Info(); 25 | } 26 | final Info left = process(root.left, p, q); 27 | final Info right = process(root.right, p, q); 28 | if (left.target != null) { 29 | return left; 30 | } 31 | if (right.target != null) { 32 | return right; 33 | } 34 | boolean foundP = root == p || left.foundP || right.foundP; 35 | boolean foundQ = root == q || left.foundQ || right.foundQ; 36 | Info info = new Info(); 37 | info.foundP = foundP; 38 | info.foundQ = foundQ; 39 | if (foundP && foundQ) { 40 | info.target = root; 41 | } 42 | return info; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0238_ProductExceptSelf.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0238_ProductExceptSelf { 4 | 5 | public int[] productExceptSelf(int[] nums) { 6 | int n = nums.length; 7 | int[] s1 = new int[n]; 8 | int[] s2 = new int[n]; 9 | 10 | s1[0] = nums[0]; 11 | s2[n - 1] = nums[n - 1]; 12 | for (int i = 1; i < n; i++) { 13 | s1[i] = nums[i] * s1[i - 1]; 14 | } 15 | for (int i = n - 2; i >= 0 ; i--) { 16 | s2[i] = nums[i] * s2[i+1]; 17 | } 18 | int[] res = new int[n]; 19 | res[0] = s2[1]; 20 | res[n - 1] = s1[n - 2]; 21 | for (int i = 1; i < n - 1; i++) { 22 | res[i] = s1[i-1] * s2[i+1]; 23 | } 24 | return res; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0239_MaxSlidingWindow.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class _0239_MaxSlidingWindow { 7 | 8 | public int[] maxSlidingWindow(int[] nums, int k) { 9 | int n = nums.length; 10 | if (n == 0) { 11 | return new int[0]; 12 | } 13 | int[] res = new int[n - k + 1]; 14 | Deque stack = new LinkedList<>(); 15 | for (int i = 0; i < n; i++) { 16 | while(!stack.isEmpty() && nums[i] > nums[stack.peek()]) { 17 | stack.pop(); 18 | } 19 | stack.push(i); 20 | if (i >= k) { 21 | if (stack.peekLast() <= (i - k)) { 22 | stack.pollLast(); 23 | } 24 | } 25 | if (i >= k - 1) { 26 | res[i - k + 1] = nums[stack.peekLast()]; 27 | } 28 | 29 | } 30 | return res; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0240_SearchMatrix.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0240_SearchMatrix { 4 | 5 | public boolean searchMatrix(int[][] matrix, int target) { 6 | int n = matrix.length; 7 | int m = matrix[0].length; 8 | 9 | int x = m - 1; 10 | int y = 0; 11 | while (x >= 0 && y < n) { 12 | int curr = matrix[y][x]; 13 | if (curr == target) { 14 | return true; 15 | } 16 | if (curr < target) { 17 | y++; 18 | } else { 19 | x--; 20 | } 21 | } 22 | return false; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0279_NumSquares.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class _0279_NumSquares { 8 | 9 | public int numSquares(int n) { 10 | int nlen = (int)Math.sqrt(n) + 1; 11 | int[] nn = new int[nlen]; 12 | for (int i = 0; i < nlen; i++) { 13 | nn[i] = (i + 1) * (i + 1); 14 | } 15 | int[] dp = new int[n+1]; 16 | Arrays.fill(dp, Integer.MAX_VALUE); 17 | dp[0] = 0; 18 | for (int i = 0; i < n; i++) { 19 | for (int j = 0; j < nlen; j++) { 20 | if (i + nn[j] <= n) { 21 | dp[i + nn[j]] = Math.min(dp[i+nn[j]], dp[i] + 1); 22 | } 23 | } 24 | } 25 | return dp[n]; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0283_MoveZeroes.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0283_MoveZeroes { 4 | 5 | public void moveZeroes(int[] nums) { 6 | int index = 0; 7 | int count = nums.length; 8 | 9 | for (int i = 0; i < count; i++) { 10 | if (nums[i] == 0){ 11 | continue; 12 | } 13 | if (index < i) { 14 | nums[index] = nums[i]; 15 | } 16 | index++; 17 | } 18 | for (int i = index; i < count; i++) { 19 | nums[i] = 0; 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0295_FindMedianFromDataStream.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | public class _0295_FindMedianFromDataStream { 6 | 7 | public class MedianFinder { 8 | PriorityQueue big; 9 | PriorityQueue small; 10 | 11 | int size; 12 | 13 | public MedianFinder() { 14 | big = new PriorityQueue<>((o1, o2) -> o2 - o1); 15 | small = new PriorityQueue<>(); 16 | size = 0; 17 | } 18 | 19 | public void addNum(int num) { 20 | size++; 21 | small.add(num); 22 | big.add(small.poll()); 23 | if (big.size()> small.size() + 1) { 24 | small.add(big.poll()); 25 | } 26 | } 27 | 28 | public double findMedian() { 29 | if (size % 2 == 0) { 30 | return (small.peek() + big.peek()) / 2.0d; 31 | } else { 32 | return big.peek(); 33 | } 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0300_LengthOfLIS.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0300_LengthOfLIS { 4 | 5 | public int lengthOfLIS(int[] nums) { 6 | if (nums.length == 0) { 7 | return 0; 8 | } 9 | int[] dp = new int[nums.length]; 10 | dp[0] = 1; 11 | int maxans = 1; 12 | for (int i = 1; i< nums.length; i++) { 13 | dp[i] = 1; 14 | for (int j = 0; j < i ; j++) { 15 | if (nums[i] > nums[j]) { 16 | dp[i] = Math.max(dp[i], dp[j] + 1); 17 | } 18 | } 19 | maxans = Math.max(maxans, dp[i]); 20 | } 21 | return maxans; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0322_CoinChange.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Arrays; 4 | 5 | public class _0322_CoinChange { 6 | 7 | public int coinChange(int[] coins, int amount) { 8 | int[] dp = new int [amount + 1]; 9 | Arrays.fill(dp, Integer.MAX_VALUE); 10 | dp[0] = 0; 11 | for (int coin : coins) { 12 | for (int i = coin; i <= amount ; i++) { 13 | if (dp[i - coin] != Integer.MAX_VALUE) { 14 | dp[i] = Math.min(dp[i], dp[i -coin] + 1); 15 | } 16 | } 17 | } 18 | return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount]; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0347_TopKFrequent.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.HashMap; 4 | import java.util.PriorityQueue; 5 | 6 | public class _0347_TopKFrequent { 7 | 8 | public int[] topKFrequent(int[] nums, int k) { 9 | HashMap map = new HashMap<>(); 10 | for (int n : nums) { 11 | map.put(n, map.getOrDefault(n, 0) + 1); 12 | } 13 | PriorityQueue queue = new PriorityQueue<>((o1, o2) -> o1[0] - o2[0]); 14 | for (int key : map.keySet()) { 15 | int[] t = new int[]{map.get(key), key}; 16 | queue.add(t); 17 | if (queue.size() > k) { 18 | queue.poll(); 19 | } 20 | } 21 | int[] res = new int[k]; 22 | int count = 0; 23 | while (!queue.isEmpty()) { 24 | res[count++] = queue.poll()[1]; 25 | } 26 | return res; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0394_DecodeString.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class _0394_DecodeString { 7 | 8 | public String decodeString(String s) { 9 | StringBuilder curr = new StringBuilder(); 10 | Deque stack = new LinkedList<>(); 11 | Deque stackCount = new LinkedList<>(); 12 | int count = 0; 13 | for (int i = 0; i < s.length(); i++) { 14 | final char c = s.charAt(i); 15 | if ( '0' <= c && c <= '9') { 16 | count = count * 10 + (c - '0'); 17 | } 18 | if ('a' <= c && c <= 'z') { 19 | curr.append(c); 20 | } 21 | if (c == '[') { 22 | stack.push(curr.toString()); 23 | stackCount.push(count); 24 | curr = new StringBuilder(); 25 | count = 0; 26 | } 27 | if (c == ']') { 28 | final Integer loop = stackCount.pop(); 29 | StringBuilder t = new StringBuilder(); 30 | for (int j = 0; j < loop; j++) { 31 | t.append(curr); 32 | } 33 | curr = new StringBuilder(stack.pop() + t); 34 | } 35 | } 36 | return curr.toString(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0416_CanPartition.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | 4 | public class _0416_CanPartition { 5 | 6 | public boolean canPartition(int[] nums) { 7 | int sum = 0; 8 | for (int num : nums) { 9 | sum += num; 10 | } 11 | if (sum %2 == 1) { 12 | return false; 13 | } 14 | int target = sum >> 1; 15 | boolean[] dp = new boolean[target + 1]; 16 | dp[0] = true; 17 | for (int i = 0; i < nums.length; i++) { 18 | int curr = nums[i]; 19 | for (int j = target; j >= curr; j--) { 20 | dp[j] = dp[j] || dp [j - curr]; 21 | } 22 | } 23 | return dp[target]; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0437_PathSum.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0437_PathSum { 4 | 5 | public int pathSum(TreeNode root, long targetSum) { 6 | if (root ==null) { 7 | return 0; 8 | } 9 | int res = process(root, targetSum); 10 | res += pathSum (root.left, targetSum); 11 | res += pathSum (root.right, targetSum); 12 | return res; 13 | } 14 | 15 | public int process(TreeNode root, long targetSum) { 16 | if (root == null) { 17 | return 0; 18 | } 19 | int res = 0; 20 | if (root.val == targetSum) { 21 | res++; 22 | } 23 | res += process(root.left, targetSum - root.val); 24 | res += process(root.right, targetSum - root.val); 25 | return res; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0438_FindAnagrams.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class _0438_FindAnagrams { 8 | 9 | public List findAnagrams(String s, String p) { 10 | List res = new ArrayList<>(); 11 | if (s.length() < p.length()) { 12 | return res; 13 | } 14 | final int[] target = count(p, 0, p.length()); 15 | final int[] sCount = count(s, 0, p.length() - 1); 16 | for (int i = 0; i < s.length() - p.length() + 1; i++) { 17 | ++sCount[s.charAt(i + p.length() - 1) - 'a']; 18 | if (Arrays.equals(target, sCount)) { 19 | res.add(i); 20 | } 21 | --sCount[s.charAt(i) - 'a']; 22 | } 23 | return res; 24 | } 25 | 26 | private int[] count(String s , int start, int end) { 27 | int[] res = new int[26]; 28 | for (int i = start; i < end; i++) { 29 | final int c = s.charAt(i) - 'a'; 30 | res[c]++; 31 | } 32 | return res; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0543_DiameterOfBinary.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | public class _0543_DiameterOfBinary { 4 | 5 | public int diameterOfBinaryTree(TreeNode root) { 6 | if (root == null) { 7 | return 0; 8 | } 9 | return process(root)[1]; 10 | } 11 | 12 | private int[] process(TreeNode root) { 13 | if(root == null) { 14 | return null; 15 | } 16 | final int[] l = process(root.left); 17 | final int[] r = process(root.right); 18 | 19 | int h = 0; 20 | int d = 0; 21 | if (l != null) { 22 | h = l[0] + 1; 23 | d = l[0] + 1; 24 | } 25 | if (r != null) { 26 | h = Math.max(h, r[0] + 1); 27 | d = d + r[0] + 1; 28 | } 29 | if (l != null) { 30 | d = Math.max(d, l[1]); 31 | } 32 | if (r != null) { 33 | d = Math.max(d, r[1]); 34 | } 35 | return new int[]{h, d}; 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0560_SubarraySum.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.HashMap; 4 | 5 | public class _0560_SubarraySum { 6 | 7 | public int subarraySum(int[] nums, int k) { 8 | int n = nums.length; 9 | int[] sums = new int[n]; 10 | int count = 0; 11 | sums[0] = nums[0]; 12 | for (int i = 1; i < n; i++) { 13 | sums[i] = nums[i] + sums[i - 1]; 14 | } 15 | final HashMap map = new HashMap<>(); 16 | map.put(0,1); 17 | for (int i = 0; i < n; i++) { 18 | count += map.getOrDefault(sums[i] - k, 0); 19 | map.put(sums[i], map.getOrDefault(sums[i], 0) + 1); 20 | } 21 | return count; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0739_DailyTemperatures.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class _0739_DailyTemperatures { 7 | 8 | public int[] dailyTemperatures(int[] temperatures) { 9 | Deque deque = new LinkedList<>(); 10 | int n = temperatures.length; 11 | int[] res = new int[n]; 12 | for( int i = n - 1; i >=0 ; i--) { 13 | while ( !deque.isEmpty() && temperatures[i] >= temperatures[deque.peek()]) { 14 | deque.pop(); 15 | } 16 | res[i] = deque.isEmpty() ? 0 : deque.peek() - i; 17 | deque.push(i); 18 | } 19 | return res; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0763_PartitionLabels.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Comparator; 5 | import java.util.List; 6 | 7 | public class _0763_PartitionLabels { 8 | 9 | public List partitionLabels(String s) { 10 | final int n = s.length(); 11 | int[][] pairs = new int[256][2]; 12 | for (int i = 0 ; i < n; i++) { 13 | final char c = s.charAt(i); 14 | int[] pair = pairs[c]; 15 | if (pair[0] == 0) { 16 | pair[0] = i + 1; 17 | } 18 | pair[1] = i + 1; 19 | } 20 | List pairsList = new ArrayList<>(); 21 | for( int[] pair : pairs) { 22 | if (pair[0] != 0) { 23 | pairsList.add(pair); 24 | } 25 | } 26 | 27 | pairsList.sort((Comparator.comparingInt(o -> o[0]))); 28 | final ArrayList res = new ArrayList<>(); 29 | int l = pairsList.get(0)[0]; 30 | int r = pairsList.get(0)[1]; 31 | for (int i = 1; i < pairsList.size(); i++) { 32 | final int[] pair = pairsList.get(i); 33 | int newL = pair[0]; 34 | int newR = pair[1]; 35 | if (newL > r) { 36 | res.add(r - l + 1); 37 | l = newL; 38 | r = newR; 39 | } else { 40 | r = Math.max(r, newR); 41 | } 42 | } 43 | res.add(r - l + 1); 44 | return res; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_0994_RottingOranges.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | /** 4 | * @author : loso 5 | * @version : v1.0 6 | * @date : Created in 2023/11/14 10:59 7 | * @description : 8 | * @modified By : loso 9 | */ 10 | public class _0994_RottingOranges { 11 | 12 | public int orangesRotting(int[][] grid) { 13 | int m = grid.length; 14 | int n = grid[0].length; 15 | int step = 0; 16 | int count = count(grid); 17 | int lastCount = -1; 18 | while(count > 0) { 19 | if (lastCount == count) { 20 | return -1; 21 | } 22 | step++; 23 | for (int y = 0; y < m; y++) { 24 | for (int x = 0; x < n; x++) { 25 | if (grid[y][x] == 2) { 26 | infect(grid, x, y); 27 | } 28 | } 29 | } 30 | lastCount = count; 31 | count -= flash(grid); 32 | } 33 | return step; 34 | } 35 | 36 | public int flash(int [][] grid) { 37 | int count = 0; 38 | int m = grid.length; 39 | int n = grid[0].length; 40 | for (int y = 0; y < m ; y++) { 41 | for (int x = 0; x < n; x++) { 42 | if ( grid[y][x] == -2) { 43 | count++; 44 | grid[y][x] = 2; 45 | } 46 | } 47 | } 48 | return count; 49 | } 50 | 51 | public void infect(int[][] grid, int x, int y) { 52 | int m = grid.length; 53 | int n = grid[0].length; 54 | 55 | if (x < 0 || x >= n) { 56 | return; 57 | } 58 | if (x < 0 || y >= m) { 59 | return; 60 | } 61 | if ( grid[y][x] == 2) { 62 | mark(grid, x + 1, y); 63 | mark(grid, x - 1,y); 64 | mark(grid, x , y + 1); 65 | mark(grid, x , y - 1); 66 | } 67 | } 68 | 69 | public void mark(int[][] grid, int x , int y) { 70 | int m = grid.length; 71 | int n = grid[0].length; 72 | if ( x < 0 || x >= n) { 73 | return; 74 | } 75 | if ( y < 0 || y >= m) { 76 | return; 77 | } 78 | if (grid[y][x] == 1) { 79 | grid[y][x] = -2; 80 | } 81 | } 82 | 83 | public int count(int[][] grid) { 84 | int m = grid.length; 85 | int n = grid[0].length; 86 | int count = 0; 87 | for (int y = 0; y < m; y++) { 88 | for (int x = 0; x < n; x++) { 89 | if ( grid[y][x] == 1) { 90 | count++; 91 | } 92 | } 93 | } 94 | return count; 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_1143_LongestCommonSubsequence.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | import java.util.Arrays; 4 | 5 | public class _1143_LongestCommonSubsequence { 6 | 7 | public int longestCommonSubsequence(String text1, String text2) { 8 | int l1 = text1.length(); 9 | int l2 = text2.length(); 10 | int[][] dp = new int[l1][l2]; 11 | 12 | for(int i = 0; i < l1;i++) { 13 | Arrays.fill(dp[i], -1); 14 | } 15 | return process(text1, text2, 0,0,dp); 16 | } 17 | 18 | int process (String text1, String text2, int index1, int index2 , int[][] dp) { 19 | int l1 = text1.length(); 20 | int l2 = text2.length(); 21 | if (index1 == l1 || index2 == l2) { 22 | return 0; 23 | } 24 | if (dp[index1][index2] != -1) { 25 | return dp[index1][index2]; 26 | } 27 | int res = 0; 28 | if (text1.charAt(index1) == text2.charAt(index2)) { 29 | res = process(text1, text2, index1 + 1, index2 + 1, dp) + 1; 30 | } else { 31 | int p2 = process(text1, text2, index1, index2 + 1, dp); 32 | int p3 = process(text1, text2, index1 + 1, index2, dp); 33 | res += Math.max(p2,p3); 34 | } 35 | dp[index1][index2] = res; 36 | return res; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/_206_ReverseLinkedList.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | /** 4 | * @author : loso 5 | * @version : v1.0 6 | * @date : Created in 2023/11/11 14:38 7 | * @description : 8 | * @modified By : loso 9 | */ 10 | public class _206_ReverseLinkedList { 11 | 12 | public ListNode reverseList(ListNode head) { 13 | ListNode nextNode ; 14 | ListNode lastNode = null; 15 | while ( head != null) { 16 | nextNode = head.next; 17 | head.next = lastNode; 18 | lastNode = head; 19 | head = nextNode; 20 | } 21 | return lastNode; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/backTrackingTemplate.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | /** 4 | * @author : loso 5 | * @version : v1.0 6 | * @date : Created in 2024/3/12 10:14 7 | * @description : 8 | * @modified By : loso 9 | */ 10 | @SuppressWarnings("all") 11 | public class backTrackingTemplate { 12 | 13 | boolean 终止条件 = true; 14 | 15 | void 添加结果() { 16 | } 17 | 18 | class 解空间 { 19 | } 20 | 21 | void 处理节点() { 22 | } 23 | 24 | void 回溯() { 25 | } 26 | 27 | Object[] 解空间 = new Object[0]; 28 | 29 | void process(Object 路径, Object 选择列表) { 30 | if (终止条件) { 31 | 添加结果(); 32 | return; 33 | } 34 | for ( Object 解 : 解空间 ) { 35 | 处理节点(); 36 | process(路径, 选择列表); 37 | 回溯();//撤销处理结果 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/cannedmitang/hot100/binarySearchTemplate.java: -------------------------------------------------------------------------------- 1 | package com.cannedmitang.hot100; 2 | 3 | /** 4 | * @author : loso 5 | * @version : v1.0 6 | * @date : Created in 2024/3/20 18:30 7 | * @description : 8 | * @modified By : loso 9 | */ 10 | @SuppressWarnings("all") 11 | public class binarySearchTemplate { 12 | 13 | public static int findNum(int[] arr, int num) { 14 | int l = 0; 15 | int r = arr.length - 1; 16 | int m = 0; 17 | int res = -1; 18 | while (l <= r) { 19 | m = l + ((r - l) >> 1); 20 | if (arr[m] == num) { 21 | //⭐️ 处理相等 22 | } else if (arr[m] > num) { 23 | //⭐️ 处理大于 24 | r = m - 1; 25 | } else if (arr[m] < num) { 26 | //⭐️ 处理大于 27 | l = m + 1; 28 | } 29 | } 30 | return res; 31 | } 32 | 33 | 34 | /** 35 | * 寻找指定数据 36 | * @param arr 37 | * @param num 38 | * @return 39 | */ 40 | public static int find(int[] arr, int num) { 41 | int l = 0; 42 | int r = arr.length - 1; 43 | int m = 0; 44 | int res = -1; 45 | while (l <= r) { 46 | m = l + ((r - l) >> 1); 47 | if (arr[m] == num) { 48 | return m ; 49 | } else if (arr[m] > num) { 50 | r = m - 1; 51 | } else if (arr[m] < num) { 52 | l = m + 1; 53 | } 54 | } 55 | return res; 56 | } 57 | 58 | /** 59 | * 寻找指定数据中>num最左位置 60 | * @param arr 61 | * @param num 62 | * @return 63 | */ 64 | public static int findLeft(int[] arr, int num) { 65 | int l = 0; 66 | int r = arr.length - 1; 67 | int m = 0; 68 | int res = -1; 69 | while (l <= r) { 70 | m = l + ((r - l) >> 1); 71 | if (arr[m] == num) { 72 | l = m + 1; 73 | } else if (arr[m] > num) { 74 | res = m; 75 | r = m - 1; 76 | } else if (arr[m] < num) { 77 | l = m + 1; 78 | } 79 | } 80 | return res; 81 | } 82 | 83 | /** 84 | * 寻找指定数据中>=num最左位置 85 | * @param arr 86 | * @param num 87 | * @return 88 | */ 89 | public static int findLeftEq(int[] arr, int num) { 90 | int l = 0; 91 | int r = arr.length - 1; 92 | int m = 0; 93 | int res = -1; 94 | while (l <= r) { 95 | m = l + ((r - l) >> 1); 96 | if (arr[m] == num) { 97 | res = m; 98 | r = m - 1; 99 | } else if (arr[m] > num) { 100 | res = m; 101 | r = m - 1; 102 | } else if (arr[m] < num) { 103 | l = m + 1; 104 | } 105 | } 106 | return res; 107 | } 108 | 109 | /** 110 | * 寻找指定数据中> 1); 122 | if (arr[m] == num) { 123 | r = m - 1; 124 | } else if (arr[m] > num){ 125 | r = m - 1; 126 | } else if (arr[m] < num) { 127 | res = m; 128 | l = m + 1; 129 | } 130 | } 131 | return res; 132 | } 133 | 134 | /** 135 | * 寻找指定数据中<=num最右位置 136 | * @param arr 137 | * @param num 138 | * @return 139 | */ 140 | public static int findRightEq(int[] arr, int num) { 141 | int l = 0; 142 | int r = arr.length - 1; 143 | int m = 0; 144 | int res = -1; 145 | while (l <= r) { 146 | m = l + ((r - l) >> 1); 147 | if (arr[m] == num) { 148 | res = m; 149 | l = m + 1; 150 | } else if (arr[m] > num){ 151 | r = m - 1; 152 | } else if (arr[m] < num) { 153 | res = m; 154 | l = m + 1; 155 | } 156 | } 157 | return res; 158 | } 159 | 160 | } 161 | --------------------------------------------------------------------------------