[US Giants] 十. Data Structure

本文介绍如何在未排序整数数组中寻找最长连续元素序列的方法,并给出O(n)复杂度的算法实现。此外,还展示了如何通过两个栈实现队列的功能,以及如何构建带有最小值查询功能的栈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Clarification: Your algorithm should run in O(n) complexity.
Example

Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

思路:遍历数组每一个元素,并且对每一个元素值上下两边找它的连续序列个数

         因为要避免已经被数过的元素再次被用,因此加一个set,每个被用过的元素都从set里删除

         对每一个元素的序列个数进行比较,最大的就是所求最长。

注意:1. set经常被用来避免重复使用

         2. 由于每一个元素被加入set一次,从set删除的也是一次,因为复杂度为O(n)

public class Solution {
    /*
     * @param num: A list of integers
     * @return: An integer
     */
    public int longestConsecutive(int[] num) {
        if(num==null || num.length==0){
            return 0;
        }
        
        Set<Integer> set=new HashSet<>();
        for(int i=0;i<num.length;i++){
            set.add(num[i]);
        }
        
        int maxVal=Integer.MIN_VALUE;
        for(int i=0;i<num.length;i++){
            int down=num[i]-1;
            int up=num[i]+1; 
            while(set.contains(down)){              //每一个元素对应的连续序列的下边界
                set.remove(down);
                down--;
            }
            while(set.contains(up)){                //每一个元素对应的连续序列的上边界
                set.remove(up);
                up++;
            }
            maxVal=Math.max(maxVal,up-down-1);      //比较每一个元素的连续序列个数取最长
        }
        
        return maxVal;
    }
}
Implement Queue by Two Stacks
class MyQueue {
    private Stack<Integer> s1;
    private Stack<Integer> s2;

    /** Initialize your data structure here. */
    public MyQueue() {
        s1=new Stack<>();
        s2=new Stack<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        s1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(s2.isEmpty()){                    //对翻转栈为空的判断,因为如果不是空直接从2里拿出来就是,不用翻转
            switchStack();
        }
        return s2.pop();    
    }
    
    /** Get the front element. */
    public int peek() {
        if(s2.isEmpty()){
            switchStack();
        }
        return s2.peek();
    }
    
    private void switchStack(){
        while(!s1.isEmpty()){
            s2.push(s1.pop());
        }
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
Min Stack

Implement a stack with min() function, which will return the smallest number in the stack.

It should support push, pop and min operation all in O(1) cost.


min operation will never be called if there is no number in the stack.

Example
push(1)
pop()   // return 1
push(2)
push(3)
min()   // return 2
push(1)
min()   // return 1
public class MinStack {
    
    Stack<Integer> stack;
    Stack<Integer> minStack;
    
    public MinStack() {
        stack=new Stack<Integer>();
        minStack=new Stack<Integer>();
    }

    public void push(int number) {
        if(minStack.isEmpty() || minStack.peek()>=number){
            minStack.push(number);
        }
        stack.push(number);
    }

    public int pop() {
        if(stack.peek().equals(minStack.peek())){
            minStack.pop();
        }
        return stack.pop();                                    //一定要后stack.pop()
    }

    public int min() {
        return minStack.peek();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值