【LEETCODE】53、數組分類,簡單級別,題目:989、674、1018、724、840、747

真的感覺有點難。。。

這還是簡單級別。。。

我也是醉了

package y2019.Algorithm.array;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: AddToArrayForm
 * @Author: xiaof
 * @Description: TODO 989. Add to Array-Form of Integer
 * For a non-negative integer X, the array-form of X is an array of its digits in left to right order.
 * For example, if X = 1231, then the array form is [1,2,3,1].
 * Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
 *
 * Input: A = [1,2,0,0], K = 34
 * Output: [1,2,3,4]
 * Explanation: 1200 + 34 = 1234
 *
 * 對於非負整數 X 而言,X 的數組形式是每位數字按從左到右的順序形成的數組。例如,如果 X = 1231,那麼其數組形式爲 [1,2,3,1]。
 * 給定非負整數 X 的數組形式 A,返回整數 X+K 的數組形式。
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/add-to-array-form-of-integer
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 * @Date: 2019/7/11 17:50
 * @Version: 1.0
 */
public class AddToArrayForm {

    //但是數組很長的時候就涼了
    public List<Integer> solution(int[] A, int K) {
        //說白了就是獲取相加的結果
        StringBuffer value = new StringBuffer();
        for(int i = 0; i < A.length; ++i) {
            value.append("" + A[i]);
        }
        int num = Integer.valueOf(value.toString()) + K;
        //轉換爲int數組
        String numStr = String.valueOf(num);
        List<Integer> res = new ArrayList<>();
        for(int i = 0; i < numStr.length(); ++i) {
            res.add(Integer.valueOf(numStr.charAt(i)));
        }
        return res;
    }

    public List<Integer> solution1(int[] A, int K) {
        //說白了就是獲取相加的結果,那麼現在只能通過對數據進行相加了,進位了
        //我們從最後面一個數開始加起,然後不斷進位,吧值放到list中
        List<Integer> res = new ArrayList<>();
        for(int i = A.length - 1; i >= 0; --i) {
            //這裏要頭插,因爲我們從低位開始
            res.add(0, (A[i] + K) % 10);
            //吧進位值重新給K
            K = (A[i] + K) / 10;
        }

        //如果到最後K還是大於0,那麼繼續進位
        while(K > 0) {
            res.add(0, K % 10);
            K /= 10;
        }

        return res;

    }

}

 

package y2019.Algorithm.array;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: FindLengthOfLCIS
 * @Author: xiaof
 * @Description: TODO 674. Longest Continuous Increasing Subsequence
 * Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
 * Example 1:
 * Input: [1,3,5,4,7]
 * Output: 3
 * Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
 * Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
 *
 * 給定一個未經排序的整數數組,找到最長且連續的的遞增序列。
 *
 * @Date: 2019/7/11 9:53
 * @Version: 1.0
 */
public class FindLengthOfLCIS {

    public int solution(int[] nums) {

        if(nums == null || nums.length <= 0) {
            return 0;
        }

        //1.需要統計當前遞增的長度,2.保存最長遞增個數 每次和上一個比較,當不是遞增的時候,吧當前遞增格式和最大比較,並且清空當前長度
        int curIncreLen = 1, maxIncreLen = 0, preValue = nums[0];
        for(int i = 1; i < nums.length; ++i) {
            if(nums[i] > preValue) {
                ++curIncreLen;
            } else {
                //如果變成不是遞增的了
                maxIncreLen = Math.max(curIncreLen, maxIncreLen);
                curIncreLen = 1;
            }
            preValue = nums[i];
        }

        return Math.max(curIncreLen, maxIncreLen);
    }

    public static void main(String args[]) {
        int[] A = {1,3,5,7};
        int[] B = {1,3,5,4,2,3,4,5};
        int k = 1;
        System.out.println(new FindLengthOfLCIS().solution(B));
    }

}
package y2019.Algorithm.array;

import java.util.*;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: PrefixesDivBy5
 * @Author: xiaof
 * @Description: TODO 1018. Binary Prefix Divisible By 5
 * Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number
 * (from most-significant-bit to least-significant-bit.)
 * Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.
 *
 * Input: [0,1,1]
 * Output: [true,false,false]
 * Explanation:
 * The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10.  Only the first number is divisible by 5,
 * so answer[0] is true.
 *
 * 給定由若干 0 和 1 組成的數組 A。我們定義 N_i:從 A[0] 到 A[i] 的第 i 個子數組被解釋爲一個二進制數(從最高有效位到最低有效位)。
 * 返回布爾值列表 answer,只有當 N_i 可以被 5 整除時,答案 answer[i] 爲 true,否則爲 false。
 *
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/binary-prefix-divisible-by-5
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 * @Date: 2019/7/11 8:58
 * @Version: 1.0
 */
public class PrefixesDivBy5 {

    public List<Boolean> solution(int[] A) {
        //直接運算求解,然後對5取餘
        int k = 0;List<Boolean> res = new ArrayList<>();
        for(int i = 0; i < A.length; ++i) {
            //計算加入當前二進制的值,這裏要先進行對5取餘,不然會溢出
            k = ((k << 1) | A[i]) % 5;
            res.add(k == 0);
        }

        return res;
    }

    public static void main(String args[]) {
        int[] A = {1,0,0,1,0,1,0,0,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1,0,0,0,0,1,1,0,1,0,0,0,1};
        int[] B = {5};
        int k = 1;
        System.out.println(new PrefixesDivBy5().solution(A));
    }

}
package y2019.Algorithm.array;

import java.util.Arrays;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: PivotIndex
 * @Author: xiaof
 * @Description: TODO 724. Find Pivot Index
 * Given an array of integers nums, write a method that returns the "pivot" index of this array.
 * We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to
 * the right of the index.
 * If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
 *
 * Example 1:
 * Input:
 * nums = [1, 7, 3, 6, 5, 6]
 * Output: 3
 * Explanation:
 * The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
 * Also, 3 is the first index where this occurs.
 *
 * 我們是這樣定義數組中心索引的:數組中心索引的左側所有元素相加的和等於右側所有元素相加的和。
 * 如果數組不存在中心索引,那麼我們應該返回 -1。如果數組有多箇中心索引,那麼我們應該返回最靠近左邊的那一個。
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/find-pivot-index
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 *
 * @Date: 2019/7/11 18:08
 * @Version: 1.0
 */
public class PivotIndex {

    public int solution(int[] nums) {
        //1.獲取中位數
        int sum = (int) Arrays.stream(nums).sum();
        int cur = 0;
        //2.順序遍歷,獲取到和爲總和一半的位置結束
        int index = -1;
        for(int i = 0; i < nums.length; cur += nums[i++]) {
            if(cur * 2 == sum - nums[i]) {
                //這個就是中間
                index = i;
                break;
            }
        }
        return index;
    }

    public static void main(String args[]) {
        int[] A = {1,7,3,6,5,6};
        int k = 1;
        System.out.println(new PivotIndex().solution(A));
    }
}
package y2019.Algorithm.array;

/**
 * @ClassName NumMagicSquaresInside
 * @Description TODO 840. Magic Squares In Grid
 *
 * A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column,
 * and both diagonals all have the same sum.
 * Given an grid of integers, how many 3 x 3 "magic square" subgrids are there?  (Each subgrid is contiguous).
 *Input: [[4,3,8,4],
 *         [9,5,1,9],
 *         [2,7,6,2]]
 * Output: 1
 * Explanation:
 * The following subgrid is a 3 x 3 magic square:
 * 438
 * 951
 * 276
 * while this one is not:
 * 384
 * 519
 * 762
 * In total, there is only one magic square inside the given grid.
 *
 *3 x 3 的幻方是一個填充有從 1 到 9 的不同數字的 3 x 3 矩陣,其中每行,每列以及兩條對角線上的各數之和都相等。
 * 給定一個由整數組成的 grid,其中有多少個 3 × 3 的 “幻方” 子矩陣?(每個子矩陣都是連續的)。
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/magic-squares-in-grid
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 *
 *
 * @Author xiaof
 * @Date 2019/7/11 22:42
 * @Version 1.0
 **/
public class NumMagicSquaresInside {

    public int solution(int[][] grid) {
        //因爲每個數字都要有一個,那麼就是中間的位置爲5和爲15
        //找到5的位置

        if(grid == null || grid.length < 3 || grid[0].length < 3) {
            return 0;
        }

        int count = 0;
        for(int i = 1; i < grid.length - 1; ++i) {
            for(int j = 1; j < grid[i].length - 1; ++j) {
                //判斷這個位置是否是5,如果是,那麼再判斷是否是幻方
                if(grid[i][j] == 5 && isMagic(grid, i, j)) {
                    ++count;
                }
            }
        }


        return count;
    }

    //判斷是否是幻方
    private boolean isMagic(int[][] grid, int r, int c) {
        int[] times = new int[9]; //避免出現重複數字
        for(int i = -1; i < 2; ++i) {
            int rSum = 0, cSum = 0;
            for(int j = -1; j < 2; ++j) {
                //計算行和
                rSum += grid[r + i][c + j];
                cSum += grid[r + j][c + i]; //一列
                int num = grid[r + i][c + j];
                if(num > 9 || num < 1 || times[num]++ > 0) {
                    return false;
                }
            }
            //計算結果
            if(rSum != 15 || cSum != 15) {
                return false;
            }
        }
        return true;
    }
}
package y2019.Algorithm.array;

/**
 * @ClassName DominantIndex
 * @Description TODO 747. Largest Number At Least Twice of Others
 *
 * In a given integer array nums, there is always exactly one largest element.
 * Find whether the largest element in the array is at least twice as much as every other number in the array.
 * If it is, return the index of the largest element, otherwise return -1.
 *
 * Input: nums = [3, 6, 1, 0]
 * Output: 1
 * Explanation: 6 is the largest integer, and for every other number in the array x,
 * 6 is more than twice as big as x.  The index of value 6 is 1, so we return 1.
 *
 * @Author xiaof
 * @Date 2019/7/11 22:49
 * @Version 1.0
 **/
public class DominantIndex {

    public int solution(int[] nums) {
        Integer[] maxNums = {null, null};
        int maxIndex = 0;
        for(int i = 0; i < nums.length; ++i) {
            //跟max數組進行比較
            int curIndex = -1;
            for(int j = 0; j < maxNums.length; ++j) {
                if(maxNums[j] == null || nums[i] > maxNums[j]) {
                    //如果比較大,或者初始化的時候
                    ++curIndex;
                } else {
                    break;
                }
            }

            if(curIndex > -1) {
                //移動相應的數據,然後放入新的數據
                if(curIndex == 1) maxIndex = i;
                for(int k = 0; k < maxNums.length - 1; ++k) {
                    maxNums[k] = maxNums[k + 1];
                }
                maxNums[curIndex] = nums[i];
            }
        }

        //最後統計結果
        if(maxNums[0] != null && maxNums[1] != null && maxNums[0] != 0 && maxNums[1] != 0) {
            return maxNums[1] / maxNums[0] >= 0 ? maxIndex : -1;
        } else if(maxNums[1] != null && maxNums[1] != 0) {
            return maxIndex;
        } else {
            return -1;
        }
    }

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

}

 

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