[US Giants] 十. Data Structure

来源:互联网 发布:移动免费流量软件 编辑:程序博客网 时间:2024/05/29 09:25

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 1push(2)push(3)min()   // return 2push(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();    }}