From 007a62df5154e3843f569bf591f77bdf854d8c3a Mon Sep 17 00:00:00 2001 From: fleyx Date: Sun, 21 Jan 2024 16:53:14 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q1.java | 10 +++++++++ 5.leetcode/src/com/fanxb/common/Q128.java | 26 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q14.java | 2 +- 5.leetcode/src/com/fanxb/common/Q202.java | 25 ++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q219.java | 20 +++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q228.java | 20 +++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q242.java | 24 +++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q49.java | 25 ++++++++++++++++++++++ 8 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q128.java create mode 100644 5.leetcode/src/com/fanxb/common/Q202.java create mode 100644 5.leetcode/src/com/fanxb/common/Q219.java create mode 100644 5.leetcode/src/com/fanxb/common/Q228.java create mode 100644 5.leetcode/src/com/fanxb/common/Q242.java create mode 100644 5.leetcode/src/com/fanxb/common/Q49.java diff --git a/5.leetcode/src/com/fanxb/common/Q1.java b/5.leetcode/src/com/fanxb/common/Q1.java index 5046235..30e46b7 100644 --- a/5.leetcode/src/com/fanxb/common/Q1.java +++ b/5.leetcode/src/com/fanxb/common/Q1.java @@ -25,6 +25,16 @@ public class Q1 { return null; } + public int[] twoSum1(int[] nums, int target) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) map.put(nums[i], i); + for (int i = 0; i < nums.length; i++) { + Integer targetI = map.get(target - nums[i]); + if (targetI != null && targetI != i) return new int[]{i, map.get(target - nums[i])}; + } + return null; + } + /** * 更优,一次循环即可 * diff --git a/5.leetcode/src/com/fanxb/common/Q128.java b/5.leetcode/src/com/fanxb/common/Q128.java new file mode 100644 index 0000000..8b1f2a9 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q128.java @@ -0,0 +1,26 @@ +package com.fanxb.common; + + +import java.util.HashSet; +import java.util.Set; + +public class Q128 { + + public int longestConsecutive(int[] nums) { + Set set = new HashSet<>(nums.length); + for (int num : nums) set.add(num); + int res = 0; + for (int num : nums) { + if (!set.contains(num - 1)) { + //说明num是一个序列的起点 + int temp = num, curRes = 1; + while (set.contains(++temp)) curRes++; + res = Math.max(res, curRes); + } + } + return res; + } + + public static void main(String[] args) { + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q14.java b/5.leetcode/src/com/fanxb/common/Q14.java index f4851f4..f11a439 100644 --- a/5.leetcode/src/com/fanxb/common/Q14.java +++ b/5.leetcode/src/com/fanxb/common/Q14.java @@ -35,7 +35,7 @@ public class Q14 { if (res.charAt(j) == strs[i].charAt(j)) temp.append(res.charAt(j)); else break; } - if (temp.isEmpty()) return ""; + if (temp.length() == 0) return ""; res = temp.toString(); } return res; diff --git a/5.leetcode/src/com/fanxb/common/Q202.java b/5.leetcode/src/com/fanxb/common/Q202.java new file mode 100644 index 0000000..f0a4861 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q202.java @@ -0,0 +1,25 @@ +package com.fanxb.common; + +import java.util.HashSet; +import java.util.Set; + +public class Q202 { + + public boolean isHappy(int n) { + Set cache = new HashSet<>(); + long temp = n; + while (true) { + String[] strs = Long.toString(temp).split(""); + long res = 0; + for (String s : strs) res += (long) Integer.parseInt(s) * Integer.parseInt(s); + if (res == 1) return true; + if (cache.contains(res)) return false; + cache.add(res); + temp = res; + } + } + + public static void main(String[] args) { + new Q202().isHappy(2); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q219.java b/5.leetcode/src/com/fanxb/common/Q219.java new file mode 100644 index 0000000..8338e1a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q219.java @@ -0,0 +1,20 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q219 { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int num = nums[i]; + Integer index = map.get(num); + if (index != null && Math.abs(i - index) <= k) return true; + map.put(num, i); + } + return false; + } + + public static void main(String[] args) { + } + +} diff --git a/5.leetcode/src/com/fanxb/common/Q228.java b/5.leetcode/src/com/fanxb/common/Q228.java new file mode 100644 index 0000000..1735ff1 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q228.java @@ -0,0 +1,20 @@ +package com.fanxb.common; + +import java.util.LinkedList; +import java.util.List; + +public class Q228 { + public List summaryRanges(int[] nums) { + List res = new LinkedList<>(); + if (nums.length == 0) return res; + int startIndex = 0, length = nums.length; + for (int i = 1; i < length; i++) { + if (nums[i] - nums[startIndex] != i - startIndex) { + res.add(startIndex == i - 1 ? String.valueOf(nums[startIndex]) : (nums[startIndex] + "->" + nums[i - 1])); + startIndex = i; + } + } + res.add(startIndex == length - 1 ? String.valueOf(nums[startIndex]) : (nums[startIndex] + "->" + nums[length - 1])); + return res; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q242.java b/5.leetcode/src/com/fanxb/common/Q242.java new file mode 100644 index 0000000..d8572ba --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q242.java @@ -0,0 +1,24 @@ +package com.fanxb.common; + +/** + * @author fanxb + * @date 2021-10-25-下午4:25 + */ +public class Q242 { + public boolean isAnagram(String s, String t) { + int sn = s.length(), tn = t.length(); + if (sn != tn) return false; + int[] count = new int[128]; + for (int i = 0; i < sn; i++) { + count[s.charAt(i)]++; + count[t.charAt(i)]--; + } + for (int j : count) if (j != 0) return false; + return true; + } + + + public static void main(String[] args) { + int[][] params = {{-1, 3}}; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q49.java b/5.leetcode/src/com/fanxb/common/Q49.java new file mode 100644 index 0000000..ef0bee7 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q49.java @@ -0,0 +1,25 @@ +package com.fanxb.common; + +import java.util.*; + +/** + * Created with IntelliJ IDEA + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q49 { + + public List> groupAnagrams(String[] strs) { + Map> map = new HashMap<>(); + for (String str : strs) { + char[] chars = str.toCharArray(); + Arrays.sort(chars); + map.computeIfAbsent(new String(chars), k -> new LinkedList<>()).add(str); + } + return new ArrayList<>(map.values()); + } + + public static void main(String[] args) { + } +}