力扣熱題100
1. 兩數之和 - 力扣(LeetCode)
- 查找兩數之和是不是等于target
- 也就是我們找到一個數之后,用target將其減掉,再尋找應當對應的元素是什么
- 每找到一個數,我們就將其放在集合中,因為集合中可以去重,保證我們只遍歷過一次,然后再繼續遍歷數組,將target減去當前的數組中的值,看看已經遍歷過的數組中是不是有該值,有的話就加入返回結果。
- 沒有的話就將其加入。
思想:用集合存放遍歷過的數值,然后根據當前定位到的數值,判斷自己尋找的數值在集合中是否出現,若出現就返回結果。
class Solution {public int[] twoSum(int[] nums, int target) {int[] res = new int[2];Map<Integer,Integer> map = new HashMap<>();if(nums.length == 0 || nums == null){return res;}for(int i = 0;i < nums.length;i++){int temp = target - nums[i];if(map.containsKey(temp)){res[0] = map.get(temp);res[1] = i;}else{map.put(nums[i],i);}}return res;}
}
49. 字母異位詞分組 - 力扣(LeetCode)
思想:如果可以重組后構成一個單詞,那么可以把字符串先轉換成字符數組,然后對字符數組中的字母進行排序。將排序過后的字符數組轉換成字符串,然后再作為鍵出現。然后把鍵一樣的值添加到該鍵對應的列表當中即可。如果沒有改鍵,那就創建一個新的鏈表,并同時把鍵值對插入進去。
class Solution {public List<List<String>> groupAnagrams(String[] strs) {Map<String,List<String>> map = new HashMap<>();for(String str : strs){char[] array = str.toCharArray();Arrays.sort(array);String key = new String(array);List<String> list = map.getOrDefault(key,new ArrayList<>());list.add(str);map.put(key,list);}return new ArrayList<List<String>>(map.values());}
}
128. 最長連續序列 - 力扣(LeetCode)
思想:遍歷該數組,當遍歷到的元素,在該集合中有前驅的時候就跳過。也就是說,遍歷到的數字必須是該序列中第一個打頭的才進行處理和操作。并且要記錄是當前數字打頭的連續序列更長還是已經記錄的舊的更長。
class Solution {public int longestConsecutive(int[] nums) {Set<Integer> sets = new HashSet<>();for(int num : nums){sets.add(num);}int maxLong = 0;for(int set : sets ){int num = set;if(!sets.contains(num - 1)){int curLong = 1;while(sets.contains(num+1)){num++;curLong++;}maxLong = Math.max(curLong,maxLong);}}return maxLong;}
}
283. 移動零 - 力扣(LeetCode)
思想:使用雙指針法進行求解,左指針先不動,指向已經處理好的序列的尾部,右指針尋找不為零的時候,如果不為零,就和left指向的元素進行交換。到最后,left之前的數組就都是不為零的,且位置沒有被改變。
class Solution {public void moveZeroes(int[] nums) {int left = 0, right = 0,len = nums.length;while(right < len){if(nums[right] != 0){int temp = nums[right];nums[right] = nums[left];nums[left++] = temp;}right++;}}
}
餓了嗎2025.3.21
1.求V字形的數組,也就是連續的三個數字,中間是凹進去的。
package dlut.com.java;import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int[] arr = new int[n];int count = 0;for(int i = 0;i < n;i++){arr[i] = scanner.nextInt();}for(int i = 1;i < n-1;i++){if(arr[i]<arr[i-1] && arr[i]<arr[i+1]){count++;}}System.out.println(count);}
}
2.第一個數字n:是字符串的長度,第二個數字g:是相差應該小于等于的,若數組的位置下標奇偶性相同,且滿足相差小于等于g那么就是有緣分的位置對數。
package dlut.com.java;import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int g = scanner.nextInt();String s = scanner.next();int[] count1 = new int[26];int[] count2 = new int[26];char[] c = s.toCharArray();for(int i = 0;i < n;i++){if(i%2==0){//記錄中間差幾個元素count1[c[i] - 'a']++;}else{count2[c[i] - 'a']++;}}//count的下標就是和a相差幾個元素for(int i = 0;i<26;i++){if(count1[i] != 0)System.out.println("count1["+i+"]="+count1[i]);}for(int i = 0;i<26;i++){if(count2[i] != 0)System.out.println("count2["+i+"]="+count2[i]);}long sum = 0;for(int i = 0;i < 26;i++){sum += (long)count1[i]*(count1[i]-1)/2;for(int j = i+1;j<26;j++){if(Math.abs(j - i - 1)<=g){sum +=(long) count1[i]*count1[j];}}}for(int i = 0;i < 26;i++){sum += (long)count2[i]*(count2[i]-1)/2;for(int j = i+1;j<26;j++){if(Math.abs(j - i - 1)<=g){sum +=(long) count2[i]*count2[j];}}}System.out.println(sum);}
}