Skip to content

Commit 695dde2

Browse files
committed
update
1 parent 9e2f511 commit 695dde2

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.leetcode.hash;
2+
3+
import java.sql.Array;
4+
import java.util.*;
5+
6+
/**
7+
*
8+
*
9+
* keyword : hash , group
10+
*/
11+
12+
public class Anagram {
13+
14+
public List<List<String>> groupAnagrams(String[] strs) {
15+
List<List<String>> result = new ArrayList<>();
16+
HashMap<String,List<String>> map = new HashMap<String,List<String>>();
17+
18+
for (int i = 0; i < strs.length; i++) {
19+
char[] temp = strs[i].toCharArray();
20+
Arrays.sort(temp);
21+
String sortedStr = String.valueOf(temp);
22+
Set<String> set = map.keySet();
23+
24+
if(set.contains(sortedStr)){
25+
map.get(sortedStr).add(strs[i]);
26+
}else{
27+
ArrayList<String> list = new ArrayList<>();
28+
list.add(strs[i]);
29+
map.put(sortedStr , list );
30+
}
31+
}
32+
result.addAll(map.values());
33+
return result;
34+
}
35+
36+
37+
public static void main(String[] args) {
38+
String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
39+
List<List<String>> lists = new Anagram().groupAnagrams(strs);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.leetcode.hash;
2+
3+
import java.util.*;
4+
5+
public class ConsecutiveSequence {
6+
7+
public int longestConsecutive(int[] nums) {
8+
9+
// sort the array using Arrays.sort()
10+
Arrays.sort(nums);
11+
int count = 1;
12+
int max = 1;
13+
for (int i = 1; i < nums.length; i++) {
14+
if (nums[i] - nums[i - 1] == 1) {
15+
count++;
16+
} else {
17+
// record last max count.
18+
max = Math.max(max, count);
19+
count = 1;
20+
}
21+
}
22+
return Math.max(max, count);
23+
}
24+
25+
26+
public static void main(String[] args) {
27+
28+
int[] nums = {100, 4, 200, 1, 3, 2};
29+
int i = new ConsecutiveSequence().longestConsecutive(nums);
30+
System.out.println(i);
31+
}
32+
}

src/main/java/com/leetcode/hash/TwoSumS.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**
7+
* put the ele and index of array into a hashmap.
8+
*
9+
*/
610
public class TwoSumS {
711

8-
public int[] twoSun(int[] nums , int target){
12+
public int[] twoSun(int[] nums, int target) {
913
Map<Integer, Integer> map = new HashMap<>();
1014
for (int i = 0; i < nums.length; i++) {
11-
if (map.containsKey(target-nums[i])) {
12-
return new int[]{i, map.get(target-nums[i])};
15+
if (map.containsKey(target - nums[i])) {
16+
return new int[]{i, map.get(target - nums[i])};
1317
}
1418
map.put(nums[i], i);
1519
}
1620
return new int[]{};
1721
}
1822

1923
public static void main(String[] args) {
20-
int[] arr = {2,5,7,9};
24+
int[] arr = {2, 5, 7, 9};
2125
int[] ints = new TwoSumS().twoSun(arr, 7);
22-
for(int i : ints){
26+
for (int i : ints) {
2327
System.out.println(i);
2428
}
2529

0 commit comments

Comments
 (0)