Last updated on 2023年6月7日 晚上
350.两个数组的交集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Solution { public int [] intersect (int [] nums1, int [] nums2) { int [] record1 = new int [1001 ]; int [] record2 = new int [1001 ]; int nums1Length = nums1.length; int nums2Length = nums2.length; for (int i = 0 ; i < nums1Length;i++){ record1[nums1[i]]++; } for (int i = 0 ; i< nums2Length;i++){ record2[nums2[i]]++; } List<Integer > tmp = new ArrayList<Integer >(); int n = Math.max(record1.length, record2.length); for (int i = 0 ;i < n;i++){ if (record1[i] > 0 && record2[i] > 0 ){ int m = Math.min(record1[i], record2[i]); for (int j = 0 ;j < m;j++){ tmp.add (i); } } } int [] result = new int [tmp.size()]; for (int i = 0 ; i < tmp.size();i++){ result[i] = tmp.get (i); } return result; } }
202.快乐数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class Solution { int getSum(int n){ int sum = 0 ; while (n != 0 ){ sum += (n % 10 ) * (n % 10 ); n = n / 10 ; } return sum ; } public boolean isHappy(int n) { Map<Integer, Integer> dict = new HashMap<>(); while (true ){ int currSum = getSum(n); if (currSum == 1 ){ return true ; } if (dict.containsKey(currSum) && dict.get(currSum) != 0 ){ return false ; }else { dict.put(currSum, 1 ); } n = currSum; } } }
454.四数相加II 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public int fourSumCount (int [] nums1, int [] nums2, int [] nums3, int [] nums4) { Map<Integer,Integer> map = new HashMap<>(); for (int i = 0 ;i < nums1.length;i++){ for (int j = 0 ; j < nums2.length;j++){ map.put (nums1[i] + nums2[j], map.getOrDefault (nums1[i] + nums2[j], 0 ) + 1 ); } } int count = 0 ; for (int i = 0 ; i < nums3.length;i++){ for (int j = 0 ;j < nums4.length;j++){ count += map.getOrDefault (0 - (nums3[i] + nums4[j]), 0 ); } } return count; } }
15.三数之和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 class Solution { public List<List<Integer>> threeSum(int [] nums) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(nums); if (nums[0 ] > 0 ) return result; int n = nums.length; for (int i = 0 ; i < n;i++){ if (nums[0 ] > 0 ){ return result; } if (i > 0 && nums[i] == nums[i - 1 ]){ continue ; } int left = i + 1 ; int right = n - 1 ; while (left < right){ if (nums[i] + nums[left] + nums[right] > 0 ){ right--; }else if (nums[i] + nums[left] + nums[right] < 0 ){ left++; }else { List<Integer> tmp = new ArrayList<>(); tmp.add (nums[i]); tmp.add (nums[left]); tmp.add (nums[right]); result.add (tmp); while (left < right && nums[right] == nums[right - 1 ]){ right--; } while (left < right && nums[left] == nums[left + 1 ]){ left++; } left++; right--; } } } return result; } }
18.四数之和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 class Solution { public List<List<Integer>> fourSum(int [] nums, int target ) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(nums); int n = nums.length; for (int i = 0 ; i < nums.length - 1 ;i ++){ if (nums[i] > target && nums[i] >= 0 ){ break ; } if (i > 0 && nums[i - 1 ] == nums[i]){ continue ; } for (int j = i + 1 ; j < nums.length;j++){ if (nums[i] + nums[j] > target && nums[i] + nums[j] >= 0 ){ break ; } if (j > i + 1 && nums[j - 1 ] == nums[j]){ continue ; } int left = j + 1 ; int right = n - 1 ; while (left < right){ if (nums[i] + nums[j] + nums[left] + nums[right] > target ){ right--; }else if (nums[i] + nums[j] + nums[left] + nums[right] < target ) { left++; }else { List<Integer> tmp = new ArrayList<>(); tmp.add(nums[i]); tmp.add(nums[j]); tmp.add(nums[left]); tmp.add(nums[right]); result.add(tmp); while (left < right && nums[right] == nums[right - 1 ]){ right--; } while (left < right && nums[left] == nums[left + 1 ]){ left++; } left++; right--; } } } } return result; } }
344.反转字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public void reverseString(char [] s) { int left = 0 ; int right = s.length - 1 ; while(left < right ){ char tmp = s[left ]; s[left ] = s[right ]; s[right ] = tmp; left ++; right --; } } }
541.反转字符串 II 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution { void reverse(char [] s_char, int begin , int end ){ while (begin < end ){ char tmp = s_char[end ]; s_char[end ] = s_char[begin ]; s_char[begin ] = tmp; begin ++; end } } public String reverseStr(String s, int k) { //将String字符串转换为char 数组 char [] s_char = s.toCharArray(); int n = s.length(); int i = 0 ; for (;i < n;i = i + 2 *k){ int start = i; int end = Math.min(n - 1 , start + k - 1 ); reverse(s_char, start , end ); } return new String(s_char); } }
剑指 Offer 05. 替换空格 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public String replaceSpace(String s ) { if (s == null){ return null; } StringBuilder sb = new StringBuilder() ; for (int i = 0 ;i < s.length() ;i++){ if (s.char At(i ) == ' ' ){ sb.append("%20" ); }else { sb.append(s.char At(i ) ); } } return sb.to String() ; } }
151.反转字符串中的单词 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 class Solution { void reverseString(StringBuilder sb , int begin , int end ) { while (begin < end ){ char tmp = sb.char At(begin ) ; sb.setCharAt(begin , sb .charAt (end ) ); sb.setCharAt(end , tmp ) ; begin ++; end --; } } public String reverseWords(String s ) { StringBuilder sb = new StringBuilder() ; int left = 0 ; int right = s.length() - 1 ; while (s.char At(left ) == ' ' ){ left++; } while (s.char At(right ) == ' ' ){ right--; } while (left <= right){ if (s.char At(left ) != ' ' || sb.char At(sb .length () - 1 ) != ' ' ){ sb.append(s.char At(left ) ); } left++; } int begin = 0 ; int end = sb.length() - 1 ; reverseString(sb , begin , end ) ; int start = 0 ; int inner_end = 1 ; int n = sb.length() ; while (start < n){ while (inner_end < n && sb.char At(inner_end ) != ' ' ){ inner_end++; } reverseString(sb , start , inner_end - 1) ; start = inner_end + 1 ; inner_end = start + 1 ; } return sb.to String() ; } }
剑指Offer58-II.左旋转字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public String reverseLeftWords(String s , int n ) { int l = s.length() ; int k = n % l; StringBuilder sb = new StringBuilder() ; for (int i = k;i < l;i++){ sb.append(s.char At(i ) ); } for (int i = 0 ;i < k;i++){ sb.append(s.char At(i ) ); } return sb.to String() ; } }
28.找出字符串中第一个匹配项的下标 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Solution { public int strStr(String haystack , String needle ) { if (haystack.length() < needle.length() ) return -1 ; int i = 0 ; int j = 0 ; int result = -1 ; while (i < haystack.length() ){ j = 0 ; result = i; if (haystack.char At(i ) == needle.char At(j ) ){ while (j < needle.length() && i < haystack.length() ){ if (haystack.char At(i ) == needle.char At(j ) ){ i++; j++; continue; }else { break; } } if (j == needle.length() ){ return result; } } i = result + 1 ; } return -1 ; } }
my-leetcode-logs-20230601
https://thewangyang.github.io/2023/06/01/leetcode-notes-20230601/