力扣---2020.6.11

739. 每日温度

class Solution {
    public int[] dailyTemperatures(int[] T) {
        if(T==null||T.length==0){
            return null;
        }
        int[] res = new int[T.length];
        Stack<Integer> stack =new Stack<>();
        for(int i = 0;i<T.length;i++){
            while(!stack.isEmpty() && T[stack.peek()] < T[i]){
                int len = stack.pop();
                res[len] = i - len;
            }
            stack.add(i);
        }
        return res;
    }
}
/**
 * 根据题意,从最后一天推到第一天,这样会简单很多。因为最后一天显然不会再有升高的可能,结果直接为0。
 * 再看倒数第二天的温度,如果比倒数第一天低,那么答案显然为1,如果比倒数第一天高,又因为倒数第一天
 * 对应的结果为0,即表示之后不会再升高,所以倒数第二天的结果也应该为0。
 * 自此我们容易观察出规律,要求出第i天对应的结果,只需要知道第i+1天对应的结果就可以:
 * - 若T[i] < T[i+1],那么res[i]=1;
 * - 若T[i] > T[i+1]
 *   - res[i+1]=0,那么res[i]=0;
 *   - res[i+1]!=0,那就比较T[i]和T[i+1+res[i+1]](即将第i天的温度与比第i+1天大的那天的温度进行比较)
 */
class Solution {
    public int[] dailyTemperatures(int[] T) {
        int[] res = new int[T.length];
        res[T.length - 1] = 0;
        for (int i = T.length - 2; i >= 0; i--) {
            for (int j = i + 1; j < T.length; j += res[j]) {
                if (T[i] < T[j]) {
                    res[i] = j - i;
                    break;
                } else if (res[j] == 0) {
                    res[i] = 0;
                    break;
                }
            }
        }
    return res;
    }
}

面试题 10.11. 峰与谷

class Solution {
    public void wiggleSort(int[] nums) {
        int n = nums.length;
        for (int i = 1; i < n; i ++) {
            if (i % 2 == 0) {
                if (nums[i] < nums[i - 1]) {
                    int temp = nums[i - 1];
                    nums[i - 1] = nums[i];
                    nums[i] = temp;
                }
            } else {
                if (nums[i] > nums[i - 1]) {
                    int temp = nums[i - 1];
                    nums[i - 1] = nums[i];
                    nums[i] = temp;
                }
            }
        }
    }
}
class Solution {
    public void wiggleSort(int[] nums) {
        int l = nums.length;
        if (l < 3) return;
        Arrays.sort(nums);
        int i = 0, tmp;
        while (i < l - 1) {
            tmp = nums[i];
            nums[i] = nums[i+1];
            nums[i+1] = tmp;
            i += 2;
        }
    }
}

面试题 03.06. 动物收容所

class AnimalShelf 
{
    Queue<int[]> queue1;//for cats
    Queue<int[]> queue2;//for dogs
    public AnimalShelf() 
    {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }
    
    public void enqueue(int[] animal) 
    {
        if(animal[1]==0)queue1.offer(animal);
        else queue2.offer(animal);
    }
    
    public int[] dequeueAny() 
    {
        if(queue1.isEmpty()&&queue2.isEmpty())return new int[]{-1,-1};
        if(queue1.isEmpty()||queue2.isEmpty())
        {
            return queue1.isEmpty()?queue2.poll():queue1.poll();
        }
        int[] temp1=queue1.peek();
        int[] temp2=queue2.peek();
        if(temp1[0]<temp2[0])return queue1.poll();
        else return queue2.poll();
    }
    
    public int[] dequeueDog() 
    {
        if(queue2.isEmpty())return new int[]{-1,-1};
        return queue2.poll();
    }
    
    public int[] dequeueCat() 
    {
        if(queue1.isEmpty())return new int[]{-1,-1};
        return queue1.poll();
    }
}

你知道的越多,你不知道的越多。

你可能感兴趣的:(数据结构与算法)