【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697
package y2019.Algorithm.array; import java.util.HashSet;
import java.util.Set; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: FairCandySwap
* @Author: xiaof
* @Description: TODO 888. Fair Candy Swap
*
* Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has,
* and B[j] is the size of the j-th bar of candy that Bob has.
* Since they are friends, they would like to exchange one candy bar each so that after the exchange,
* 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.)
* Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange,
* and ans[1] is the size of the candy bar that Bob must exchange.
* If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.
*
* Input: A = [1,1], B = [2,2]
* Output: [1,2]
*
* 交换A,B两个数组中的一个数字,使得两个数组的和相等。要返回的结果是个要交换的两个数字,分别来自A,B。
*
* @Date: 2019/7/8 9:25
* @Version: 1.0
*/
public class FairCandySwap { public int[] solution(int[] A, int[] B) {
//说白了对数组求和,然后求两个数组的平均数的差值,然后看看是否存在这个差值
Set seta = new HashSet();
int[] result = new int[2];
int suma = 0, sumb = 0; for(int i = 0; i < A.length; ++i) {
seta.add(A[i]);
suma += A[i];
} for(int i = 0; i < B.length; ++i) {
sumb += B[i];
}
//求两边数据跟平均数的差值,两边的和的平均数就是两边需要到达的数据
//现在求B距离这个平均数的差距
int dif = (suma + sumb) / 2 - sumb;
//然后我们再第二个数组中找,看是否存在正好对应的补上
for(int i = 0; i < B.length; ++i) {
//获取对应i的数据值,判断B加上这个差距值,判断A中是否存在
//因为吧b[i]移动过去之后,还要减去B[i]的值
if(seta.contains(dif + B[i])) {
//看看A中是否包含这个差值,如果包含,那么就可以返回了
result[0] = dif + B[i];
result[1] = B[i];
// break;
}
} return result; } public static void main(String args[]) {
int[] A = {1,2};
int[] B = {2,3}; System.out.println(new FairCandySwap().solution(A, B));
} }
package y2019.Algorithm.array; import java.util.stream.IntStream; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: CanThreePartsEqualSum
* @Author: xiaof
* @Description: TODO 1013. Partition Array Into Three Parts With Equal Sum
* 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.
* Formally, we can partition the array
* 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])
*
* Input: [0,2,1,-6,6,-7,9,1,2,0,1]
* Output: true
* Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
*
* 题目的意思应该是平均吧数据分成三等分,每份的和相同,并且每份数据要求是连续的
*
* @Date: 2019/7/9 9:18
* @Version: 1.0
*/
public class CanThreePartsEqualSum { public boolean solution(int[] A) {
int sum = IntStream.of(A).sum(); //求和
//分成三份
int threeFen = sum / 3;
//我们连续操作,获取,直到获取到三份
boolean result = false; int index = 0;
int tempSum = 0;
int times = 0;
while(index < A.length) {
tempSum += A[index];
if(tempSum == threeFen) {
times++;
tempSum = 0;
} ++index;
} if(times == 3) {
result = true;
} return result;
} }
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: IsMonotonic
* @Author: xiaof
* @Description: TODO 896. Monotonic Array
* An array is monotonic if it is either monotone increasing or monotone decreasing.
* An array A is monotone increasing if for all i <= j, A[i] <= A[j].
* An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
* Return true if and only if the given array A is monotonic.
*
* Input: [1,2,2,3]
* Output: true
*
* 根据题意,这个数组应该是单向数组,递增,或者递减
*
* @Date: 2019/7/9 9:31
* @Version: 1.0
*/
public class IsMonotonic { public boolean solution(int[] A) {
int direct = 0; //数组变化方向
int curNum = A[0]; for(int i = 1; i < A.length; ++i) { if(A[i] > curNum && (direct == 0 || direct == 1)) {
direct = 1;
curNum = A[i];
} else if(A[i] < curNum && (direct == 0 || direct == 2)) {
direct = 2;
curNum = A[i];
} else if (A[i] == curNum) {
curNum = A[i];
} else {
return false;
}
} return true; } public static void main(String args[]) {
int[] A = {1,1,0};
int[] B = {2,3}; System.out.println(new IsMonotonic().solution(A));
} }
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: FindMaxConsecutiveOnes
* @Author: xiaof
* @Description: TODO 485. Max Consecutive Ones
* Given a binary array, find the maximum number of consecutive 1s in this array.
*
* Input: [1,1,0,1,1,1]
* Output: 3
* Explanation: The first two digits or the last three digits are consecutive 1s.
* The maximum number of consecutive 1s is 3.
*
* 统计最大1连续出现次数
*
* @Date: 2019/7/9 10:17
* @Version: 1.0
*/
public class FindMaxConsecutiveOnes { public int solution(int[] nums) { int maxTimes = 0;
int cnt = 0;
for(int i = 0; i < nums.length; ++i) {
if(nums[i] == 1) {
cnt++;
} else {
if(cnt > maxTimes) {
maxTimes = cnt;
}
cnt = 0;
}
} //如果一直延续到循环末尾还是还是没有else,那么这里做最后拦截
if(cnt > maxTimes) {
maxTimes = cnt;
} return maxTimes;
} }
package y2019.Algorithm.array; import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: FindDisappearedNumbers
* @Author: xiaof
* @Description: TODO 448. Find All Numbers Disappeared in an Array
* Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
* Find all the elements of [1, n] inclusive that do not appear in this array.
* Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
*
* Input:
* [4,3,2,7,8,2,3,1]
*
* Output:
* [5,6]
*
* @Date: 2019/7/9 10:25
* @Version: 1.0
*/
public class FindDisappearedNumbers { public List<Integer> solution(int[] nums) {
List ret = new ArrayList();
for(int i = 0; i < nums.length; ++i) {
int val = Math.abs(nums[i]) - 1; //用来做下标位置
if(nums[val] > 0) {
nums[val] = -nums[val];//标记val大小的数据
}
} //遍历获取没有标记的数据
for(int i = 0; i < nums.length; ++i) {
if(nums[i] > 0) {
ret.add(i + 1); //这个位置是没有标记到的
}
} return ret;
} public static void main(String args[]) {
int[] A = {4,3,2,7,8,2,3,1};
int[] B = {2,3}; System.out.println(new FindDisappearedNumbers().solution(A));
} }
package y2019.Algorithm.array; import java.util.HashMap;
import java.util.Map; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: FindShortestSubArray
* @Author: xiaof
* @Description: TODO 697. Degree of an Array
* Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any
* one of its elements.
* Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
*
* Input: [1, 2, 2, 3, 1]
* Output: 2
* Explanation:
* The input array has a degree of 2 because both elements 1 and 2 appear twice.
* Of the subarrays that have the same degree:
* [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
* The shortest length is 2. So return 2.
*
* 最短的子串使得其包含出现次数最多的元素,子串中刚好包含出现次数最多的数字
*
* @Date: 2019/7/9 14:06
* @Version: 1.0
*/
public class FindShortestSubArray { public int solution(int[] nums) {
//1.首先统计出现次数最多的数据
Integer maxTimesNum = 0, times = 0;
//这里第二个参数用来放一个数组,三个数据,第一个次数,第二个第一次出现位置,第三个最后一次出现位置
Map<Integer, int[]> countMap = new HashMap();
for(int i = 0; i < nums.length; ++i) {
if(!countMap.containsKey(nums[i])) {
//如果不包含
countMap.put(nums[i], new int[]{1, i, i});
} else {
//如果已经包含了
int[] temp = countMap.get(nums[i]);
temp[0]++;
temp[2] = i;
}
} //2.然后统计这个数据的子串,所有这个字符出现的次数达到最大次数就结束遍历
int result = 0, maxTimes = 0;
for(int[] values : countMap.values()) {
if(values[0] > maxTimes) {
//新的出现次数最大值
maxTimes = values[0];
//计算子串长度
result = values[2] - values[1] + 1;
} else if (values[0] == maxTimes) {
//出现次数一样,取最小子串
result = Math.min(result, values[2] - values[1] + 1);
}
} return result;
} public static void main(String args[]) {
int[] A = {1,2,2,3,1};
int[] B = {2,1,1,2,1,3,3,3,1,3,1,3,2}; System.out.println(new FindShortestSubArray().solution(B));
}
}
【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697的更多相关文章
- 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747
真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...
- 【LEETCODE】54、数组分类,简单级别,题目:605、532
数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...
- 【LeetCode】数组-1(643)-返回规定长度k的最大子数组的平均数
好久没有刷LeetCode了,准备重拾并坚持下去,每天刷个两小时.今天算是开始的第一天,不过出师不利,在一道很简单的题目上墨迹半天.不过还好,现在踩过的坑,应该都不会白踩,这些可能都是以后程序员路上稳 ...
- 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:数组中的第K个最大元素【215】
LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...
- 【剑指offer】50.数组中重复出现的数字
50.数组中重复出现的数字 知识点:数组:Set的不可重复性 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重 ...
- Java数据结构和算法之数组与简单排序
一.数组于简单排序 数组 数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信 ...
随机推荐
- facl
file access control lists 文件的额外赋权机制,针对性的对某用户对文件的权限进行处理 setfacl 指定空权限
- 设计模式之MVC和MVT
MVC各部分的功能 全拼为Model-View-Controller(如上图所示) M(模型)全拼为Model,主要封装对数据库层的访问(内嵌ORM框架),对数据库中的数据进行增.删.改.查操作. V ...
- 安利一个github上面的一个神级库thefuck,Linux命令敲错了,没关系,自动纠正你的命令
没错就是这么神奇,名字相当噶性,thefuck.当你命令输入错误不要怕,直接来一句fuck,自动纠正你输入的命令. 在你输入错误的命令的时候,忍俊不禁的想来一句fuck,没错你不仅可以嘴上说,命令里面 ...
- 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1。 buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t”分割
实验内容(mapReduce安装请按照林子雨教程http://dblab.xmu.edu.cn/blog/631-2/) 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为 ...
- 将对象转化为数组,并且适用select下拉
当你做element-ui的select下拉的时候数据是从后台请求,但是怎么才能将obj转成数组呢.并且后台返回的key和value中的key是要传的参数 var obj = { name: 'gab ...
- 第10组 Alpha冲刺(5/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 学习调用中国天气网API,接近实现天气推送功能 对天气推送的形式进行讨论及重确 ...
- 剑指offer:左旋转字符串
题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”ab ...
- Spring Cloud Eureka配置文件详解
本篇内容用来说明Eureka 常用配置的含义. 以下配置都是以 eureka.server 开头: 参数 描述 备注 eureka.server.eviction-interval-timer-in- ...
- Python中的args和kwargs
有时,你会看到python中定义函数的时候带有两个奇怪的参数:*args.**kwargs.如果你曾经想知道它们是干什么的,或者想知道你的IDE为什么在main()函数中定义它们,那么本文可以帮助到你 ...
- windows 安装 mysql 启动
mysqld --defaults-file="d:/lovejava/mysql-5.6/my-default.ini"