├── 0001.TwoSum.java ├── 0002.AddTwoNumbers.java ├── 0003.LongestSubstringWithoutRepeatingCharacters.java ├── 0005.LongestPalindromicSubstring.java ├── 0015.3Sum.java ├── 0019.RemoveNthNodeFromEndofList.java ├── 0020.ValidParentheses.java ├── 0021.MergeTwoSortedLists.java ├── 0024.SwapNodesinPairs.java ├── 0049.GroupAnagrams.java ├── 0053.MaximumSubarray.java ├── 0054.SpiralMatrix.java ├── 0055.JumpGame.java ├── 0056.MergeIntervals.java ├── 0062.UniquePaths.java ├── 0066.PlusOne.java ├── 0070.ClimbingStairs.java ├── 0073.SetMatrixZeroes.java ├── 0083.RemoveDuplicatesfromSortedList.java ├── 0092.ReverseLinkedListII.java ├── 0121.BestTimetoBuyandSellStock.java ├── 0122.BestTimetoBuyandSellStockII.java ├── 0123.BestTimetoBuyandSellStockIII.java ├── 0125.ValidPalindrome.java ├── 0134.GasStation.java ├── 0141.LinkedListCycle.java ├── 0142.LinkedListCycleII.java ├── 0152.MaximumProductSubarray.java ├── 0153.FindMinimuminRotatedSortedArray.java ├── 0160.IntersectionofTwoLinkedLists.java ├── 0187.RepeatedDNASequences.java ├── 0188.BestTimetoBuyandSellStockIV.java ├── 0198.HouseRobber.java ├── 0200.NumberofIslands.java ├── 0206.ReverseLinkedList.java ├── 0217.ContainsDuplicate.java ├── 0219.ContainsDuplicateII.java ├── 0238.ProductofArrayExceptSelf.java ├── 0242.ValidAnagram.java ├── 0283.MoveZeroes.java ├── 0328.OddEvenLinkedList.java ├── 0349.IntersectionofTwoArrays.java ├── 0419.BattleshipsinaBoard.java ├── 0445.AddTwoNumbersII.java ├── 0509.FibonacciNumber.java ├── 0680.ValidPalindromeII.java ├── 0695.MaxAreaofIsland.java ├── 0704.BinarySearch.java ├── 0733.FloodFill.java ├── 0796.RotateString.java ├── 0836.RectangleOverlap.java ├── 0876.MiddleoftheLinkedList.java ├── 0904.FruitIntoBaskets.java ├── 0905.SortArrayByParity.java └── 0922.SortArrayByParityII.java /0001.TwoSum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | Map map = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | int complement = target - nums[i]; 6 | if (map.containsKey(complement) && map.get(complement) != i) { 7 | return new int[] { i, map.get(complement) }; 8 | } else { 9 | map.put(nums[i], i); 10 | } 11 | 12 | } 13 | return new int[2]; 14 | } 15 | } -------------------------------------------------------------------------------- /0002.AddTwoNumbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 3 | ListNode dummy = new ListNode(0); 4 | ListNode curr = dummy; 5 | int carry = 0; 6 | while (l1 != null || l2 != null) { 7 | int sum = 0; 8 | if (l1 != null) { 9 | sum += l1.val; 10 | l1 = l1.next; 11 | } 12 | if (l2 != null) { 13 | sum += l2.val; 14 | l2 = l2.next; 15 | } 16 | sum += carry; 17 | curr.next = new ListNode(sum % 10); 18 | carry = sum / 10; 19 | curr = curr.next; 20 | } 21 | if (carry > 0) { 22 | curr.next = new ListNode(carry); 23 | } 24 | return dummy.next; 25 | } 26 | } -------------------------------------------------------------------------------- /0003.LongestSubstringWithoutRepeatingCharacters.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLongestSubstring(String s) { 3 | HashSet set = new HashSet<>(); 4 | int j = 0, maxLength = 0; 5 | if (s == null || s.length() == 0) { 6 | return 0; 7 | } 8 | for (int i = 0; i < s.length(); i++) { 9 | if (!set.contains(s.charAt(i))) { 10 | set.add(s.charAt(i)); 11 | maxLength = Math.max(maxLength, set.size()); 12 | } else { 13 | while (set.contains(s.charAt(i))) { 14 | set.remove(s.charAt(j)); 15 | j++; 16 | } 17 | 18 | set.add(s.charAt(i)); 19 | } 20 | } 21 | 22 | return maxLength; 23 | } 24 | } -------------------------------------------------------------------------------- /0005.LongestPalindromicSubstring.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private int start = 0, maxLength = 1; 3 | 4 | public String longestPalindrome(String s) { 5 | if (s.length() < 2) { 6 | return s; 7 | } 8 | 9 | for (int i = 0; i < s.length(); i++) { 10 | expandAroundCenter(s, i - 1, i + 1); 11 | expandAroundCenter(s, i, i + 1); 12 | } 13 | 14 | return s.substring(start, start + maxLength); 15 | } 16 | 17 | private void expandAroundCenter(String s, int left, int right) { 18 | while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { 19 | if (right - left + 1 > maxLength) { 20 | maxLength = right - left + 1; 21 | start = left; 22 | } 23 | left--; 24 | right++; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /0015.3Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> threeSum(int[] nums) { 3 | ArrayList> result = new ArrayList>(); 4 | Arrays.sort(nums); 5 | 6 | for (int i = 0; i < nums.length - 2; i++) { 7 | if (i == 0 || nums[i] != nums[i - 1]) { 8 | int start = i + 1, end = nums.length - 1; 9 | while (start < end) { 10 | // 3 situations: equal, greater than, or less than. 11 | if (nums[i] + nums[start] + nums[end] == 0) { 12 | result.add(Arrays.asList(nums[i], nums[start], nums[end])); 13 | start++; 14 | end--; 15 | while (start < end && nums[start] == nums[start - 1]) { 16 | start++; 17 | } 18 | while (start < end && nums[end] == nums[end + 1]) { 19 | end--; 20 | } 21 | } else if (nums[i] + nums[start] + nums[end] < 0) { 22 | start++; 23 | } else { 24 | end--; 25 | } 26 | 27 | } 28 | } 29 | } 30 | return result; 31 | } 32 | } -------------------------------------------------------------------------------- /0019.RemoveNthNodeFromEndofList.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode removeNthFromEnd(ListNode head, int n) { 3 | ListNode dummy = new ListNode(0); 4 | dummy.next = head; 5 | ListNode n1 = dummy; 6 | ListNode n2 = dummy; 7 | for (int i = 0; i <= n; i++) { 8 | n2 = n2.next; 9 | } 10 | while (n2 != null) { 11 | n1 = n1.next; 12 | n2 = n2.next; 13 | } 14 | n1.next = n1.next.next; 15 | return dummy.next; 16 | } 17 | } -------------------------------------------------------------------------------- /0020.ValidParentheses.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public boolean isValid(String s) { 4 | HashMap mappings = new HashMap(); 5 | mappings.put('(', ')'); 6 | mappings.put('[', ']'); 7 | mappings.put('{', '}'); 8 | 9 | Stack stack = new Stack(); 10 | 11 | for (int i = 0; i < s.length(); i++) { 12 | char c = s.charAt(i); 13 | if (mappings.containsKey(c)) { 14 | stack.push(mappings.get(c)); 15 | } else { 16 | if (stack.size() == 0 || stack.pop() != c) { 17 | return false; 18 | } 19 | } 20 | } 21 | 22 | if (stack.size() != 0) { 23 | return false; 24 | } 25 | 26 | return true; 27 | } 28 | } -------------------------------------------------------------------------------- /0021.MergeTwoSortedLists.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 3 | ListNode curr = new ListNode(0); 4 | ListNode dummy = curr; 5 | while(l1!=null && l2!=null){ 6 | if(l1.val> groupAnagrams(String[] strs) { 3 | if (strs.length == 0) { 4 | return new ArrayList(); 5 | } 6 | HashMap map = new HashMap(); 7 | int[] characters = new int[26]; 8 | for (String str : strs) { 9 | Arrays.fill(characters, 0); 10 | for (int i = 0; i < str.length(); i++) { 11 | int ascii = str.charAt(i) - 97; 12 | characters[ascii]++; 13 | } 14 | StringBuilder sb = new StringBuilder(""); 15 | for (int i = 0; i < 26; i++) { 16 | sb.append(characters[i]); 17 | } 18 | String key = sb.toString(); 19 | if (!map.containsKey(key)) { 20 | map.put(key, new ArrayList<>()); 21 | } 22 | map.get(key).add(str); 23 | } 24 | 25 | return new ArrayList(map.values()); 26 | } 27 | } -------------------------------------------------------------------------------- /0053.MaximumSubarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int memo[] = new int[nums.length]; 4 | memo[0] = nums[0]; 5 | 6 | for (int i = 1; i < nums.length; i++) { 7 | memo[i] = Math.max(nums[i] + memo[i - 1], nums[i]); 8 | } 9 | 10 | int max = nums[0]; 11 | 12 | for (int i = 1; i < memo.length; i++) { 13 | max = Math.max(max, memo[i]); 14 | } 15 | 16 | return max; 17 | } 18 | } -------------------------------------------------------------------------------- /0054.SpiralMatrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List spiralOrder(int[][] matrix) { 3 | List result = new ArrayList<>(); 4 | if (matrix.length == 0) { 5 | return result; 6 | } 7 | 8 | int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1; 9 | 10 | String direction = "right"; 11 | 12 | /* 13 | * [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] 14 | */ 15 | while (left <= right && top <= bottom) { 16 | if (direction == "right") { 17 | for (int i = left; i <= right; i++) { 18 | result.add(matrix[top][i]); 19 | } 20 | top++; 21 | direction = "down"; 22 | } else if (direction == "down") { 23 | for (int i = top; i <= bottom; i++) { 24 | result.add(matrix[i][right]); 25 | } 26 | right--; 27 | direction = "left"; 28 | } else if (direction == "left") { 29 | for (int i = right; i >= left; i--) { 30 | result.add(matrix[bottom][i]); 31 | } 32 | bottom--; 33 | direction = "top"; 34 | } else if (direction == "top") { 35 | for (int i = bottom; i >= top; i--) { 36 | result.add(matrix[i][left]); 37 | } 38 | left++; 39 | direction = "right"; 40 | } 41 | } 42 | return result; 43 | } 44 | } -------------------------------------------------------------------------------- /0055.JumpGame.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int maxJump = nums.length-1; 4 | for(int i=nums.length-2;i>=0;i--){ 5 | if(i+nums[i] >= maxJump){ 6 | maxJump = i; 7 | } 8 | } 9 | 10 | return maxJump == 0; 11 | } 12 | } -------------------------------------------------------------------------------- /0056.MergeIntervals.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] merge(int[][] intervals) { 3 | if (intervals.length < 2) { 4 | return intervals; 5 | } 6 | Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0])); 7 | 8 | int[] curr = intervals[0]; 9 | ArrayList result = new ArrayList<>(); 10 | 11 | for (int[] interval : intervals) { 12 | if (curr[1] >= interval[0]) { 13 | curr[1] = Math.max(curr[1], interval[1]); 14 | } else { 15 | result.add(curr); 16 | curr = interval; 17 | } 18 | } 19 | 20 | if (curr.length != 0) { 21 | result.add(curr); 22 | } 23 | 24 | return result.toArray(new int[result.size()][]); 25 | } 26 | } -------------------------------------------------------------------------------- /0062.UniquePaths.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int memo[][] = new int[n][m]; 4 | for(int row=0;row=0;i--){ 4 | if(digits[i]!=9){ 5 | digits[i]++; 6 | return digits; 7 | }else{ 8 | digits[i] = 0; 9 | } 10 | } 11 | 12 | int[] result = new int [digits.length+1]; 13 | result[0] = 1; 14 | return result; 15 | } 16 | } -------------------------------------------------------------------------------- /0070.ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | if(n == 1 || n==2){ 4 | return n; 5 | } 6 | 7 | int[] memo = new int[n+1]; 8 | memo[1] = 1; 9 | memo[2] = 2; 10 | for(int i=3;i<=n;i++){ 11 | memo[i] = memo[i-1] + memo[i-2]; 12 | } 13 | 14 | return memo[n]; 15 | } 16 | } -------------------------------------------------------------------------------- /0073.SetMatrixZeroes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void setZeroes(int[][] matrix) { 3 | boolean firstColHasZero = false; 4 | boolean firstRowHasZero = false; 5 | // 检查第一列是否有0 6 | for (int i = 0; i < matrix.length; i++) { 7 | if (matrix[i][0] == 0) { 8 | firstColHasZero = true; 9 | } 10 | } 11 | // 检查第一行是否有0 12 | for (int i = 0; i < matrix[0].length; i++) { 13 | if (matrix[0][i] == 0) { 14 | firstRowHasZero = true; 15 | } 16 | } 17 | // 使用第一行和第一列,来标记其余行列是否含有0 18 | for (int row = 1; row < matrix.length; row++) { 19 | for (int col = 1; col < matrix[0].length; col++) { 20 | if (matrix[row][col] == 0) { 21 | matrix[row][0] = 0; 22 | matrix[0][col] = 0; 23 | } 24 | } 25 | } 26 | // 接下来,利用第一行和第一列的标0情况,将matrix中的数字标0 27 | for (int row = 1; row < matrix.length; row++) { 28 | for (int col = 1; col < matrix[0].length; col++) { 29 | if (matrix[row][0] == 0 || matrix[0][col] == 0) { 30 | matrix[row][col] = 0; 31 | } 32 | } 33 | } 34 | // 最后,处理第一行和第一列 35 | // 如果firstColHasZero等于true,将第一列全设为0 36 | if (firstColHasZero) { 37 | for (int i = 0; i < matrix.length; i++) { 38 | matrix[i][0] = 0; 39 | } 40 | } 41 | // 如果firstRowHasZero等于true,将第一行全设为0 42 | if (firstRowHasZero) { 43 | for (int i = 0; i < matrix[0].length; i++) { 44 | matrix[0][i] = 0; 45 | } 46 | } 47 | return ; 48 | } 49 | } -------------------------------------------------------------------------------- /0083.RemoveDuplicatesfromSortedList.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode deleteDuplicates(ListNode head) { 3 | ListNode current = head; 4 | while(current != null && current.next != null){ 5 | if(current.val == current.next.val){ 6 | current.next = current.next.next; 7 | }else{ 8 | current = current.next; 9 | } 10 | } 11 | return head; 12 | } 13 | } -------------------------------------------------------------------------------- /0092.ReverseLinkedListII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode reverseBetween(ListNode head, int m, int n) { 3 | ListNode prev = null; 4 | ListNode curr = head; 5 | ListNode next = head; 6 | for(int i=1; i maxProfit) { 14 | maxProfit = prices[i] - minPrice; 15 | } 16 | } 17 | 18 | return maxProfit; 19 | } 20 | } -------------------------------------------------------------------------------- /0122.BestTimetoBuyandSellStockII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | if(prices.length == 0){ 4 | return 0; 5 | } 6 | int profit = 0, valley = prices[0], peak = prices[0]; 7 | int i = 0; 8 | while (i < prices.length - 1) { 9 | while (i < prices.length - 1 && prices[i] >= prices[i + 1]) 10 | i++; 11 | valley = prices[i]; 12 | while (i < prices.length - 1 && prices[i] <= prices[i + 1]) 13 | i++; 14 | peak = prices[i]; 15 | profit += peak - valley; 16 | } 17 | return profit; 18 | } 19 | } -------------------------------------------------------------------------------- /0123.BestTimetoBuyandSellStockIII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | if(prices.length == 0){ 4 | return 0; 5 | } 6 | int dp[][] = new int[3][prices.length]; 7 | 8 | for(int i=1;i<3;i++){ 9 | int maxProfit = -prices[0]; 10 | for(int j=1; j< prices.length;j++){ 11 | dp[i][j] = Math.max(dp[i][j-1], prices[j]+maxProfit); 12 | maxProfit = Math.max(maxProfit, dp[i-1][j] - prices[j]); 13 | } 14 | } 15 | 16 | return dp[2][prices.length-1]; 17 | } 18 | } -------------------------------------------------------------------------------- /0125.ValidPalindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(String s) { 3 | if(s.isEmpty()){ 4 | return true; 5 | } 6 | 7 | int left = 0; 8 | int right = s.length()-1; 9 | 10 | while(left nums[0]) { 10 | return nums[0]; 11 | } 12 | 13 | while (left < right) { 14 | int mid = left + (right - left) / 2; 15 | 16 | if (nums[mid] > nums[mid + 1]) { 17 | return nums[mid + 1]; 18 | } 19 | 20 | if (nums[mid - 1] > nums[mid]) { 21 | return nums[mid]; 22 | } 23 | 24 | if (nums[mid] > nums[left]) { 25 | left = mid + 1; 26 | } else { 27 | right = mid - 1; 28 | } 29 | } 30 | return nums[left]; 31 | } 32 | } -------------------------------------------------------------------------------- /0160.IntersectionofTwoLinkedLists.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 3 | ListNode n1 = headA; 4 | ListNode n2 = headB; 5 | while(n1!=n2){ 6 | if(n1==null){ 7 | n1 = headB; 8 | }else{ 9 | n1 = n1.next; 10 | } 11 | if(n2==null){ 12 | n2 = headA; 13 | }else{ 14 | n2 = n2.next; 15 | } 16 | } 17 | return n1; 18 | } 19 | } -------------------------------------------------------------------------------- /0187.RepeatedDNASequences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findRepeatedDnaSequences(String s) { 3 | Set set = new HashSet(); 4 | Set result = new HashSet(); 5 | int i = 0; 6 | while(i+10<=s.length()){ 7 | String dna = s.substring(i,i+10); 8 | if(set.contains(dna)){ 9 | result.add(dna); 10 | }else{ 11 | set.add(dna); 12 | } 13 | 14 | i++; 15 | } 16 | 17 | return new ArrayList(result); 18 | } 19 | } -------------------------------------------------------------------------------- /0188.BestTimetoBuyandSellStockIV.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int k, int[] prices) { 3 | if(prices.length == 0){ 4 | return 0; 5 | } 6 | int dp[][] = new int[k+1][prices.length]; 7 | for(int i=1;i= grid.length || col <0 || col >=grid[0].length || grid[row][col] == '0'){ 21 | return; 22 | } 23 | 24 | grid[row][col] = '0'; 25 | dfs(grid, row, col-1); 26 | dfs(grid, row, col+1); 27 | dfs(grid, row-1, col); 28 | dfs(grid, row+1, col); 29 | } 30 | } -------------------------------------------------------------------------------- /0206.ReverseLinkedList.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode reverseList(ListNode head) { 3 | ListNode prev = null; 4 | ListNode curr = head; 5 | ListNode next = null; 6 | while(curr !=null){ 7 | next = curr.next; 8 | curr.next = prev; 9 | prev = curr; 10 | curr = next; 11 | } 12 | return prev; 13 | } 14 | } -------------------------------------------------------------------------------- /0217.ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsDuplicate(int[] nums) { 3 | Set set = new HashSet(); 4 | for(int i=0;i map = new HashMap(); 4 | 5 | for(int i=0;i=0;i--){ 15 | result[i] = result[i] * product; 16 | product = product * nums[i]; 17 | } 18 | 19 | return result; 20 | } 21 | } -------------------------------------------------------------------------------- /0242.ValidAnagram.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isAnagram(String s, String t) { 3 | if (s.length() != t.length()) { 4 | return false; 5 | } 6 | 7 | Map map = new HashMap(); 8 | for (int i = 0; i < s.length(); i++) { 9 | char sChar = s.charAt(i); 10 | if (map.containsKey(sChar)) { 11 | map.put(sChar, map.get(sChar) + 1); 12 | } else { 13 | map.put(sChar, 1); 14 | } 15 | char tChar = t.charAt(i); 16 | if (map.containsKey(tChar)) { 17 | map.put(tChar, map.get(tChar) - 1); 18 | } else { 19 | map.put(tChar, -1); 20 | } 21 | } 22 | 23 | for (int count : map.values()) { 24 | if (count != 0) { 25 | return false; 26 | } 27 | } 28 | 29 | return true; 30 | } 31 | } -------------------------------------------------------------------------------- /0283.MoveZeroes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void moveZeroes(int[] nums) { 3 | int nonZeroIndex = 0; 4 | 5 | for(int i=0;i set = new HashSet(); 4 | 5 | Set result = new HashSet(); 6 | 7 | for(int num : nums1){ 8 | set.add(num); 9 | } 10 | 11 | for(int num : nums2){ 12 | if(set.contains(num)){ 13 | result.add(num); 14 | } 15 | } 16 | int[] resultArr = new int[result.size()]; 17 | int i = 0; 18 | for (int num : result) { 19 | resultArr[i++] = num; 20 | } 21 | return resultArr; 22 | } 23 | } -------------------------------------------------------------------------------- /0419.BattleshipsinaBoard.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countBattleships(char[][] board) { 3 | int result = 0; 4 | for(int row=0;row=board.length || col<0 || col>=board[0].length || board[row][col] !='X'){ 17 | return; 18 | } 19 | board[row][col] = '.'; 20 | dfs(board,row-1,col); 21 | dfs(board,row+1,col); 22 | dfs(board,row,col-1); 23 | dfs(board,row,col+1); 24 | } 25 | } -------------------------------------------------------------------------------- /0445.AddTwoNumbersII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 3 | Stack stack1 = new Stack(); 4 | Stack stack2 = new Stack(); 5 | while(l1 != null){ 6 | stack1.push(l1.val); 7 | l1 = l1.next; 8 | } 9 | while(l2 != null){ 10 | stack2.push(l2.val); 11 | l2 = l2.next; 12 | } 13 | 14 | int carry = 0; 15 | ListNode curr = null; 16 | 17 | while(!stack1.empty() || !stack2.empty()){ 18 | int sum = 0; 19 | if(!stack1.empty()){ 20 | sum += stack1.pop(); 21 | } 22 | if(!stack2.empty()){ 23 | sum += stack2.pop(); 24 | } 25 | 26 | sum += carry; 27 | 28 | ListNode node = new ListNode(sum % 10); 29 | carry = sum / 10; 30 | 31 | node.next = curr; 32 | curr = node; 33 | } 34 | 35 | if(carry != 0){ 36 | ListNode node = new ListNode(carry); 37 | node.next = curr; 38 | curr = node; 39 | } 40 | 41 | return curr; 42 | } 43 | } -------------------------------------------------------------------------------- /0509.FibonacciNumber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fib(int N) { 3 | if (N <= 1) { 4 | return N; 5 | } 6 | 7 | int prev2 = 0; 8 | int prev1 = 1; 9 | int result = 0; 10 | for(int i=2;i<=N;i++){ 11 | result = prev1 +prev2; 12 | prev2 = prev1; 13 | prev1 = result; 14 | } 15 | 16 | return result; 17 | } 18 | } -------------------------------------------------------------------------------- /0680.ValidPalindromeII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validPalindrome(String s) { 3 | int left = 0, right = s.length()-1; 4 | while(left < right){ 5 | if(s.charAt(left) != s.charAt(right)){ 6 | boolean result = isPalindrome(s, left+1, right) || isPalindrome(s, left, right-1); 7 | return result; 8 | } 9 | left++; 10 | right--; 11 | } 12 | 13 | return true; 14 | } 15 | 16 | public boolean isPalindrome(String s, int left, int right){ 17 | while(left < right){ 18 | if(s.charAt(left) != s.charAt(right)){ 19 | return false; 20 | } 21 | left++; 22 | right--; 23 | } 24 | return true; 25 | } 26 | } -------------------------------------------------------------------------------- /0695.MaxAreaofIsland.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxAreaOfIsland(int[][] grid) { 3 | int max = 0; 4 | for(int row=0;row=grid.length || col<0 || col>=grid[0].length || grid[row][col] == 0){ 17 | return 0; 18 | } 19 | 20 | grid[row][col] = 0; 21 | int count = 1; 22 | count += dfs(grid,row-1,col); 23 | count += dfs(grid,row+1,col); 24 | count += dfs(grid,row,col-1); 25 | count += dfs(grid,row,col+1); 26 | return count; 27 | } 28 | } -------------------------------------------------------------------------------- /0704.BinarySearch.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int left = 0, right = nums.length - 1; 4 | while (left <= right) { 5 | int mid = left + (right - left) / 2; 6 | if (nums[mid] == target) { 7 | return mid; 8 | } else if (target < nums[mid]) { 9 | right = mid - 1; 10 | } else { 11 | left = mid + 1; 12 | } 13 | } 14 | 15 | return -1; 16 | } 17 | } -------------------------------------------------------------------------------- /0733.FloodFill.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { 3 | if(image[sr][sc] == newColor){ 4 | return image; 5 | } 6 | int oldColor = image[sr][sc]; 7 | dfs(image, newColor, oldColor, sr, sc); 8 | return image; 9 | } 10 | 11 | public void dfs(int[][] image, int newColor, int oldColor, int sr, int sc){ 12 | if(sr<0 || sr>=image.length || sc<0 || sc >= image[0].length || image[sr][sc] != oldColor){ 13 | return; 14 | } 15 | image[sr][sc] = newColor; 16 | dfs(image,newColor,oldColor,sr-1,sc); 17 | dfs(image,newColor,oldColor,sr+1,sc); 18 | dfs(image,newColor,oldColor,sr,sc-1); 19 | dfs(image,newColor,oldColor,sr,sc+1); 20 | } 21 | } -------------------------------------------------------------------------------- /0796.RotateString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean rotateString(String A, String B) { 3 | if(A.length() != B.length()){ 4 | return false; 5 | } 6 | 7 | String str = A + A; 8 | 9 | return str.contains(B); 10 | } 11 | } -------------------------------------------------------------------------------- /0836.RectangleOverlap.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isRectangleOverlap(int[] rec1, int[] rec2) { 3 | if(rec1[2] <= rec2[0] || rec1[1] >= rec2[3] || rec1[0] >= rec2[2] || rec1[3] <= rec2[1]){ 4 | return false; 5 | }else{ 6 | return true; 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /0876.MiddleoftheLinkedList.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode middleNode(ListNode head) { 3 | ListNode slow = head; 4 | ListNode fast = head; 5 | while(fast != null && fast.next != null){ 6 | slow = slow.next; 7 | fast = fast.next.next; 8 | } 9 | 10 | return slow; 11 | } 12 | } -------------------------------------------------------------------------------- /0904.FruitIntoBaskets.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int totalFruit(int[] tree) { 3 | int max = 1; 4 | Map map = new HashMap(); 5 | int j=0; 6 | 7 | for(int i=0;i2){ 10 | int minIndex = tree.length - 1; 11 | for(int fruit : map.values()){ 12 | if(fruit < minIndex){ 13 | minIndex = fruit; 14 | } 15 | } 16 | 17 | map.remove(tree[minIndex]); 18 | j = minIndex+1; 19 | } 20 | 21 | max = Math.max(max, i-j+1); 22 | } 23 | 24 | 25 | return max; 26 | } 27 | } -------------------------------------------------------------------------------- /0905.SortArrayByParity.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortArrayByParity(int[] A) { 3 | int i=0, j = A.length - 1; 4 | while(i < j){ 5 | if(A[i] % 2 == 1 && A[j] % 2 == 0){ 6 | int temp = A[i]; 7 | A[i] = A[j]; 8 | A[j] = temp; 9 | } 10 | 11 | if(A[i]%2 == 0){ 12 | i++; 13 | } 14 | if(A[j]%2 == 1){ 15 | j--; 16 | } 17 | 18 | } 19 | 20 | return A; 21 | } 22 | } -------------------------------------------------------------------------------- /0922.SortArrayByParityII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortArrayByParityII(int[] A) { 3 | int j = 1; 4 | for(int i=0; i