牛客top100 - 自刷打卡day6 - 哈希

文章展示了使用哈希表解决一系列数组问题的Java代码实现,包括两数之和、数组中出现次数超过一半的数字、数组中只出现一次的两个数字、缺失的第一个正整数以及三数之和的问题。这些算法主要通过哈希映射优化搜索效率。
摘要由CSDN通过智能技术生成


哈希

BM50两数之和

两数之和

思路简单37.15%

import java.util.*;


public class Solution {
    /**
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public int[] twoSum (int[] numbers, int target) {
        // write code here
        int[] res = new int[2];
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < numbers.length; i++) {
            if(map.containsKey(target - numbers[i])) {
                res[0] = map.get(target - numbers[i]);
                res[1] = i + 1;
                return res;
            }
            if(map.get(numbers[i]) == null) {
                map.put(numbers[i], i + 1);
            }
        }
        return res;
    }
}

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

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

思路简单33.08%

  • 巧解吧, 但是估计会被打, 还是老老实实写别的办法
import java.util.*;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        Arrays.sort(array);
        int mid = array.length / 2;
        return array[mid];
    }
}
  • 正规写法?!
  • 我们计算出现最多的票数数为 + 1, 其他票数数 - 1
  • 投票法 - 出现最多的数 > 数组数 - 出现最多的数 所以票数和>0
  • 当前i个数的票数和为0 , 那么在后续的数串票数和依旧是大于0的
  • 所以更换出现最多的数值
import java.util.*;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        int cnt = array[0];//设出现最多的数值cnt
        int count = 1;
        for (int i = 1; i < array.length; i++) {
            if (array[i] != cnt) {
                count--;
            } else {
                count++;
            }
            if (count == 0) {
                cnt = array[i + 1];
                count = 1;
            }
        }
        return cnt;
    }
}

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

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

思路中等55.51%

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        int m = 1;
        int ans = 0;
        for(int a : array) {
            ans ^= a;
        }
        while((ans & m) == 0) {
            m = m << 1;
        }
        int[] res = new int[2];
        for(int i = 0; i < array.length; i++) {
            if((array[i] & m) == 0) {
                res[0] ^= array[i];
            }else{
                res[1] ^= array[i];
            }
        }
        if(res[0] > res[1]){
            int tmp = res[0];
            res[0] = res[1];
            res[1] = tmp;
        }
        return res;
    }
}

BM53缺失的第一个正整数

缺失的第一个正整数

思路中等39.79%

  • 标记法
import java.util.*;
import java.math.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @return int整型
     */
    public int minNumberDisappeared (int[] nums) {
        // write code here
        int len = nums.length;
        for(int i = 0; i < len; i++) {
            if(nums[i] <= 0){
                nums[i] = len + 1;
            }
        }
        for(int i = 0; i < len; i++) {
            if(Math.abs(nums[i]) <= len) {
                nums[Math.abs(nums[i]) - 1] = -1 * Math.abs(nums[Math.abs(nums[i]) - 1]);
            }
        }
        for(int i = 0; i < len; i++) {
            if(nums[i] > 0){
                return i + 1;
            }
        }
        return len + 1;
    }
}

BM54三数之和

三数之和

思路中等25.05%

import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        //排序
        if (num.length < 3) return res;
        Arrays.sort(num);
        int left = 0;
        int right = num.length - 1;
        for (int i = 0; i < num.length; i++) {
            left = i + 1;
            right = num.length - 1;
            while (left < right) {
                if (num[i] + num[left] + num[right] == 0) {
                    ArrayList<Integer> path = new ArrayList<>();
                    path.add(num[i]);
                    path.add(num[left]);
                    path.add(num[right]);
                    res.add(path);
                    //注意排除重复元素  [-2,0,0,2,2]
                    //预期 [[-2,0,2]]
                    //如果不排除重复元素就会 : [[-2,0,2],[-2,0,2]]
                    while (left < right && num[left] == num[left + 1]) {
                        left++;
                    }
                    while (left < right && num[right] == num[right - 1]) {
                        right--;
                    }
                    left++;
                    right--;
                } else if (num[i] + num[left] + num[right] < 0) {
                    left++;
                } else if (num[i] + num[left] + num[right] > 0) {
                    right--;
                }
            }
            while (i + 1 < num.length - 1 && num[i + 1] == num[i]) i++;
        }


        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值