package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: IsOneBitCharacter
* @Author: xiaof
* @Description: TODO 717. 1-bit and 2-bit Characters
* We have two special characters. The first character can be represented by one bit 0.
* The second character can be represented by two bits (10 or 11).
* Now given a string represented by several bits.
* Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
*
* Input:
* bits = [1, 0, 0]
* Output: True
* Explanation:
* The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
*
* 有两种字符,一种是0,一种是10或者11,现在要判断整个数组是否由这两种组成的,要求最后一位的数字必须是单个的0.
* @Date: 2019/7/10 8:54
* @Version: 1.0
*/
public class IsOneBitCharacter { public boolean solution(int[] bits) {
//必须最后一个是单个0,中间是10或者11,那么一定是奇数,然后最后一个必须是0
if(bits[bits.length - 1] != 0) {
return false;
}
//遍历获取结果,每次遍历两个
for(int i = 0; i < bits.length - 1;) {
if(bits[i] == 1 && (bits[i + 1] == 0 || bits[i + 1] == 1) && i < bits.length - 2) {
i += 2;
} else if (bits[i] == 0) {
i += 1;
} else {
return false;
}
} return true;
} public static void main(String args[]) {
// int[] A = {1,0,0};
// int[] A = {0,0};
int[] A = {1,1,1,0};
System.out.println(new IsOneBitCharacter().solution(A));
} }
package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: ImageSmoother
* @Author: xiaof
* @Description: TODO 661. Image Smoother
* Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray
* scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself.
* If a cell has less than 8 surrounding cells, then use as many as you can.
*
* Input:
* [[1,1,1],
* [1,0,1],
* [1,1,1]]
* Output:
* [[0, 0, 0],
* [0, 0, 0],
* [0, 0, 0]]
* Explanation:
* For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
* For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
* For the point (1,1): floor(8/9) = floor(0.88888889) = 0
*
* 包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,
* 平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/image-smoother
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* @Date: 2019/7/10 9:27
* @Version: 1.0
*/
public class ImageSmoother { public int[][] solution(int[][] M) {
int[][] result = new int[M.length][M[0].length];
//直接暴力求解
for(int i = 0; i < M.length; ++i) {
for(int j = 0; j < M[i].length; ++j) {
//8个值得位置
int minUp = i > 0 ? i - 1 : 0;
int maxDown = i < M.length - 1 ? i + 1 : M.length - 1;
int minLeft = j > 0 ? j - 1 : 0;
int maxRight = j < M[i].length - 1 ? j + 1 : M[i].length - 1;
int countT = 0, countN = 0;
//获取所有的数据的平均值
for(int r = minUp; r <= maxDown; ++r) {
for(int c = minLeft; c <= maxRight; ++c) {
countN++;
countT += M[r][c];
}
} result[i][j] = countT / countN;
}
} return result;
} }
package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: MinCostClimbingStairs
* @Author: xiaof
* @Description: TODO 746. Min Cost Climbing Stairs
* On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
* Once you pay the cost, you can either climb one or two steps.
* You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0,
* or the step with index 1.
*
* Input: cost = [10, 15, 20]
* Output: 15
* Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
*
* 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。
* 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。
* 您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。
*
* Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
* Output: 6
* Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
* 1(1) + 1(2)
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/min-cost-climbing-stairs
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* @Date: 2019/7/10 9:42
* @Version: 1.0
*/
public class MinCostClimbingStairs { public int solution(int[] cost) {
//因为每次可以爬一个楼梯,或者2个楼梯,那么dp一维数组
//爬到n层需要前面MIN{(n-1) + 当前层, 或者n-2 到达当前层,可以最后一层直接跳过
int[] dp = new int[cost.length + 1];
int index = 0;
//可以第一步走一层,或者2层
dp[0] = cost[0]; dp[1] = cost[1];
for(int i = 2; i < cost.length + 1; ++i) {
dp[i] = Math.min(dp[i - 1], dp[i - 2]);
if(i < cost.length)
dp[i] += cost[i];
} return Math.min(dp[cost.length - 1], dp[cost.length]);
} public static void main(String args[]) {
// int[] A = {1,0,0};
// int[] A = {0,0};
int[] A = {0,0,0,1};
System.out.println(new MinCostClimbingStairs().solution(A));
} }
package y2019.Algorithm.array;

/**
* @ClassName MaximumProduct
* @Description TODO 628. Maximum Product of Three Numbers
*
* Given an integer array, find three numbers whose product is maximum and output the maximum product.
*
* Input: [1,2,3]
* Output: 6
*
* 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。
*
* @Author xiaof
* @Date 2019/7/10 22:29
* @Version 1.0
**/
public class MaximumProduct { public int solution(int[] nums) {
//最大的三个数,除了最大的三个数相乘之外还要考虑一下负数,那么就是2个负数乘以一个正数,那么必须是最小的两个数乘最大的那个数
Integer[] maxThreeNum = new Integer[3];
Integer[] minTwoNum = new Integer[2];
for(int i = 0; i < maxThreeNum.length; ++i) {
maxThreeNum[i] = null;
}
//先值为空,然后遍历
for(int i = 0; i < nums.length; ++i) {
//依次和max数组上的数据比较,然后依次吧数组的数据进行调整
int index = -1; //填入的位置
for(int j = 0; j < 3; ++j) {
if(maxThreeNum[j] == null || nums[i] > maxThreeNum[j]) {
++index;
} else {
break;
}
} if(index > -1) {
//修改位置
for(int k = 0; k < index; ++k) {
//前面几位从新排序
maxThreeNum[k] = maxThreeNum[k+1];
}
maxThreeNum[index] = nums[i];
} //计算最小的两个数
int minIndex = 2;
for(int j = 1; j >= 0; --j) {
if(minTwoNum[j] == null || nums[i] < minTwoNum[j]) {
--minIndex;
} else {
break;
}
} if(minIndex < 2) {
//移动位置
for(int k = 1; k > minIndex; --k) {
minTwoNum[k] = minTwoNum[k - 1];
}
minTwoNum[minIndex] = nums[i];
}
} //最大三个数的乘积
return Math.max(maxThreeNum[0] * maxThreeNum[1] * maxThreeNum[2], minTwoNum[0] * minTwoNum[1] * maxThreeNum[2]); } public static void main(String args[]) {
// int[] A = {1,0,0};
// int[] A = {0,0};
int[] A = {-4,-3,-2,-1,60};
int[] B = {-1,-2,-3};
System.out.println(new MaximumProduct().solution(B));
} }
package y2019.Algorithm.array;

/**
* @ClassName FindMaxAverage
* @Description TODO 643. Maximum Average Subarray I
*
* Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.
* Example 1:
* Input: [1,12,-5,-6,50,3], k = 4
* Output: 12.75
* Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
*
* Note:
* 1 <= k <= n <= 30,000.
* Elements of the given array will be in the range [-10,000, 10,000].
*
* 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。
*
* @Author xiaof
* @Date 2019/7/10 23:12
* @Version 1.0
**/
public class FindMaxAverage {
public double solution(int[] nums, int k) {
//每次统计k个数,然后从第k个开始,没多遍历一个数就减去前面一个数
int sum = 0;
int result = 0;
for(int i = 0; i < k; ++i) {
sum += nums[i];
result = sum;
} for(int i = k; i < nums.length; ++i) {
int cur = sum + nums[i] - nums[i - k];
result = Math.max(cur, result);
sum = cur;
} return result / (k * 1.0);
} public static void main(String args[]) {
int[] A = {0,4,0,3,2};
int[] B = {5};
int k = 1;
System.out.println(new FindMaxAverage().solution(B, k));
} }
package y2019.Algorithm.array;

import java.util.HashMap;
import java.util.Map; /**
* @ClassName MaxDistToClosest
* @Description TODO 849. Maximize Distance to Closest Person
*
* In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.
* There is at least one empty seat, and at least one person sitting.
* Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
* Return that maximum distance to closest person.
* Example 1:
* Input: [1,0,0,0,1,0,1]
* Output: 2
* Explanation:
* If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
* If Alex sits in any other open seat, the closest person has distance 1.
* Thus, the maximum distance to the closest person is 2.
*
* 在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的。
* 至少有一个空座位,且至少有一人坐在座位上。
* 亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。
* 返回他到离他最近的人的最大距离。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/maximize-distance-to-closest-person
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* @Author xiaof
* @Date 2019/7/10 23:27
* @Version 1.0
**/
public class MaxDistToClosest {
public int solution(int[] seats) {
//说白了就是求间距最大的中间位置,也就是连续0最长的子串
int result = 0, n = seats.length, lastNull = -1;
for(int i = 0; i < seats.length; ++i) {
if(seats[i] == 1) {
//遇到人,计算上一个位置到当前位置的空格最大值
//如果小于0,那就是第一次,也就是可以做开头位置
result = lastNull < 0 ? i : Math.max((i - lastNull) / 2, result);
lastNull = i; //遇到人,记录目前最后一次遇到的人的时候
}
} //判断最后一个位置
result = Math.max(result, n - lastNull - 1); return result;
} public static void main(String args[]) {
int[] A = {1,0,0,0,1,0,1};
int[] B = {5};
int k = 1;
System.out.println(new MaxDistToClosest().solution(A));
}
}

【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849的更多相关文章

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

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

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

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

  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. Java数据结构和算法之数组与简单排序

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

  10. 1145: 零起点学算法52——数组中删数II

    1145: 零起点学算法52--数组中删数II Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 293 ...

随机推荐

  1. pgloader 学习(九) pg 2 pg 使用with 参数控制同步逻辑

    pgloader 支持比较丰富的配置参数,同时默认数据在同步的时候是会进行索.schema 以及数据的同步对于实际我们可能存在需要进行控制,我们可以通过with 参数方便的处理 参考配置 load 文 ...

  2. 洛谷P3177 树上染色

    题目 一道非常好的树形DP. 状态:\(dp[u][n]\)为u的子树选n个黑点所能得到的收益最大值. 则最终的结果就是\(dp[root][k],\)\(root\)可以为任何值,为了方便,使\(r ...

  3. SpringBoot聚合项目打包

    1.打包遇到的问题: 子模块打包时,每次打包出来的jar都只有3k左右,没办法运行,目测应该是依赖的jar没有打包进来! 2.解决办法: 2.1.主项目的pom.xml里面不需要打包配置 2.2.在需 ...

  4. Mysql 之根据经纬度按距离排序

    一.方式一 st_distance 计算的结果单位是度,需要乘111195(地球半径6371000*PI/180)是将值转化为米. SELECT *, (st_distance(point(lng,l ...

  5. 怎么在app上添加图标和文字

    window.showAddMenu = this.addSearch let data = { type: 'ICON', text: 'https://static-image.lexing360 ...

  6. MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法

    MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法 事情的起因     今天课堂上要展示小组项目,需要用一个软件叫W ...

  7. Java 面向对象(九)

    常用类之Random Random类位于 java.util 包中,主要用于生成伪随机数 Random类将种子数作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关 创建Random ...

  8. 微信小程序全局设置分享内容

    微信小程序每个页面都可以在onShareAppMessage中设置分享内容,如果想要全局设置成一样的分享内容如何设置呢? 在app.js中新增以下方法: //重写分享方法 overShare: fun ...

  9. 【NumPy】 之常见运算(np.around、np.floor、np.ceil、np.where)

    aroundnp.around 返回四舍五入后的值,可指定精度. around(a, decimals=0, out=None) a 输入数组 decimals 要舍入的小数位数. 默认值为0. 如果 ...

  10. 解决VS Code开发Python3语言自动补全功能

    1.打开设置界面 2)使用快捷键组合[Ctrl+Shift+p] . 输入setting,选中首选项的user setting模式设置界面 在打开的User Settings文件界面,搜索:pytho ...