【LEETCODE】48、數組分類,簡單級別,題目:189,217,219,268,283,414

package y2019.Algorithm.array;

import java.util.Arrays;
import java.util.Stack;

/**
 * @ClassName Rotate
 * @Description TODO  189. Rotate Array
 *
 * Given an array, rotate the array to the right by k steps, where k is non-negative.
 *
 * Input: [1,2,3,4,5,6,7] and k = 3
 * Output: [5,6,7,1,2,3,4]
 * Explanation:
 * rotate 1 steps to the right: [7,1,2,3,4,5,6]
 * rotate 2 steps to the right: [6,7,1,2,3,4,5]
 * rotate 3 steps to the right: [5,6,7,1,2,3,4]
 *
 * @Author xiaof
 * @Date 2019/7/6 18:44
 * @Version 1.0
 **/
public class Rotate {

    public void solution(int[] nums, int k) {

        //這裏移動的意思就是把末尾循環遍歷到前面,那麼只要倒着遍歷,然後跳轉到開始就可以了
        int index = nums.length - k % nums.length; //開始讀取的位置
        int[] nums2 = new int[nums.length];
        int i = 0;
        while(i < nums.length) {
            if(index >= nums.length) {
                index = 0;
            }
            nums2[i++] = nums[index++];
        }
        //複製到nums中
        System.arraycopy(nums2, 0, nums, 0, nums.length);

    }

    public static void main(String args[]) {
        int A1[] = {-1};
        int k = 2;
        Rotate fuc = new Rotate();
        fuc.solution(A1, k);
        System.out.println();
    }

}

 

package y2019.Algorithm.array;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName ContainsDuplicate
 * @Description TODO  217. Contains Duplicate
 *
 * Given an array of integers, find if the array contains any duplicates.
 * Your function should return true if any value appears at least twice in the array,
 * and it should return false if every element is distinct.
 *
 * Input: [1,2,3,1]
 * Output: true
 * Example 2:
 *
 * @Author xiaof
 * @Date 2019/7/6 20:31
 * @Version 1.0
 **/
public class ContainsDuplicate {

    public boolean solution(int[] nums) {

        //判斷是否有重複,很簡單用set
        Set set = new HashSet();
        for(int a : nums) {
            if(set.contains(a)) {
                return true;
            } else {
                set.add(a);
            }
        }

        return false;
    }

}

 

package y2019.Algorithm.array;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName ContainsNearbyDuplicate
 * @Description TODO 219. Contains Duplicate II
 * Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such
 * that nums[i] = nums[j] and the absolute difference between i and j is at most k.
 *
 * Input: nums = [1,2,3,1], k = 3
 * Output: true
 *
 * Input: nums = [1,0,1,1], k = 1
 * Output: true
 *
 * Input: nums = [1,2,3,1,2,3], k = 2
 * Output: false
 *
 * @Author xiaof
 * @Date 2019/7/6 20:36
 * @Version 1.0
 **/
public class ContainsNearbyDuplicate {

    public boolean solution(int[] nums, int k) {
        //1.因爲範圍內是k
        //那麼可以設置一個set,每次存放一個k範圍內的值,超出部分丟掉即可
        Set set = new HashSet();
        for(int i = 0; i < nums.length; ++i) {
            if(i > k) {
                //如果超出範圍,那麼把範圍外去掉
                set.remove(nums[i - k - 1]);
            }
            //判斷是否有重複
            if(set.contains(nums[i])) {
                return true;
            } else {
                set.add(nums[i]);
            }
        }

        return false;
    }

    public static void main(String args[]) {
        int A1[] = {1,2,3,1,2,3};
        int k = 2;
        ContainsNearbyDuplicate fuc = new ContainsNearbyDuplicate();
        fuc.solution(A1, k);
        System.out.println();
    }

}

 

package y2019.Algorithm.array;

/**
 * @ClassName MissingNumber
 * @Description TODO 268. Missing Number
 * Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
 * Input: [3,0,1]
 * Output: 2
 *
 * Input: [9,6,4,2,3,5,7,0,1]
 * Output: 8
 * @Author xiaof
 * @Date 2019/7/6 21:09
 * @Version 1.0
 **/
public class MissingNumber {

    //仔細看題,都是從0開始的,那麼要計算從0開始到nums.length之間少的那個,我們只要算出總值減去所有哦值即可
    public int solution(int[] nums) {
        int sum = (nums.length + 1) * nums.length / 2;
        for(int i = 0; i < nums.length; ++i) {
            sum -= nums[i];
        }
        return sum;
    }

    public static void main(String args[]) {
        int A1[] = {3,0,1};
        int k = 2;
        MissingNumber fuc = new MissingNumber();
        fuc.solution(A1);
        System.out.println();
    }

}

 

package y2019.Algorithm.array;

/**
 * @ClassName MoveZeroes
 * @Description TODO 283. Move Zeroes283. Move Zeroes
 * Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
 * Input: [0,1,0,3,12]
 * Output: [1,3,12,0,0]
 * @Author xiaof
 * @Date 2019/7/6 21:42
 * @Version 1.0
 **/
public class MoveZeroes {

    public void solution(int[] nums) {
        //哎,有的時候不能想太多,這個時候就得用2個數組
        int[] newNums = new int[nums.length];
        int indexNewNums = 0;
        for(int n : nums) {
            if(n != 0) {
                newNums[indexNewNums++] = n;
            }
        }
        //最後補上0
        for(; indexNewNums < nums.length; ++indexNewNums) {
            newNums[indexNewNums] = 0;
        }

        //拷貝回去
        System.arraycopy(newNums, 0, nums, 0, nums.length);

    }

    public static void main(String args[]) {
        int A1[] = {1,0};
        int k = 2;
        MoveZeroes fuc = new MoveZeroes();
        fuc.solution(A1);
        System.out.println();
    }

}

 

package y2019.Algorithm.array;

/**
 * @ClassName ThirdMax
 * @Description TODO 414. Third Maximum Number
 * Given a non-empty array of integers, return the third maximum number in this array.
 * If it does not exist, return the maximum number. The time complexity must be in O(n).
 *
 * Input: [3, 2, 1]
 * Output: 1
 * Explanation: The third maximum is 1.
 *
 * @Author xiaof
 * @Date 2019/7/6 22:40
 * @Version 1.0
 **/
public class ThirdMax {

    //尋找第三大的數據
    public int solution(int[] nums) {
        //我們定義長度爲3的數組,用來存放前三大的數據,最後0位就是第三大的數據
        Integer[] maxNum = new Integer[3];
        //初始化數據
        for(int i = 0; i < maxNum.length; ++i) {
            maxNum[i] = null;
        }

        for(int i = 0; i < nums.length; ++i) {
            //依次和前三比較
            int index = 0;
            for(int j = 0; j < maxNum.length; ++j) {
                if(maxNum[j] == null || maxNum[j] < nums[i]) {
                    ++index;
                } else if (nums[i] == maxNum[j]) {
                    //去除重複數據入列
                    index = -1;
                    break;
                } else {
                    break;//如果比max裏面的位置小那麼直接跳出
                }
            }

            if(index > 0) {
                //修改位置
                for(int k = 0; k < index - 1; ++k) {
                    //前面幾位從新排序
                    maxNum[k] = maxNum[k+1];
                }
                maxNum[index - 1] = nums[i];
            }
        }

        //如果不存在第三大的,那麼就獲取最大的
        int max = maxNum[0] == null ? maxNum[2] : maxNum[0];
        return max;

    }

    public static void main(String args[]) {
        int A1[] = {1,2,-2147483648};
        int k = 2;
        ThirdMax fuc = new ThirdMax();
        fuc.solution(A1);
        System.out.println();
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章