49-字母异位词分组

class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
//创建一个哈希Map集合(无序)
Map<String,List<String>> map=new HashMap<>();
//遍历数组中的每一个字符串
for(String str:strs){
//将每一个字符串转为字符数组,然后将该数组按字典排序
char[] s = strs.charToArray();
Arrays.sort(s);
//将排序后的结果转为字符串,作为map集合中的key
//若当前key值不存在,则新增(key,list集合)
//若存在,则获取到对应的value,将当前字符串添加到list中
String key=new String(s);
if(!map.containsKey(key)){
map.put(key,new ArrayList<>());
}
map.get(key).add(str);
}
return new ArrayList(map.values());
}
}
遇到的问题:把 char[] 类型 s 直接作为 Map 的 key 来用 containsKey(s)。
数组是按照地址比较的,不会比较内容,所以应该将排序后的char[]转换为String来作为key值。
128-最长连续序列

第一反应就是先对数组进行排序,但是之后下一步就毫无头绪了。
看了题解中
灵茶山艾府的题解,还是一知半解。
核心思路:对于 nums 中的元素 x,以 x 为起点,不断查找下一个数 x+1,x+2,⋯ 是否在 nums 中,并统计序列的长度。
要先将数组转为哈希集合,来对数组进行去重,然后遍历哈希集合。
Set<Integer> set=new HashSet<>();
for(int n : nums){
set.add(n);
}
完整代码如下:
class Solution {
public int longestConsecutive(int[] nums) {
int count=0;
Set<Integer> set=new HashSet<>();
for(int n : nums){
set.add(n);
}
for(int n : set){
if(set.contains(n-1)){
continue;
}
int y = n+1;
while(set.contains(y)){
y++;
}
count=Math.max(count,y-n);
}
return count;
}
}
只有当 x - 1 不在集合中时,才认为 x 是 一个连续序列的起点。否则它就是别人序列的中间项 → 跳过continue
如果是起点的话,使用while循环来检查x+1,x+2,...是否存在,直到找不到。
例如:输入:[100, 4, 200, 1, 3, 2]
遍历:
-
x=100→99不在 → 起点 → 找不到101→ 长度 1 -
x=4→3在 → 不是起点 → 跳过 -
x=200→199不在 → 起点 → 找不到201→ 长度 1 -
x=1→0不在 → 起点 → 找到 2、3、4 → 连续长度 = 4(执行完while此时y=5,因此长度=y-x)
283-移动零

没有仔细审题,没看到保持非零元素的相对顺序。
class Solution {
public void moveZeroes(int[] nums) {
int i0 = 0;//记录非0元素应该存放的位置
for (int i = 0; i < nums.length; i++) {
//如果当前元素为0,则跳过不管
if (nums[i] != 0) {
// 交换 nums[i] 和 nums[i0]
int tmp = nums[i];
nums[i] = nums[i0];
nums[i0] = tmp;
i0++;
}
}
}
}
例如:输入[0,1,0,3,12]
| i | nums[i] | i0 | 动作 | 数组变化 |
|---|---|---|---|---|
| 0 | 0 | 0 | 跳过 | [0,1,0,3,12] |
| 1 | 1 | 0 | 交换 nums[1] 和 nums[0] | [1,0,0,3,12] |
| 2 | 0 | 1 | 跳过 | |
| 3 | 3 | 1 | 交换 nums[3] 和 nums[1] | [1,3,0,0,12] |
| 4 | 12 | 2 | 交换 nums[4] 和 nums[2] | [1,3,12,0,0] |
11-盛水最多的容器

这个题我自己想解法的话只能想到用两个指针一前一后暴力枚举所有的(i,j)组合,但是在数据量大时会出现超时的问题。
我的原错误代码:(测试用例通过,提交用例未通过)
class Solution {
public int maxArea(int[] height) {
int n=height.length;
int ares=0;
for(int i=0;i<n;i++){
for(int j=n-1;j>i;j--){
int r=Math.min(height[i],height[j]);
ares=Math.max(ares,(j-i)*r);
}
}
return ares;
}
}
看了题解的思路:

修改代码:
class Solution {
public int maxArea(int[] height) {
int n=height.length;
int ares=0;
for(int i=0,j=n-1;i<j;){
int r=Math.min(height[i],height[j]);
ares=Math.max(ares,(j-i)*r);
if(height[i]<height[j]){
i++;
}else {
j--;
}
}
return ares;
}
}
15-三数之和

这个好难,毫无思路。又看题解了。
class Solution {
//定义三个指针,保证遍历数组中的每一个结果
//画图,解答
public List<List<Integer>> threeSum(int[] nums) {
//定义一个结果集
List<List<Integer>> res = new ArrayList<>();
//数组的长度
int len = nums.length;
//当前数组的长度为空,或者长度小于3时,直接退出
if(nums == null || len <3){
return res;
}
//将数组进行排序
Arrays.sort(nums);
//遍历数组中的每一个元素
for(int i = 0; i<len;i++){
//如果遍历的起始元素大于0,就直接退出
//原因,此时数组为有序的数组,最小的数都大于0了,三数之和肯定大于0
if(nums[i]>0){
break;
}
//去重,当起始的值等于前一个元素,那么得到的结果将会和前一次相同
if(i > 0 && nums[i] == nums[i-1]) continue;
int l = i +1;
int r = len-1;
//当 l 不等于 r时就继续遍历
while(l<r){
//将三数进行相加
int sum = nums[i] + nums[l] + nums[r];
//如果等于0,将结果对应的索引位置的值加入结果集中
if(sum==0){
// 将三数的结果集加入到结果集中
res.add(Arrays.asList(nums[i], nums[l], nums[r]));
//在将左指针和右指针移动的时候,先对左右指针的值,进行判断
//如果重复,直接跳过。
//去重,因为 i 不变,当此时 l取的数的值与前一个数相同,所以不用在计算,直接跳
while(l < r && nums[l] == nums[l+1]) {
l++;
}
//去重,因为 i不变,当此时 r 取的数的值与前一个相同,所以不用在计算
while(l< r && nums[r] == nums[r-1]){
r--;
}
//将 左指针右移,将右指针左移。
l++;
r--;
//如果结果小于0,将左指针右移
}else if(sum < 0){
l++;
//如果结果大于0,将右指针左移
}else if(sum > 0){
r--;
}
}
}
return res;
}
}
5908




被折叠的 条评论
为什么被折叠?



