栈和队列的互相实现以及n数之和

文章介绍了如何使用两个栈来模拟队列的操作,以及如何利用两个队列实现栈的功能。此外,还探讨了两数之和问题的双重循环解法和优化后的哈希表解法,以及三数之和问题的解决方案,强调了排序和避免重复值的重要性,以降低时间复杂度。
摘要由CSDN通过智能技术生成

栈实现队列

使用两个栈,一个栈用于接受输入(push),一个栈用于输出(pop和peek)。

public class QueueByStack {
    Stack<Integer> inStack;
    Stack<Integer> outStack;
    public QueueByStack(){
        inStack = new Stack<>();
        outStack = new Stack<>();
    }
    public void push(int x){
        inStack.push(x);
    }
    public int pop(){
        if (outStack.isEmpty()){
            in2out();
        }
        return outStack.pop();
    }
    public int peek(){
        if (outStack.isEmpty()){
            in2out();
        }
        return outStack.peek();
    }
    public boolean isEmpty(){
        return outStack.isEmpty()&&inStack.isEmpty();
    }
    public void in2out(){
        while (!inStack.isEmpty()){
            outStack.push(inStack.pop());
        }
    }
}

队列实现栈

使用两个队列queue1和queue2实现栈。在进行push操作时,先将输入值压入队列queue2中,再将queue1的所有元素压入queue2中(当queue1不为空时),然后将queue2和queue1交换,使得后来的元素在队列queue1的第一个元素。

 

public class StackByQueue {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public StackByQueue(){
        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 isEmpty(){
        return queue1.isEmpty();
    }
}

n数之和

2数之和

给定一个数组nums和一个整数target,要求返回和为target的两个数组元素的下标。

双重for循环解决

第一层for循环确定一个数nums[i],第二层for循环确定另一个数nums[j]。

    public int[] twoSum(int[] nums,int target){
        int n = nums.length;
        for (int i = 0;i < n; i ++){
            for (int j = i + 1;j < n;j++){
                if (nums[i]+nums[j]==target){
                    return new int[]{i,j};
                }
            }
        }
        return new int[0];
    }

使用hash对时间复杂度进行优化,对数组nums进行遍历,如果hash表中的key不和target-nums[i]匹配,就将nums[i],i放入hash表,以保证不和自己匹配。


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

3数之和

找出nums数组中和为0的三个元素的值。

直接3层for循环时间复杂度为O(n^{3}),太高。先对数组排序,遇到相同的值就跳过,使得时间复杂度降低。进行遍历,先确定一个数nums[first],如果nums[first]==nums[first-1]&&first>0,说明值相同,使用continue遍历下一个值;令third=nums.length-1,target=-nums[first],对first后元素进行遍历,令second=first+1,如果nums[second]==nums[second-1]&&second<third,说明值相同,使用continue遍历下一个值;当second<third&&nums[third]+nums[second]>target时,third--,如果second==third,退出second的循环,如果nums[second]+nums[third]==target,找到一组答案。

public List<List<Integer>> threeSum(int[] nums){
        int n = nums.length;
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();
        for (int first = 0;first<n;++first){
            if (first > 0 && nums[first] == nums[first-1]) {
                continue;
            }
            int third= n-1;
            int target = -nums[first];
            for (int second = first+1;second<n;++second){
                if (second>first+1&&nums[second]==nums[second-1]){
                    continue;
                }
                //因为数组升序排列,所以对nums[second]+nums[third]>target进行判断
                while (second < third&&nums[second]+nums[third]>target){
                    --third;
                }
              
                if (second==third){
                    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;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值