This commit is contained in:
fleyx 2024-01-21 16:53:14 +08:00
parent 08407ea39c
commit 007a62df51
8 changed files with 151 additions and 1 deletions

View File

@ -25,6 +25,16 @@ public class Q1 {
return null; return null;
} }
public int[] twoSum1(int[] nums, int target) {
Map<Integer, Integer> 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;
}
/** /**
* 更优一次循环即可 * 更优一次循环即可
* *

View File

@ -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<Integer> 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) {
}
}

View File

@ -35,7 +35,7 @@ public class Q14 {
if (res.charAt(j) == strs[i].charAt(j)) temp.append(res.charAt(j)); if (res.charAt(j) == strs[i].charAt(j)) temp.append(res.charAt(j));
else break; else break;
} }
if (temp.isEmpty()) return ""; if (temp.length() == 0) return "";
res = temp.toString(); res = temp.toString();
} }
return res; return res;

View File

@ -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<Long> 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);
}
}

View File

@ -0,0 +1,20 @@
package com.fanxb.common;
import java.util.*;
public class Q219 {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> 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) {
}
}

View File

@ -0,0 +1,20 @@
package com.fanxb.common;
import java.util.LinkedList;
import java.util.List;
public class Q228 {
public List<String> summaryRanges(int[] nums) {
List<String> 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;
}
}

View File

@ -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}};
}
}

View File

@ -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<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> 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) {
}
}