真的感觉有点难。。。

这还是简单级别。。。

我也是醉了

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的更多相关文章

  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. SVN 常用 下载仓库

    仓库的基本使用: 1.管理仓库的人会给你一个SVN的仓库地址,如: https://192.168.2.98:8443/svn/建筑工程健康监测系统 2.然后就下载仓库里面的所有文件 3.对仓库做增删 ...

  2. 点击复制文字到剪贴板兼容性安卓ios

    一般那种活动H5分享可能会用到点击复制文字到剪贴板,很简单的功能 于是搜了一搜:js复制文字到剪贴板,可用结果大致分为两类: 一类是js原生方法,这种方法兼容性不好,不兼容ios: https://d ...

  3. hdoj - 1342 Lotto

    Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,... ...

  4. YoTube 视频如何下载

    因我学习自动化测试 ,国内的C# selenium 搭建的环境的资料甚少,然后去国外网站找资料, 曹鼠给我的gogle安装一个下载YoTube视频插件,特此非常感谢他. 前提条件需要一个服务器:Sha ...

  5. ipv4的TCP的几个状态 (SYN, FIN, ACK, PSH, RST, URG)

    1 在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 2 3 其中,对于我们日常的分析有用的就是前面的五个字段. 4 5 它们的含义是 ...

  6. C# redis客户端帮助类

    需要在NuGet里面引用ServiceStack.Redis using ServiceStack.Redis; using ServiceStack.Redis.Generic; using Sys ...

  7. HTTP协议复习

    HTTP请求/响应的步骤: 客户端连接到WEB服务器:浏览器与web服务器的HTTP端口建立一个TCP套接字连接,例如:http://www.baidu.com 发送HTTP请求:通过TCP套接字,客 ...

  8. docker安装并运行rabbitmq

    拉取镜像: [mall@VM_0_7_centos ~]$ [sudo] password for mall: : Pulling from library/rabbitmq 5b7339215d1d ...

  9. Spring cloud微服务安全实战-8-1课程总结

    总结 首先讲了api的安全.安全常见的风险.安全措施.然后我们把简单的api演化成一个这种微服务的架构. 首先讲了在网关上可以做哪些安全的措施.然后讲了如何搭建一个安全中心,也就是认证服务器,包括一些 ...

  10. App installation failed (A valid provisioning profile for this executable was not found)

    真机调试build success ,App installation failed (A valid provisioning profile for this executable was not ...