【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 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 res = new ArrayList<>();
        for(int i = 0; i < numStr.length(); ++i) {
            res.add(Integer.valueOf(numStr.charAt(i)));
        }
        return res;
    }

    public List solution1(int[] A, int K) {
        //说白了就是获取相加的结果,那么现在只能通过对数据进行相加了,进位了
        //我们从最后面一个数开始加起,然后不断进位,吧值放到list中
        List 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 solution(int[] A) {
        //直接运算求解,然后对5取余
        int k = 0;List 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));
    }

}

 

你可能感兴趣的:(【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747)