Skip to content

Commit efb2479

Browse files
author
谢育欣
committed
done
1 parent a424440 commit efb2479

File tree

4 files changed

+99
-27
lines changed

4 files changed

+99
-27
lines changed

src/com/leetcode/Main11.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,28 @@
99
*/
1010
public class Main11 {
1111
public int maxArea(int[] height) {
12-
if (height==null || height.length<=1)
12+
if (height == null || height.length <= 1) {
1313
return 0;
14-
int max = 0;
15-
int l = 0;
16-
int r = height.length-1;
17-
int curCap = 0;
18-
while (l < r) {
19-
curCap = (r-l) * Math.min(height[l], height[r]);
20-
if (curCap > max)
21-
max = curCap;
22-
if (height[l] <= height[r])
23-
l++;
24-
else
25-
r--;
2614
}
27-
return max;
15+
int left = 0;
16+
int right = height.length - 1;
17+
int maxMl = Math.min(height[left], height[right]) * (right - left);
18+
while (left != right) {
19+
if (height[left] <= height[right]) {
20+
left += 1;
21+
} else {
22+
right -= 1;
23+
}
24+
if (left != right) {
25+
maxMl = Math.max(maxMl, Math.min(height[left], height[right]) * (right - left));
26+
}
27+
}
28+
return maxMl;
2829
}
2930

3031
public static void main(String[] args) {
3132
Main11 m = new Main11();
32-
int[] height = {1,8,6,2,5,4,8,3,7};
33+
int[] height = {2,3,4,5,18,17,6};
3334
System.out.println(m.maxArea(height));
3435
}
3536
}

src/com/leetcode/Main128.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@
2121
*/
2222
public class Main128 {
2323
public int longestConsecutive(int[] nums) {
24-
if (nums == null || nums.length == 0)
24+
if (nums == null || nums.length == 0) {
2525
return 0;
26-
Set<Integer> set = new HashSet<>();
27-
for(int num: nums) {
28-
set.add(num);
2926
}
30-
int res = 0;
31-
for (int num: set) {
32-
if (!set.contains(num-1)) {
33-
int curRes = 1;
27+
Set<Integer> numSet = new HashSet<>();
28+
for (int num: nums) {
29+
numSet.add(num);
30+
}
31+
int longestLen = 1;
32+
for (Integer num: numSet) {
33+
if (!numSet.contains(num - 1)) {
34+
int currentLen = 1;
3435
int curNum = num;
35-
while (set.contains(curNum+1)) {
36-
curRes += 1;
36+
while (numSet.contains(curNum + 1)) {
37+
currentLen += 1;
3738
curNum += 1;
3839
}
39-
res = Math.max(res, curRes);
40+
longestLen = Math.max(longestLen, currentLen);
4041
}
4142
}
42-
return res;
43+
return longestLen;
4344
}
4445

4546
public static void main(String[] args) {

src/com/leetcode/Main283.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 283. 移动零
7+
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
8+
* 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
9+
* 示例 1:
10+
*
11+
* 输入: nums = [0,1,0,3,12]
12+
* 输出: [1,3,12,0,0]
13+
* 示例 2:
14+
*
15+
* 输入: nums = [0]
16+
* 输出: [0]
17+
*/
18+
public class Main283 {
19+
public void moveZeroes(int[] nums) {
20+
int first = 0;
21+
int second = 0;
22+
while (second < nums.length) {
23+
if (nums[second] != 0) {
24+
nums[first] = nums[second];
25+
second += 1;
26+
first += 1;
27+
} else {
28+
second += 1;
29+
}
30+
}
31+
while (first < nums.length) {
32+
nums[first] = 0;
33+
first += 1;
34+
}
35+
}
36+
37+
public static void main(String[] args) {
38+
Main283 main283 = new Main283();
39+
int[] nums = {0};
40+
main283.moveZeroes(nums);
41+
System.out.println(Arrays.toString(nums));
42+
}
43+
}

src/com/leetcode/Main49.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.leetcode;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 49. 字母异位词分组
7+
* 给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
8+
* 字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
9+
* 示例 1:
10+
* 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
11+
* 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
12+
*/
13+
public class Main49 {
14+
public List<List<String>> groupAnagrams(String[] strs) {
15+
Map<String, List<String>> word2List = new HashMap<>();
16+
for (int i = 0; i < strs.length; i++) {
17+
char[] chars = strs[i].toCharArray();
18+
Arrays.sort(chars);
19+
String str = new String(chars);
20+
List<String> stringList = word2List.getOrDefault(str, new ArrayList<>());
21+
stringList.add(strs[i]);
22+
word2List.put(str, stringList);
23+
}
24+
return new ArrayList<>(word2List.values());
25+
}
26+
27+
}

0 commit comments

Comments
 (0)