代码随想录算法训练营第五天-242有效的字母异位词、349两个数组的交集、202快乐数、1两数之和
初步题解
242 有效的字母异位词
题目链接:(https://leetcode.cn/problems/valid-anagram/description/)
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
| public class LE242 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String t = scanner.nextLine(); System.out.println(isAnagram(s, t)); }
public static boolean isAnagram(String s, String t) { char[] charsS = s.toCharArray(); char[] charsT = t.toCharArray(); HashMap<String, Integer> numMapS = getNumMap(charsS); HashMap<String, Integer> numMapT = getNumMap(charsT); if (numMapS.size() != numMapT.size()) { return false; } for (String key : numMapS.keySet()) { if (!numMapS.get(key).equals(numMapT.get(key))) { return false; } } return true; }
private static HashMap<String, Integer> getNumMap(char[] charsS) { HashMap<String, Integer> map = new HashMap<>(); for (char c : charsS) { if (map.containsKey(String.valueOf(c))) { map.put(String.valueOf(c) , map.get(String.valueOf(c)) + 1); } else { map.put(String.valueOf(c), 1); } } return map; } }
|
349 两个数组的交集
题目链接:(https://leetcode.cn/problems/intersection-of-two-arrays/description/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class LE349 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] nums1 = Arrays.stream(scanner.nextLine().split(",")).filter(Objects::nonNull).mapToInt(Integer::parseInt).toArray(); int[] nums2 = Arrays.stream(scanner.nextLine().split(",")).filter(Objects::nonNull).mapToInt(Integer::parseInt).toArray(); int[] intersection = intersection(nums1, nums2); }
public static int[] intersection(int[] nums1, int[] nums2) { HashSet<Integer> set = new HashSet<>(); HashSet<Integer> intersection = new HashSet<>(); for (int i : nums1) { set.add(i); } for (int i : nums2) { if (set.contains(i)) { intersection.add(i); } }
return intersection.stream().filter(Objects::nonNull).mapToInt(i -> i).toArray(); } }
|
202 快乐数
题目链接:(https://leetcode.cn/problems/happy-number/description/)
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
| public class LE202 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); System.out.println(isHappy(Integer.parseInt(s))); }
public static boolean isHappy(int n) { HashSet<Integer> set = new HashSet<>(); String num = n + ""; set.add(n); while (true) { int sum = 0; for (int i = 0; i < num.length(); i++) { sum += Math.pow(Integer.parseInt(num.charAt(i) + ""), 2); } if (sum == 1) { return true; } if (set.contains(sum)) { return false; } num = sum + ""; set.add(sum); } } }
|
1 两数之和
题目链接:(https://leetcode.cn/problems/two-sum/)
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
| public class LE1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] nums = Arrays.stream(scanner.nextLine().split(",")).filter(Objects::nonNull).mapToInt(Integer::parseInt).toArray(); int target = Integer.parseInt(scanner.next()); twoSum(nums, target); }
public static int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(target - nums[i])) { return new int[]{map.get(target - nums[i]), i}; } map.put(nums[i], i); } return null; } }
|
看讲解
242 有效的字母异位词
题目链接/文章讲解/视频讲解:(https://programmercarl.com/0242.有效的字母异位词.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public static boolean isAnagram1(String s, String t) { int[] record = new int[26]; for (int i = 0; i < s.length(); i++) { record[s.charAt(i) - 'a']++; } for (int i = 0; i < t.length(); i++) { record[t.charAt(i) - 'a']--; } for (int count : record) { if (count != 0) { return false; } } return true; }
|
349 两个数组的交集
题目链接/文章讲解/视频讲解:(https://programmercarl.com/0349.两个数组的交集.html)
202 快乐数
题目链接/文章讲解:(https://programmercarl.com/0202.快乐数.html)
1 2 3 4 5 6
| while (n > 0) { int temp = n % 10; sum += temp * temp; n = n / 10; }
|
1 两数之和
题目链接/文章讲解/视频讲解:(https://programmercarl.com/0001.两数之和.html)