1. package y2019.Algorithm.array;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5.  
  6. /**
  7. * @ProjectName: cutter-point
  8. * @Package: y2019.Algorithm.array
  9. * @ClassName: FairCandySwap
  10. * @Author: xiaof
  11. * @Description: TODO 888. Fair Candy Swap
  12. *
  13. * Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has,
  14. * and B[j] is the size of the j-th bar of candy that Bob has.
  15. * Since they are friends, they would like to exchange one candy bar each so that after the exchange,
  16. * they both have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.)
  17. * Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange,
  18. * and ans[1] is the size of the candy bar that Bob must exchange.
  19. * If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.
  20. *
  21. * Input: A = [1,1], B = [2,2]
  22. * Output: [1,2]
  23. *
  24. * 交换A,B两个数组中的一个数字,使得两个数组的和相等。要返回的结果是个要交换的两个数字,分别来自A,B。
  25. *
  26. * @Date: 2019/7/8 9:25
  27. * @Version: 1.0
  28. */
  29. public class FairCandySwap {
  30.  
  31. public int[] solution(int[] A, int[] B) {
  32. //说白了对数组求和,然后求两个数组的平均数的差值,然后看看是否存在这个差值
  33. Set seta = new HashSet();
  34. int[] result = new int[2];
  35. int suma = 0, sumb = 0;
  36.  
  37. for(int i = 0; i < A.length; ++i) {
  38. seta.add(A[i]);
  39. suma += A[i];
  40. }
  41.  
  42. for(int i = 0; i < B.length; ++i) {
  43. sumb += B[i];
  44. }
  45. //求两边数据跟平均数的差值,两边的和的平均数就是两边需要到达的数据
  46. //现在求B距离这个平均数的差距
  47. int dif = (suma + sumb) / 2 - sumb;
  48. //然后我们再第二个数组中找,看是否存在正好对应的补上
  49. for(int i = 0; i < B.length; ++i) {
  50. //获取对应i的数据值,判断B加上这个差距值,判断A中是否存在
  51. //因为吧b[i]移动过去之后,还要减去B[i]的值
  52. if(seta.contains(dif + B[i])) {
  53. //看看A中是否包含这个差值,如果包含,那么就可以返回了
  54. result[0] = dif + B[i];
  55. result[1] = B[i];
  56. // break;
  57. }
  58. }
  59.  
  60. return result;
  61.  
  62. }
  63.  
  64. public static void main(String args[]) {
  65. int[] A = {1,2};
  66. int[] B = {2,3};
  67.  
  68. System.out.println(new FairCandySwap().solution(A, B));
  69. }
  70.  
  71. }
  1. package y2019.Algorithm.array;
  2.  
  3. import java.util.stream.IntStream;
  4.  
  5. /**
  6. * @ProjectName: cutter-point
  7. * @Package: y2019.Algorithm.array
  8. * @ClassName: CanThreePartsEqualSum
  9. * @Author: xiaof
  10. * @Description: TODO 1013. Partition Array Into Three Parts With Equal Sum
  11. * Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.
  12. * Formally, we can partition the array
  13. * if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])
  14. *
  15. * Input: [0,2,1,-6,6,-7,9,1,2,0,1]
  16. * Output: true
  17. * Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
  18. *
  19. * 题目的意思应该是平均吧数据分成三等分,每份的和相同,并且每份数据要求是连续的
  20. *
  21. * @Date: 2019/7/9 9:18
  22. * @Version: 1.0
  23. */
  24. public class CanThreePartsEqualSum {
  25.  
  26. public boolean solution(int[] A) {
  27. int sum = IntStream.of(A).sum(); //求和
  28. //分成三份
  29. int threeFen = sum / 3;
  30. //我们连续操作,获取,直到获取到三份
  31. boolean result = false;
  32.  
  33. int index = 0;
  34. int tempSum = 0;
  35. int times = 0;
  36. while(index < A.length) {
  37. tempSum += A[index];
  38. if(tempSum == threeFen) {
  39. times++;
  40. tempSum = 0;
  41. }
  42.  
  43. ++index;
  44. }
  45.  
  46. if(times == 3) {
  47. result = true;
  48. }
  49.  
  50. return result;
  51. }
  52.  
  53. }
  1. package y2019.Algorithm.array;
  2.  
  3. /**
  4. * @ProjectName: cutter-point
  5. * @Package: y2019.Algorithm.array
  6. * @ClassName: IsMonotonic
  7. * @Author: xiaof
  8. * @Description: TODO 896. Monotonic Array
  9. * An array is monotonic if it is either monotone increasing or monotone decreasing.
  10. * An array A is monotone increasing if for all i <= j, A[i] <= A[j].
  11. * An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
  12. * Return true if and only if the given array A is monotonic.
  13. *
  14. * Input: [1,2,2,3]
  15. * Output: true
  16. *
  17. * 根据题意,这个数组应该是单向数组,递增,或者递减
  18. *
  19. * @Date: 2019/7/9 9:31
  20. * @Version: 1.0
  21. */
  22. public class IsMonotonic {
  23.  
  24. public boolean solution(int[] A) {
  25. int direct = 0; //数组变化方向
  26. int curNum = A[0];
  27.  
  28. for(int i = 1; i < A.length; ++i) {
  29.  
  30. if(A[i] > curNum && (direct == 0 || direct == 1)) {
  31. direct = 1;
  32. curNum = A[i];
  33. } else if(A[i] < curNum && (direct == 0 || direct == 2)) {
  34. direct = 2;
  35. curNum = A[i];
  36. } else if (A[i] == curNum) {
  37. curNum = A[i];
  38. } else {
  39. return false;
  40. }
  41. }
  42.  
  43. return true;
  44.  
  45. }
  46.  
  47. public static void main(String args[]) {
  48. int[] A = {1,1,0};
  49. int[] B = {2,3};
  50.  
  51. System.out.println(new IsMonotonic().solution(A));
  52. }
  53.  
  54. }
  1. package y2019.Algorithm.array;
  2.  
  3. /**
  4. * @ProjectName: cutter-point
  5. * @Package: y2019.Algorithm.array
  6. * @ClassName: FindMaxConsecutiveOnes
  7. * @Author: xiaof
  8. * @Description: TODO 485. Max Consecutive Ones
  9. * Given a binary array, find the maximum number of consecutive 1s in this array.
  10. *
  11. * Input: [1,1,0,1,1,1]
  12. * Output: 3
  13. * Explanation: The first two digits or the last three digits are consecutive 1s.
  14. * The maximum number of consecutive 1s is 3.
  15. *
  16. * 统计最大1连续出现次数
  17. *
  18. * @Date: 2019/7/9 10:17
  19. * @Version: 1.0
  20. */
  21. public class FindMaxConsecutiveOnes {
  22.  
  23. public int solution(int[] nums) {
  24.  
  25. int maxTimes = 0;
  26. int cnt = 0;
  27. for(int i = 0; i < nums.length; ++i) {
  28. if(nums[i] == 1) {
  29. cnt++;
  30. } else {
  31. if(cnt > maxTimes) {
  32. maxTimes = cnt;
  33. }
  34. cnt = 0;
  35. }
  36. }
  37.  
  38. //如果一直延续到循环末尾还是还是没有else,那么这里做最后拦截
  39. if(cnt > maxTimes) {
  40. maxTimes = cnt;
  41. }
  42.  
  43. return maxTimes;
  44. }
  45.  
  46. }
  1. package y2019.Algorithm.array;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.stream.IntStream;
  6.  
  7. /**
  8. * @ProjectName: cutter-point
  9. * @Package: y2019.Algorithm.array
  10. * @ClassName: FindDisappearedNumbers
  11. * @Author: xiaof
  12. * @Description: TODO 448. Find All Numbers Disappeared in an Array
  13. * Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
  14. * Find all the elements of [1, n] inclusive that do not appear in this array.
  15. * Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
  16. *
  17. * Input:
  18. * [4,3,2,7,8,2,3,1]
  19. *
  20. * Output:
  21. * [5,6]
  22. *
  23. * @Date: 2019/7/9 10:25
  24. * @Version: 1.0
  25. */
  26. public class FindDisappearedNumbers {
  27.  
  28. public List<Integer> solution(int[] nums) {
  29. List ret = new ArrayList();
  30. for(int i = 0; i < nums.length; ++i) {
  31. int val = Math.abs(nums[i]) - 1; //用来做下标位置
  32. if(nums[val] > 0) {
  33. nums[val] = -nums[val];//标记val大小的数据
  34. }
  35. }
  36.  
  37. //遍历获取没有标记的数据
  38. for(int i = 0; i < nums.length; ++i) {
  39. if(nums[i] > 0) {
  40. ret.add(i + 1); //这个位置是没有标记到的
  41. }
  42. }
  43.  
  44. return ret;
  45. }
  46.  
  47. public static void main(String args[]) {
  48. int[] A = {4,3,2,7,8,2,3,1};
  49. int[] B = {2,3};
  50.  
  51. System.out.println(new FindDisappearedNumbers().solution(A));
  52. }
  53.  
  54. }
  1. package y2019.Algorithm.array;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /**
  7. * @ProjectName: cutter-point
  8. * @Package: y2019.Algorithm.array
  9. * @ClassName: FindShortestSubArray
  10. * @Author: xiaof
  11. * @Description: TODO 697. Degree of an Array
  12. * Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any
  13. * one of its elements.
  14. * Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
  15. *
  16. * Input: [1, 2, 2, 3, 1]
  17. * Output: 2
  18. * Explanation:
  19. * The input array has a degree of 2 because both elements 1 and 2 appear twice.
  20. * Of the subarrays that have the same degree:
  21. * [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
  22. * The shortest length is 2. So return 2.
  23. *
  24. * 最短的子串使得其包含出现次数最多的元素,子串中刚好包含出现次数最多的数字
  25. *
  26. * @Date: 2019/7/9 14:06
  27. * @Version: 1.0
  28. */
  29. public class FindShortestSubArray {
  30.  
  31. public int solution(int[] nums) {
  32. //1.首先统计出现次数最多的数据
  33. Integer maxTimesNum = 0, times = 0;
  34. //这里第二个参数用来放一个数组,三个数据,第一个次数,第二个第一次出现位置,第三个最后一次出现位置
  35. Map<Integer, int[]> countMap = new HashMap();
  36. for(int i = 0; i < nums.length; ++i) {
  37. if(!countMap.containsKey(nums[i])) {
  38. //如果不包含
  39. countMap.put(nums[i], new int[]{1, i, i});
  40. } else {
  41. //如果已经包含了
  42. int[] temp = countMap.get(nums[i]);
  43. temp[0]++;
  44. temp[2] = i;
  45. }
  46. }
  47.  
  48. //2.然后统计这个数据的子串,所有这个字符出现的次数达到最大次数就结束遍历
  49. int result = 0, maxTimes = 0;
  50. for(int[] values : countMap.values()) {
  51. if(values[0] > maxTimes) {
  52. //新的出现次数最大值
  53. maxTimes = values[0];
  54. //计算子串长度
  55. result = values[2] - values[1] + 1;
  56. } else if (values[0] == maxTimes) {
  57. //出现次数一样,取最小子串
  58. result = Math.min(result, values[2] - values[1] + 1);
  59. }
  60. }
  61.  
  62. return result;
  63. }
  64.  
  65. public static void main(String args[]) {
  66. int[] A = {1,2,2,3,1};
  67. int[] B = {2,1,1,2,1,3,3,3,1,3,1,3,2};
  68.  
  69. System.out.println(new FindShortestSubArray().solution(B));
  70. }
  71. }

【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697的更多相关文章

  1. 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747

    真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...

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

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

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

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

  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:数组中的第K个最大元素【215】

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  9. 【剑指offer】50.数组中重复出现的数字

    50.数组中重复出现的数字 知识点:数组:Set的不可重复性 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重 ...

  10. Java数据结构和算法之数组与简单排序

    一.数组于简单排序 数组 数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信 ...

随机推荐

  1. JS实现Base64编码、解码,即window.atob,window.btoa功能

    window.atob(),window.btoa()方法可以对字符串精选base64编码和解码,但是有些环境比如nuxt的服务端环境没法使用window,所以需要自己实现一个base64的编码解码功 ...

  2. react的3种组件

    推荐阅读:https://www.jianshu.com/p/2726b8654989 1. createClass 已不推荐使用,这里不再多讲.但你仍需要了解它,因为你可能会接触到一些旧项目,或者一 ...

  3. 一次修复linux的efi引导的集中方法总结记录

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/grub_uefi_repair 起因:EFI分区被删除导致引导问 ...

  4. Git基本介绍(三大分区及核心内部构造)

    1. Git三大工作区(工作区.暂存区和版本库) 工作区(WORKING DIRECTORY): 直接编辑文件的地方,肉眼可见直接操作: 暂存区(STAGIN AREA):数据(快照)暂时存放的地方: ...

  5. Confd+Consul 动态生成配置文件

    一.Consul安装和配置 1.consul是什么? consul是HashiCorp公司推出的一款工具,主要用于实现分布式系统的服务发现与配置,它提供了以下几个关键特性: 服务发现:Consul客户 ...

  6. 刷题记录:[DDCTF 2019]homebrew event loop

    目录 刷题记录:[DDCTF 2019]homebrew event loop 知识点 1.逻辑漏洞 2.flask session解密 总结 刷题记录:[DDCTF 2019]homebrew ev ...

  7. 剑指offer:构建乘积数组

    题目描述: 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]. ...

  8. Java12新特性 -- 只保留一个 AArch64 实现

    现状 当前 Java 11 及之前版本JDK中存在两个64位ARM端口.这些文件的主要来源位于src/hotspot/cpu/arm 和 open/src/hotspot/cpu/aarch64 目录 ...

  9. SDN实验---Ryu的应用开发(一)Hub实现

    补充: (一)Ubuntu下安装Notepadqq 背景:为什么安装Notepadqq Notepad++ 不仅有语法高亮度显示,也有语法折叠功能,并且支持宏以及扩充基本功能的外挂模组.但是可惜的是N ...

  10. matlab学习笔记10_4MATLAB中的字符串表示

    一起来学matlab-字符串操作 10_4 MATLAB中的字符串表示 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 感 ...