【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));
- }
- }
【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747的更多相关文章
- 【LEETCODE】54、数组分类,简单级别,题目:605、532
数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...
- 【LeetCode】数组-1(643)-返回规定长度k的最大子数组的平均数
好久没有刷LeetCode了,准备重拾并坚持下去,每天刷个两小时.今天算是开始的第一天,不过出师不利,在一道很简单的题目上墨迹半天.不过还好,现在踩过的坑,应该都不会白踩,这些可能都是以后程序员路上稳 ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)
这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...
- LeetCode~移除元素(简单)
移除元素(简单) 1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使 ...
- 面阿里P7,竟问这么简单的题目?
关于作者:程序猿石头(ID: tangleithu),来自十八县贫困农村(查看我的逆袭之路),BAT某厂P7,是前大疆(无人机)技术主管,曾经也在创业公司待过,有着丰富的经验. 本文首发于微信公众号, ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- LeetCode: 53. Maximum Subarray(Easy)
1. 原题链接 https://leetcode.com/problems/maximum-subarray/discuss/ 2. 题目要求 给定一个整型数组,返回其子串之和的最大值 例如,[-2, ...
随机推荐
- RookeyFrame bin 目录
如果把bin目录删掉,重新生成的话,还需要加载很多东西哦,具体可以对比一下下载下来的文件
- codevs 1814 最长链题解
codevs 1814 最长链题解 题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. 输入描述 Input Descripti ...
- 用Visual Studio编写UDF的一点小技巧(自动补全宏函数、变量)
下载Visual Studio,安装VS 下载番茄助手(Visual Assist X),链接:www.wholetomato.com,然后安装番茄助手 打开VS
- rust数据类型
fn main() { //char支持4个字节,支持emoji let jp = "ゆ"; let emoji = "✨"; let ch = "囧 ...
- 第06组 Beta冲刺(3/5)
队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 继续维护后端代码 学习深入python 研究匿名拨打电话问题.套牌多结果处理问题 Git ...
- Spark安装(standalone)
文档:http://spark.apache.org/docs/latest/spark-standalone.html 安装scalahttps://www.scala-lang.org/downl ...
- TOMCAT到底能 承受多少并发,并发量计算你方法
TOMCAT 可以稳定支持的最大并发用户数 https://www.jianshu.com/p/d306826aef7a tomcat并发数优化maxThreads.acceptCount(最 ...
- MySQL日常监控及sys库的使用【转】
一.统计信息(SQL维度) 关于SQL维度的统计信息主要集中在events_statements_summary_by_digest表中,通过将SQL语句抽象出digest,可以统计某类SQL语句在各 ...
- 安防视频互联网化的EasyDSS流媒体服务器不但能Easy安防流媒体的开发而且能更加互联网化视频协议的输出
开发EasyDSS的初衷 自从12年开始做EasyDarwin的时候,当时眼光一直都仅仅局限在安防监控视频这一块,对RTMP没有太大的重视,对于后起之秀HLS更是没有太多关注,然而经历了15直播火热的 ...
- 相位展开(phase unwrapping)算法研究与实践
1. 什么是相位展开? 相位展开(Phase Unwrapping)是一个经典的信号处理问题,它指的是从值区间中恢复原始相位值(原因在于:计算相位时,运用反正切函数,则相位图中提取的相位都是包裹在一个 ...