加油冲
This commit is contained in:
parent
a01e34bcfb
commit
2e3d5486bc
|
@ -0,0 +1,9 @@
|
|||
package com.zerroi.leetcode.Three15;
|
||||
|
||||
public class CanConstruct {
|
||||
}
|
||||
class SolutionThird {
|
||||
public boolean canConstruct(String ransomNote, String magazine) {
|
||||
return magazine.contains(ransomNote);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.zerroi.leetcode.Three15;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* 将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。
|
||||
|
||||
比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:
|
||||
|
||||
P A H N
|
||||
A P L S I I G
|
||||
Y I R
|
||||
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"。
|
||||
|
||||
请你实现这个将字符串进行指定行数变换的函数:
|
||||
|
||||
string convert(string s, int numRows);
|
||||
* */
|
||||
public class Convert {
|
||||
}
|
||||
|
||||
class SolutionFirst {
|
||||
public String convert(String s, int numRows) {
|
||||
if (numRows < 2) return s;
|
||||
List<StringBuilder> list = new ArrayList<>();
|
||||
for (int i = 0; i < numRows; i++) {
|
||||
list.add(new StringBuilder());
|
||||
}
|
||||
int flag = -1, i = 0;
|
||||
for (char c : s.toCharArray()) {
|
||||
list.get(i).append(c);
|
||||
if (i == 0 || i == numRows - 1) flag = -flag;
|
||||
i += flag;
|
||||
}
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (StringBuilder builder : list) {
|
||||
res.append(builder);
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package com.zerroi.leetcode.Three15;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/*给定一个字符串 s ,请你找出其中不含有重复字符的 最长
|
||||
子串
|
||||
的长度。*/
|
||||
public class LengthOfLongestSubstring {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionSecond {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
Set<Character> set = new HashSet<>();
|
||||
int start = 0;
|
||||
int end = 0;
|
||||
int res = 0;
|
||||
while (end < s.length()) {
|
||||
if (!set.contains(s.charAt(end))) {
|
||||
set.add(s.charAt(end));
|
||||
end++;
|
||||
} else {
|
||||
set.remove(s.charAt(start));
|
||||
start++;
|
||||
}
|
||||
res = Math.max(res, set.size());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
package com.zerroi.leetcode.Three16;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class IsValidSudoku {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(5 / 3);
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionFirst {
|
||||
public boolean isValidSudoku(char[][] board) {
|
||||
Set<Character> set = new HashSet<>();
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
char c = board[i][j];
|
||||
if (c != '.'&& set.contains(c)) return false;
|
||||
set.add(c);
|
||||
}
|
||||
set.clear();
|
||||
}
|
||||
|
||||
Set<Character> setRow = new HashSet<>();
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
char c = board[j][i];
|
||||
if (c != '.' && setRow.contains(c)) return false;
|
||||
setRow.add(c);
|
||||
}
|
||||
setRow.clear();
|
||||
}
|
||||
|
||||
Set<Character> setBlock = new HashSet<>();
|
||||
for (int i = 0; i < 9; i++) {
|
||||
int m = i / 3;
|
||||
int n = i % 3;
|
||||
setBlock.clear();
|
||||
for (int k = m * 3; k < m * 3 + 3; k++) {
|
||||
for (int j = n * 3; j < n * 3 + 3; j++) {
|
||||
char c = board[k][j];
|
||||
if (c != '.' && setBlock.contains(c)) return false;
|
||||
setBlock.add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isValidSudoku2(char[][] board) {
|
||||
Set<Character> set = new HashSet<>();
|
||||
Map<Character, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
char c = board[i][j];
|
||||
if (c != '.'&& set.contains(c)) return false;
|
||||
map.put(c, 1);
|
||||
}
|
||||
set.clear();
|
||||
}
|
||||
|
||||
Set<Character> setRow = new HashSet<>();
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
char c = board[j][i];
|
||||
if (c != '.' && setRow.contains(c)) return false;
|
||||
setRow.add(c);
|
||||
}
|
||||
setRow.clear();
|
||||
}
|
||||
|
||||
Set<Character> setBlock = new HashSet<>();
|
||||
for (int i = 0; i < 9; i++) {
|
||||
int m = i / 3;
|
||||
int n = i % 3;
|
||||
setBlock.clear();
|
||||
for (int k = m * 3; k < m * 3 + 3; k++) {
|
||||
for (int j = n * 3; j < n * 3 + 3; j++) {
|
||||
char c = board[k][j];
|
||||
if (c != '.' && setBlock.contains(c)) return false;
|
||||
setBlock.add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.zerroi.leetcode.Three16;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SpiralOrder {
|
||||
}
|
||||
|
||||
class SolutionSecond {
|
||||
public List<Integer> spiralOrder(int[][] matrix) {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
int left = 0, right = matrix.length;
|
||||
int top = 0, bottom = matrix[0].length;
|
||||
int len = right * bottom;
|
||||
int i = 0;
|
||||
while (i < len) {
|
||||
for (int j = left; j < right; j++) {
|
||||
res.add(matrix[top][j]);
|
||||
++i;
|
||||
if (i >= len) return res;
|
||||
}
|
||||
++top;
|
||||
|
||||
for (int j = top; j < bottom; j++) {
|
||||
res.add(matrix[j][right - 1]);
|
||||
++i;
|
||||
if (i >= len) return res;
|
||||
}
|
||||
--right;
|
||||
for (int j = right - 1; j >= left; j--) {
|
||||
res.add(matrix[bottom-1][j]);
|
||||
++i;
|
||||
if (i >= len) return res;
|
||||
}
|
||||
++left;
|
||||
for (int j = bottom - 1; j >= top; j--) {
|
||||
res.add(matrix[j][left]);
|
||||
++i;
|
||||
if (i >= len) return res;
|
||||
|
||||
}
|
||||
--bottom;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.zerroi.leetcode.Three17;
|
||||
|
||||
public class GameOfLife {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public void gameOfLife(int[][] board) {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.zerroi.leetcode.Three17;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Rotate {
|
||||
public static void main(String[] args) {
|
||||
int[][] matrix = new int[][]{{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}};
|
||||
SolutionFirst solutionFirst = new SolutionFirst();
|
||||
solutionFirst.rotate(matrix);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。
|
||||
|
||||
* */
|
||||
class SolutionFirst {
|
||||
public void rotate(int[][] matrix) {
|
||||
int n = matrix.length;
|
||||
int[][] res = new int[n][n];
|
||||
int indexCol;
|
||||
int indexRow = 0;
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
indexCol = n - 1;
|
||||
for (int j = n - 1; j >= 0; j--) {
|
||||
res[indexCol][indexRow] = matrix[i][j];
|
||||
indexCol--;
|
||||
}
|
||||
indexRow++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.arraycopy(res[i], 0, matrix[i], 0, n);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.zerroi.leetcode.Three17;
|
||||
|
||||
public class SetZeroes {
|
||||
public static void main(String[] args) {
|
||||
SolutionSecond solutionSecond = new SolutionSecond();
|
||||
solutionSecond.setZeroes(new int[][]{{0,1,2,0},{3,4,5,2},{1,3,1,5}});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionSecond {
|
||||
public void setZeroes(int[][] matrix) {
|
||||
int m = matrix.length;
|
||||
int n = matrix[0].length;
|
||||
int[][] position = new int[m][n];
|
||||
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (matrix[i][j] == 0) position[i][j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (position[i][j] == 1) {
|
||||
for (int k = 0; k < n; k++) {
|
||||
if (matrix[i][k] != 0) matrix[i][k] = 0;
|
||||
}
|
||||
for (int l = 0; l < n; l++) {
|
||||
if (matrix[l][j] != 0) matrix[l][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setZeroes(int[][] matrix, int a) {
|
||||
int m = matrix.length;
|
||||
int n = matrix[0].length;
|
||||
boolean[] col = new boolean[m];
|
||||
boolean[] row = new boolean[n];
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (matrix[i][j] == 0) {
|
||||
col[i] = row[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (col[i] || row[j] ) {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.zerroi.leetcode.Three18;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CanConstruct {
|
||||
public static void main(String[] args) {
|
||||
SolutionFirst solutionFirst = new SolutionFirst();
|
||||
boolean res = solutionFirst.canConstruct("aa", "aab", 1);
|
||||
System.out.println("res = " + res);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionFirst {
|
||||
public boolean canConstruct(String ransomNote, String magazine) {
|
||||
if (magazine.length() < ransomNote.length()) return false;
|
||||
|
||||
Map<Character, Integer> map = new HashMap<>();
|
||||
for (char c : magazine.toCharArray()) {
|
||||
if (map.containsKey(c)) {
|
||||
map.put(c, map.get(c) + 1);
|
||||
} else {
|
||||
map.put(c, 1);
|
||||
}
|
||||
}
|
||||
|
||||
for (char c : ransomNote.toCharArray()) {
|
||||
// 如果magazine中不包涵ransomNote中的某一个字符
|
||||
if (!map.containsKey(c)) {
|
||||
return false;
|
||||
} else {
|
||||
Integer i = map.get(c);
|
||||
if (i <= 0) {
|
||||
return false;
|
||||
}
|
||||
else map.put(c, i-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean canConstruct(String ransomNote, String magazine, int i) {
|
||||
char[] ransomNoteCharArray = ransomNote.toCharArray();
|
||||
char[] magazineCharArray = magazine.toCharArray();
|
||||
int[] count = new int[26];
|
||||
for (char c : magazineCharArray) {
|
||||
count[c - 'a']++;
|
||||
}
|
||||
|
||||
for (char c : ransomNoteCharArray) {
|
||||
count[c - 'a']--;
|
||||
if (count[c - 'a'] < 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.zerroi.leetcode.Three18;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class IsIsomorphic {
|
||||
public static void main(String[] args) {
|
||||
SolutionSecond solutionSecond = new SolutionSecond();
|
||||
boolean res = solutionSecond.isIsomorphic("badc", "baba");
|
||||
System.out.println("res = " + res);
|
||||
}
|
||||
}
|
||||
|
||||
/*给定两个字符串 s 和 t ,判断它们是否是同构的。
|
||||
|
||||
如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。
|
||||
|
||||
每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。*/
|
||||
class SolutionSecond {
|
||||
public boolean isIsomorphic(String s, String t) {
|
||||
if (s.length() != t.length()) return false;
|
||||
|
||||
Map<Character, Character> sToT = new HashMap<>();
|
||||
Map<Character, Character> tToS = new HashMap<>();
|
||||
|
||||
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char charS = s.charAt(i);
|
||||
char charT = t.charAt(i);
|
||||
|
||||
// Check s to t mapping
|
||||
if (sToT.containsKey(charS)) {
|
||||
if (sToT.get(charS) != charT) {
|
||||
return false; // Conflict in mapping
|
||||
}
|
||||
} else {
|
||||
sToT.put(charS, charT);
|
||||
}
|
||||
|
||||
// Check t to s mapping
|
||||
if (tToS.containsKey(charT)) {
|
||||
if (tToS.get(charT) != charS) {
|
||||
return false; // Conflict in mapping
|
||||
}
|
||||
} else {
|
||||
tToS.put(charT, charS);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package com.zerroi.leetcode.Three18;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class WordPattern {
|
||||
public static void main(String[] args) {
|
||||
SolutionThird solutionThird = new SolutionThird();
|
||||
boolean res = solutionThird.wordPattern("aaaa", "dog cat cat dog");
|
||||
System.out.println("res = " + res);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionThird {
|
||||
public boolean wordPattern(String pattern, String s) {
|
||||
HashMap<Character, String> patternToS = new HashMap<>();
|
||||
HashMap<String, Character> SToPattern = new HashMap<>();
|
||||
String[] strs = s.split(" ");
|
||||
if (pattern.length() != strs.length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < pattern.length(); i++) {
|
||||
char c = pattern.charAt(i);
|
||||
String str = strs[i];
|
||||
if (patternToS.containsKey(c)) {
|
||||
if (!Objects.equals(patternToS.get(c), str)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
patternToS.put(c, str);
|
||||
}
|
||||
|
||||
if (SToPattern.containsKey(str)) {
|
||||
if (SToPattern.get(str) != c) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
SToPattern.put(str, c);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.zerroi.leetcode.Three19;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class GroupAnagrams {
|
||||
public static void main(String[] args) {
|
||||
SolutionSecond solutionSecond = new SolutionSecond();
|
||||
List<List<String>> res = solutionSecond.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
|
||||
res.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionSecond {
|
||||
public List<List<String>> groupAnagrams(String[] strs) {
|
||||
if (strs.length <= 1) Arrays.asList(strs);
|
||||
|
||||
List<List<String>> res = new ArrayList<>();
|
||||
List<String> path = new ArrayList<>();
|
||||
boolean[] walked = new boolean[strs.length];
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
if (walked[i]) continue;
|
||||
path.add(strs[i]);
|
||||
for (int j = i + 1; j < strs.length; j++) {
|
||||
if (isAnagram(strs[i], strs[j])) {
|
||||
path.add(strs[j]);
|
||||
walked[j] = true;
|
||||
}
|
||||
}
|
||||
res.add(new ArrayList<>(path));
|
||||
path.clear();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean isAnagram(String s, String t) {
|
||||
if (s.length() != t.length()) return false;
|
||||
int[] count = new int[26];
|
||||
for (char c : s.toCharArray()) {
|
||||
count[c - 'a']++;
|
||||
}
|
||||
for (char c : t.toCharArray()) {
|
||||
count[c - 'a']--;
|
||||
if (count[c - 'a'] < 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<List<String>> groupAnagrams(String[] strs, int a) {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
char[] array = strs[i].toCharArray();
|
||||
Arrays.sort(array);
|
||||
String str = new String(array);
|
||||
List<String> list = map.getOrDefault(str, new ArrayList<>());
|
||||
list.add(strs[i]);
|
||||
map.put(str, list);
|
||||
}
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.zerroi.leetcode.Three19;
|
||||
|
||||
public class IsAnagram {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionFirst {
|
||||
public boolean isAnagram(String s, String t) {
|
||||
if (s.length() != t.length()) return false;
|
||||
int[] count = new int[26];
|
||||
for (char c : s.toCharArray()) {
|
||||
count[c - 'a']++;
|
||||
}
|
||||
for (char c : t.toCharArray()) {
|
||||
count[c - 'a']--;
|
||||
if (count[c - 'a'] < 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.zerroi.leetcode.Three19;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TwoSum {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionThird {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if(map.containsKey(target - nums[i])){
|
||||
return new int[]{i, map.get(target - nums[i])};
|
||||
}
|
||||
map.put(nums[i], i);
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.zerroi.leetcode.Three20;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ContainsNearbyDuplicate {
|
||||
public static void main(String[] args) {
|
||||
SolutionSecond solutionSecond = new SolutionSecond();
|
||||
boolean res = solutionSecond.containsNearbyDuplicate(new int[]{1,2,3,1}, 3);
|
||||
System.out.println("res = " + res);
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionSecond {
|
||||
public boolean containsNearbyDuplicate(int[] nums, int k) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (map.containsKey(nums[i])) {
|
||||
if (Math.abs(map.get(nums[i]) - i) <= k) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
map.put(nums[i], i);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.zerroi.leetcode.Three20;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class IsHappy {
|
||||
public static void main(String[] args) {
|
||||
SolutionFirst solutionFirst = new SolutionFirst();
|
||||
boolean res = solutionFirst.isHappy(2);
|
||||
System.out.println("res = " + res);
|
||||
}
|
||||
}
|
||||
|
||||
class SolutionFirst {
|
||||
public boolean isHappy(int n) {
|
||||
return isHappyHelper(n, new HashMap<>());
|
||||
}
|
||||
|
||||
private boolean isHappyHelper(int n, Map<Integer, Integer> map) {
|
||||
if (n == 1) return true;
|
||||
|
||||
int key = getNum(n);
|
||||
if (map.containsKey(key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
map.put(key, 1);
|
||||
return isHappyHelper(key, map);
|
||||
}
|
||||
|
||||
private int getNum(int n) {
|
||||
int sum = 0;
|
||||
while (n > 0) {
|
||||
int a = n % 10;
|
||||
n /= 10;
|
||||
sum += a * a;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.zerroi.leetcode.Three20;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class LongestConsecutive {
|
||||
public static void main(String[] args) {
|
||||
SolutionThird solutionThird = new SolutionThird();
|
||||
int res = solutionThird.longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1});
|
||||
System.out.println("res = " + res);
|
||||
}
|
||||
|
||||
}
|
||||
class SolutionThird {
|
||||
public int longestConsecutive(int[] nums) {
|
||||
if (nums.length == 0) return 0;
|
||||
|
||||
Set<Integer> set = new HashSet<>();
|
||||
for (int num : nums) {
|
||||
set.add(num);
|
||||
}
|
||||
|
||||
int longest = 0;
|
||||
for (int num : nums) {
|
||||
if (!set.contains(num - 1)) {
|
||||
int currentNum = num;
|
||||
int currentLength = 1;
|
||||
while (set.contains(currentNum + 1)) {
|
||||
currentNum++;
|
||||
currentLength++;
|
||||
}
|
||||
longest = Math.max(longest, currentLength);
|
||||
}
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue