NC刷题笔记8-哈希

本博客文章(学习笔记)导航 (点击这里访问)
在这里插入图片描述

BM50 两数之和

public class Solution {
    public int[] twoSum (int[] numbers, int target) {
        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
        for(int i=0;i<numbers.length;i++){
            if(map.containsKey(target-numbers[i])){
                return new int[]{map.get(target-numbers[i])+1,i+1};
            }
            map.put(numbers[i],i);
        }
        return new int[0];
    }
}

BM51 数组中出现次数超过一半的数字

摩尔投票法:
	两个数一样就相互抵消,如果某个数出现次数大于数组长度的一半,那抵消之后最后就剩这一个数字
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        int candidate=0;
        int nums=0;
        for(int i=0;i<array.length;i++){
            if(nums==0){
                candidate=array[i];
                nums++;
            }else{
                if(candidate==array[i]){
                    nums++;
                }else{
                    nums--;
                }
            }
        }
        if(nums!=0) return candidate;
        return 0;
    }
}

BM52 数组中只出现一次的两个数字

思路:分组异或
	& 判断某一位上两个数字是否都是1
	^ 去重 两个相同的数异或为0
public class Solution {
    public int[] FindNumsAppearOnce (int[] array) {
        int res=0;
        for(int i:array) res^=i;
        int bit=1;
        while((bit&res)==0) bit=bit<<1;
        int A=0,B=0;
        for(int i:array){
            if((i&bit)==0){
                A^=i;
            }else{
                B^=i;
            }
        }
        return A<B?new int[]{A,B}:new int[]{B,A};
    }
}

BM53 缺失的第一个正整数

思路:
	缺失的数据一定存在于1-n之间
	新建1-n的数组,如果在范围内就给对应索引处赋值
	返回数组中没有赋值的索引
	时间复杂度:O(n)  空间复杂度:O(n)
import java.util. ;
public class Solution {
    public int minNumberDisappeared (int[] nums) {
        int[] res=new int[nums.length+1];
        for(int i=0;i<nums.length;i++){
            if(nums[i]>0&&nums[i]<=nums.length) res[nums[i]]=nums[i];
        }
        for(int i=1;i<res.length;i++){
            if(res[i]==0) return i;
        }
        return res.length;
    }
}

BM54 三数之和

思路:
	for循环+双指针
import java.util. ;
public class Solution {
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        Arrays.sort(num);
        for(int i=0;i<num.length-2;i++){
            if(i>0 && num[i]==num[i-1]) continue;
            int l=i+1,r=num.length-1;
            while(l<r){
                ArrayList<Integer> list=new ArrayList<>();
                if(num[i]+num[l]+num[r]==0){
                    list.add(num[i]);
                    list.add(num[l]);
                    list.add(num[r]);
                    res.add(list);
                    while(l<r&&num[l]==num[++l]);
                    while(l<r&&num[r]==num[--r]);
                }else if(num[i]+num[l]+num[r]<0){
                     l++;
                }else if(num[i]+num[l]+num[r]>0){
                     r--;
                }
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CandyDingDing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值