真的感觉有点难。。。

这还是简单级别。。。

我也是醉了

  1. package y2019.Algorithm.array;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. /**
  8. * @ProjectName: cutter-point
  9. * @Package: y2019.Algorithm.array
  10. * @ClassName: AddToArrayForm
  11. * @Author: xiaof
  12. * @Description: TODO 989. Add to Array-Form of Integer
  13. * For a non-negative integer X, the array-form of X is an array of its digits in left to right order.
  14. * For example, if X = 1231, then the array form is [1,2,3,1].
  15. * Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
  16. *
  17. * Input: A = [1,2,0,0], K = 34
  18. * Output: [1,2,3,4]
  19. * Explanation: 1200 + 34 = 1234
  20. *
  21. * 对于非负整数 X 而言,X 的数组形式是每位数字按从左到右的顺序形成的数组。例如,如果 X = 1231,那么其数组形式为 [1,2,3,1]。
  22. * 给定非负整数 X 的数组形式 A,返回整数 X+K 的数组形式。
  23. * 来源:力扣(LeetCode)
  24. * 链接:https://leetcode-cn.com/problems/add-to-array-form-of-integer
  25. * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
  26. * @Date: 2019/7/11 17:50
  27. * @Version: 1.0
  28. */
  29. public class AddToArrayForm {
  30.  
  31. //但是数组很长的时候就凉了
  32. public List<Integer> solution(int[] A, int K) {
  33. //说白了就是获取相加的结果
  34. StringBuffer value = new StringBuffer();
  35. for(int i = 0; i < A.length; ++i) {
  36. value.append("" + A[i]);
  37. }
  38. int num = Integer.valueOf(value.toString()) + K;
  39. //转换为int数组
  40. String numStr = String.valueOf(num);
  41. List<Integer> res = new ArrayList<>();
  42. for(int i = 0; i < numStr.length(); ++i) {
  43. res.add(Integer.valueOf(numStr.charAt(i)));
  44. }
  45. return res;
  46. }
  47.  
  48. public List<Integer> solution1(int[] A, int K) {
  49. //说白了就是获取相加的结果,那么现在只能通过对数据进行相加了,进位了
  50. //我们从最后面一个数开始加起,然后不断进位,吧值放到list中
  51. List<Integer> res = new ArrayList<>();
  52. for(int i = A.length - 1; i >= 0; --i) {
  53. //这里要头插,因为我们从低位开始
  54. res.add(0, (A[i] + K) % 10);
  55. //吧进位值重新给K
  56. K = (A[i] + K) / 10;
  57. }
  58.  
  59. //如果到最后K还是大于0,那么继续进位
  60. while(K > 0) {
  61. res.add(0, K % 10);
  62. K /= 10;
  63. }
  64.  
  65. return res;
  66.  
  67. }
  68.  
  69. }
  1. package y2019.Algorithm.array;
  2.  
  3. /**
  4. * @ProjectName: cutter-point
  5. * @Package: y2019.Algorithm.array
  6. * @ClassName: FindLengthOfLCIS
  7. * @Author: xiaof
  8. * @Description: TODO 674. Longest Continuous Increasing Subsequence
  9. * Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
  10. * Example 1:
  11. * Input: [1,3,5,4,7]
  12. * Output: 3
  13. * Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
  14. * 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.
  15. *
  16. * 给定一个未经排序的整数数组,找到最长且连续的的递增序列。
  17. *
  18. * @Date: 2019/7/11 9:53
  19. * @Version: 1.0
  20. */
  21. public class FindLengthOfLCIS {
  22.  
  23. public int solution(int[] nums) {
  24.  
  25. if(nums == null || nums.length <= 0) {
  26. return 0;
  27. }
  28.  
  29. //1.需要统计当前递增的长度,2.保存最长递增个数 每次和上一个比较,当不是递增的时候,吧当前递增格式和最大比较,并且清空当前长度
  30. int curIncreLen = 1, maxIncreLen = 0, preValue = nums[0];
  31. for(int i = 1; i < nums.length; ++i) {
  32. if(nums[i] > preValue) {
  33. ++curIncreLen;
  34. } else {
  35. //如果变成不是递增的了
  36. maxIncreLen = Math.max(curIncreLen, maxIncreLen);
  37. curIncreLen = 1;
  38. }
  39. preValue = nums[i];
  40. }
  41.  
  42. return Math.max(curIncreLen, maxIncreLen);
  43. }
  44.  
  45. public static void main(String args[]) {
  46. int[] A = {1,3,5,7};
  47. int[] B = {1,3,5,4,2,3,4,5};
  48. int k = 1;
  49. System.out.println(new FindLengthOfLCIS().solution(B));
  50. }
  51.  
  52. }
  1. package y2019.Algorithm.array;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6. * @ProjectName: cutter-point
  7. * @Package: y2019.Algorithm.array
  8. * @ClassName: PrefixesDivBy5
  9. * @Author: xiaof
  10. * @Description: TODO 1018. Binary Prefix Divisible By 5
  11. * 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
  12. * (from most-significant-bit to least-significant-bit.)
  13. * Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.
  14. *
  15. * Input: [0,1,1]
  16. * Output: [true,false,false]
  17. * Explanation:
  18. * 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,
  19. * so answer[0] is true.
  20. *
  21. * 给定由若干 0 和 1 组成的数组 A。我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位)。
  22. * 返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false。
  23. *
  24. * 来源:力扣(LeetCode)
  25. * 链接:https://leetcode-cn.com/problems/binary-prefix-divisible-by-5
  26. * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
  27. * @Date: 2019/7/11 8:58
  28. * @Version: 1.0
  29. */
  30. public class PrefixesDivBy5 {
  31.  
  32. public List<Boolean> solution(int[] A) {
  33. //直接运算求解,然后对5取余
  34. int k = 0;List<Boolean> res = new ArrayList<>();
  35. for(int i = 0; i < A.length; ++i) {
  36. //计算加入当前二进制的值,这里要先进行对5取余,不然会溢出
  37. k = ((k << 1) | A[i]) % 5;
  38. res.add(k == 0);
  39. }
  40.  
  41. return res;
  42. }
  43.  
  44. public static void main(String args[]) {
  45. 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};
  46. int[] B = {5};
  47. int k = 1;
  48. System.out.println(new PrefixesDivBy5().solution(A));
  49. }
  50.  
  51. }
  1. package y2019.Algorithm.array;
  2.  
  3. import java.util.Arrays;
  4.  
  5. /**
  6. * @ProjectName: cutter-point
  7. * @Package: y2019.Algorithm.array
  8. * @ClassName: PivotIndex
  9. * @Author: xiaof
  10. * @Description: TODO 724. Find Pivot Index
  11. * Given an array of integers nums, write a method that returns the "pivot" index of this array.
  12. * 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
  13. * the right of the index.
  14. * If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
  15. *
  16. * Example 1:
  17. * Input:
  18. * nums = [1, 7, 3, 6, 5, 6]
  19. * Output: 3
  20. * Explanation:
  21. * 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.
  22. * Also, 3 is the first index where this occurs.
  23. *
  24. * 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。
  25. * 如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。
  26. * 来源:力扣(LeetCode)
  27. * 链接:https://leetcode-cn.com/problems/find-pivot-index
  28. * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
  29. *
  30. * @Date: 2019/7/11 18:08
  31. * @Version: 1.0
  32. */
  33. public class PivotIndex {
  34.  
  35. public int solution(int[] nums) {
  36. //1.获取中位数
  37. int sum = (int) Arrays.stream(nums).sum();
  38. int cur = 0;
  39. //2.顺序遍历,获取到和为总和一半的位置结束
  40. int index = -1;
  41. for(int i = 0; i < nums.length; cur += nums[i++]) {
  42. if(cur * 2 == sum - nums[i]) {
  43. //这个就是中间
  44. index = i;
  45. break;
  46. }
  47. }
  48. return index;
  49. }
  50.  
  51. public static void main(String args[]) {
  52. int[] A = {1,7,3,6,5,6};
  53. int k = 1;
  54. System.out.println(new PivotIndex().solution(A));
  55. }
  56. }
  1. package y2019.Algorithm.array;
  2.  
  3. /**
  4. * @ClassName NumMagicSquaresInside
  5. * @Description TODO 840. Magic Squares In Grid
  6. *
  7. * 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,
  8. * and both diagonals all have the same sum.
  9. * Given an grid of integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous).
  10. *Input: [[4,3,8,4],
  11. * [9,5,1,9],
  12. * [2,7,6,2]]
  13. * Output: 1
  14. * Explanation:
  15. * The following subgrid is a 3 x 3 magic square:
  16. * 438
  17. * 951
  18. * 276
  19. * while this one is not:
  20. * 384
  21. * 519
  22. * 762
  23. * In total, there is only one magic square inside the given grid.
  24. *
  25. *3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。
  26. * 给定一个由整数组成的 grid,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。
  27. * 来源:力扣(LeetCode)
  28. * 链接:https://leetcode-cn.com/problems/magic-squares-in-grid
  29. * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
  30. *
  31. *
  32. * @Author xiaof
  33. * @Date 2019/7/11 22:42
  34. * @Version 1.0
  35. **/
  36. public class NumMagicSquaresInside {
  37.  
  38. public int solution(int[][] grid) {
  39. //因为每个数字都要有一个,那么就是中间的位置为5和为15
  40. //找到5的位置
  41.  
  42. if(grid == null || grid.length < 3 || grid[0].length < 3) {
  43. return 0;
  44. }
  45.  
  46. int count = 0;
  47. for(int i = 1; i < grid.length - 1; ++i) {
  48. for(int j = 1; j < grid[i].length - 1; ++j) {
  49. //判断这个位置是否是5,如果是,那么再判断是否是幻方
  50. if(grid[i][j] == 5 && isMagic(grid, i, j)) {
  51. ++count;
  52. }
  53. }
  54. }
  55.  
  56. return count;
  57. }
  58.  
  59. //判断是否是幻方
  60. private boolean isMagic(int[][] grid, int r, int c) {
  61. int[] times = new int[9]; //避免出现重复数字
  62. for(int i = -1; i < 2; ++i) {
  63. int rSum = 0, cSum = 0;
  64. for(int j = -1; j < 2; ++j) {
  65. //计算行和
  66. rSum += grid[r + i][c + j];
  67. cSum += grid[r + j][c + i]; //一列
  68. int num = grid[r + i][c + j];
  69. if(num > 9 || num < 1 || times[num]++ > 0) {
  70. return false;
  71. }
  72. }
  73. //计算结果
  74. if(rSum != 15 || cSum != 15) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. }
  1. package y2019.Algorithm.array;
  2.  
  3. /**
  4. * @ClassName DominantIndex
  5. * @Description TODO 747. Largest Number At Least Twice of Others
  6. *
  7. * In a given integer array nums, there is always exactly one largest element.
  8. * Find whether the largest element in the array is at least twice as much as every other number in the array.
  9. * If it is, return the index of the largest element, otherwise return -1.
  10. *
  11. * Input: nums = [3, 6, 1, 0]
  12. * Output: 1
  13. * Explanation: 6 is the largest integer, and for every other number in the array x,
  14. * 6 is more than twice as big as x. The index of value 6 is 1, so we return 1.
  15. *
  16. * @Author xiaof
  17. * @Date 2019/7/11 22:49
  18. * @Version 1.0
  19. **/
  20. public class DominantIndex {
  21.  
  22. public int solution(int[] nums) {
  23. Integer[] maxNums = {null, null};
  24. int maxIndex = 0;
  25. for(int i = 0; i < nums.length; ++i) {
  26. //跟max数组进行比较
  27. int curIndex = -1;
  28. for(int j = 0; j < maxNums.length; ++j) {
  29. if(maxNums[j] == null || nums[i] > maxNums[j]) {
  30. //如果比较大,或者初始化的时候
  31. ++curIndex;
  32. } else {
  33. break;
  34. }
  35. }
  36.  
  37. if(curIndex > -1) {
  38. //移动相应的数据,然后放入新的数据
  39. if(curIndex == 1) maxIndex = i;
  40. for(int k = 0; k < maxNums.length - 1; ++k) {
  41. maxNums[k] = maxNums[k + 1];
  42. }
  43. maxNums[curIndex] = nums[i];
  44. }
  45. }
  46.  
  47. //最后统计结果
  48. if(maxNums[0] != null && maxNums[1] != null && maxNums[0] != 0 && maxNums[1] != 0) {
  49. return maxNums[1] / maxNums[0] >= 0 ? maxIndex : -1;
  50. } else if(maxNums[1] != null && maxNums[1] != 0) {
  51. return maxIndex;
  52. } else {
  53. return -1;
  54. }
  55. }
  56.  
  57. public static void main(String args[]) {
  58. int[] A = {0,0,3,2};
  59. int k = 1;
  60. System.out.println(new DominantIndex().solution(A));
  61. }
  62.  
  63. }

【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747的更多相关文章

  1. 【LEETCODE】54、数组分类,简单级别,题目:605、532

    数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...

  2. 【LeetCode】数组-1(643)-返回规定长度k的最大子数组的平均数

    好久没有刷LeetCode了,准备重拾并坚持下去,每天刷个两小时.今天算是开始的第一天,不过出师不利,在一道很简单的题目上墨迹半天.不过还好,现在踩过的坑,应该都不会白踩,这些可能都是以后程序员路上稳 ...

  3. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  4. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  5. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

  6. LeetCode~移除元素(简单)

    移除元素(简单) 1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使 ...

  7. 面阿里P7,竟问这么简单的题目?

    关于作者:程序猿石头(ID: tangleithu),来自十八县贫困农村(查看我的逆袭之路),BAT某厂P7,是前大疆(无人机)技术主管,曾经也在创业公司待过,有着丰富的经验. 本文首发于微信公众号, ...

  8. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  9. LeetCode: 53. Maximum Subarray(Easy)

    1. 原题链接 https://leetcode.com/problems/maximum-subarray/discuss/ 2. 题目要求 给定一个整型数组,返回其子串之和的最大值 例如,[-2, ...

随机推荐

  1. RookeyFrame bin 目录

    如果把bin目录删掉,重新生成的话,还需要加载很多东西哦,具体可以对比一下下载下来的文件

  2. codevs 1814 最长链题解

    codevs 1814 最长链题解 题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. 输入描述 Input Descripti ...

  3. 用Visual Studio编写UDF的一点小技巧(自动补全宏函数、变量)

    下载Visual Studio,安装VS 下载番茄助手(Visual Assist X),链接:www.wholetomato.com,然后安装番茄助手 打开VS

  4. rust数据类型

    fn main() { //char支持4个字节,支持emoji let jp = "ゆ"; let emoji = "✨"; let ch = "囧 ...

  5. 第06组 Beta冲刺(3/5)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 继续维护后端代码 学习深入python 研究匿名拨打电话问题.套牌多结果处理问题 Git ...

  6. Spark安装(standalone)

    文档:http://spark.apache.org/docs/latest/spark-standalone.html 安装scalahttps://www.scala-lang.org/downl ...

  7. TOMCAT到底能 承受多少并发,并发量计算你方法

        TOMCAT 可以稳定支持的最大并发用户数 https://www.jianshu.com/p/d306826aef7a tomcat并发数优化maxThreads.acceptCount(最 ...

  8. MySQL日常监控及sys库的使用【转】

    一.统计信息(SQL维度) 关于SQL维度的统计信息主要集中在events_statements_summary_by_digest表中,通过将SQL语句抽象出digest,可以统计某类SQL语句在各 ...

  9. 安防视频互联网化的EasyDSS流媒体服务器不但能Easy安防流媒体的开发而且能更加互联网化视频协议的输出

    开发EasyDSS的初衷 自从12年开始做EasyDarwin的时候,当时眼光一直都仅仅局限在安防监控视频这一块,对RTMP没有太大的重视,对于后起之秀HLS更是没有太多关注,然而经历了15直播火热的 ...

  10. 相位展开(phase unwrapping)算法研究与实践

    1. 什么是相位展开? 相位展开(Phase Unwrapping)是一个经典的信号处理问题,它指的是从值区间中恢复原始相位值(原因在于:计算相位时,运用反正切函数,则相位图中提取的相位都是包裹在一个 ...