Hash和队列的面试算法题

Leetcode232用栈实现队列

Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }

    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }

    public int pop() {
        return queue1.poll();
    }

    public int top() {
        return queue1.peek();
    }

    public boolean empty() {
        return queue1.isEmpty();
    }

Leetcode225用队列实现栈

Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }

    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }

    public int pop() {
        return queue1.poll();
    }

    public int top() {
        return queue1.peek();
    }

    public boolean empty() {
        return queue1.isEmpty();
    }

N数之和

Leetcode1两数之和

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

Leetcode15三数之和

public static List<List<Integer>> threeSum(int[] nums) {
        int n=nums.length;
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<List<Integer>>();//结果
        for (int first=0;first<n;first++){
            //找到与上一个枚举不同的第一个数
            if (first>0&&nums[first]==nums[first-1]){
                continue;
            }
            //2+3==target
            int target=-nums[first];
            //从后向前的对撞指针
            int third=n-1;
            //从前向后的对撞指针
            for (int second=first+1;second<n;second++){
                //在同一个first循环中防止枚举相同的second数
                if (second>first+1&&nums[second]==nums[second-1]){
                    continue;
                }
                //找到2+3<=target
                while (second<third&&nums[second]+nums[third]>target){
                    third--;
                }
                //如果找到两个指针对撞才退出循环,说明2+3>target,那么后续循环2会继续增加,2+3一定>现在的2+3没必要继续
                if (third==second){
                    break;
                }
                //存答案
                if (nums[second]+nums[third]==target){
                    List<Integer> list=new ArrayList<>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    ans.add(list);
                }
            }
        }
        return ans;
    }

Leetcode18四数之和 

public static List<List<Integer>> fourSum(int[] nums, int target) {
        int n = nums.length;
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        for (int fourth = 0; fourth < n-3; fourth++) {
            if (fourth > 0 && nums[fourth] == nums[fourth- 1]) {
                continue;
            }
            if ((long) nums[fourth] + nums[fourth + 1] + nums[fourth + 2] + nums[fourth + 3] > target) {
                break;
            }
            if ((long) nums[fourth] + nums[n-3] + nums[n-2] + nums[n-1] < target) {
                continue;
            }
            int target1 = target - nums[fourth];
            for (int first = fourth + 1; first < n; first++) {
                //找到与上一个枚举不同的第一个数
                if (first > fourth + 1 && nums[first] == nums[first - 1]) {
                    continue;
                }
                //2+3==target
                int target2 = target1 - nums[first];
                //从后向前的对撞指针
                int third = n - 1;
                //从前向后的对撞指针
                for (int second = first + 1; second < n; second++) {
                    //在同一个first循环中防止枚举相同的second数
                    if (second > first + 1 && nums[second] == nums[second - 1]) {
                        continue;
                    }
                    //找到2+3<=target
                    while (second < third && nums[second] + nums[third] > target2) {
                        third--;
                    }
                    //如果找到两个指针对撞才退出循环,说明2+3>target,那么后续循环2会继续增加,2+3一定>现在的2+3没必要继续
                    if (third == second) {
                        break;
                    }
                    //存答案
                    if (nums[second] + nums[third] == target2) {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[fourth]);
                        list.add(nums[first]);
                        list.add(nums[second]);
                        list.add(nums[third]);
                        ans.add(list);
                    }
                }
            }
        }
        return ans;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值