【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList;
import java.util.List; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: PancakeSort
* @Author: xiaof
* @Description: TODO 969. Pancake Sorting
* Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length,
* then reverse the order of the first k elements of A.
* We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A.
*
* Return the k-values corresponding to a sequence of pancake flips that sort A.
* Any valid answer that sorts the array within 10 * A.length flips will be judged as correct.
*
* Input: [3,2,4,1]
* Output: [4,2,4,3]
* Explanation:
* We perform 4 pancake flips, with k values 4, 2, 4, and 3.
* Starting state: A = [3, 2, 4, 1]
* After 1st flip (k=4): A = [1, 4, 2, 3]
* After 2nd flip (k=2): A = [4, 1, 2, 3]
* After 3rd flip (k=4): A = [3, 2, 1, 4]
* After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted.
*
* 参考:https://blog.csdn.net/fuxuemingzhu/article/details/85937314
*
* @Date: 2019/7/16 8:57
* @Version: 1.0
*/
public class PancakeSort { public List<Integer> solution(int[] A) {
//思路是这样的,就是每次吧最大的做一个翻转,移动到最前面,然后再翻转到最后面,这样每次都可以从数据中排除掉最大的那个
//但是由于这个题的数字都是按照1~n的顺序给的值,那么就不需要每次都取最大值,只要取index索引就可以了
List<Integer> res = new ArrayList<>();
for(int i = A.length, x; i > 0; --i) {
//寻找最大的位置
for(x = 0; A[x] != i; ++x);
//当x所在的索引跟当前应该的最大值相等的时候,也就是x指向了最大值的位置的-1位置,我们翻转两次
//第一次吧值翻转到最前面,第二次翻转到最后面
reverse(A, x);
res.add(x + 1);
//然后翻转到对应的位置
reverse(A, i - 1);
res.add(i);
} return res;
} private void reverse(int[] A, int k) {
//翻转k位
for(int i = 0, j = k; i < j; ++i,--j) {
//前后交换
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
} public static void main(String[] args) {
int data[] = {3,2,4,1};
PancakeSort fuc = new PancakeSort();
System.out.println(fuc.solution(data));
System.out.println();
} }
package y2019.Algorithm.array.medium; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: FindDuplicates
* @Author: xiaof
* @Description: TODO 442. Find All Duplicates in an Array
* Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
* Find all the elements that appear twice in this array.
* Could you do it without extra space and in O(n) runtime?
*
* Input:
* [4,3,2,7,8,2,3,1]
* Output:
* [2,3]
*
* 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
* 找到所有出现两次的元素。
* 你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?
* @Date: 2019/7/16 9:00
* @Version: 1.0
*/
public class FindDuplicates { public List<Integer> solution(int[] nums) { //直接用set
List<Integer> res = new ArrayList<>();
if(nums == null || nums.length <= 0) {
return res;
}
//受限还是排序
Arrays.sort(nums);
//遍历一次
int preValue = nums[0];
for(int i = 1; i < nums.length; ++i) {
int curValue = nums[i];
if(preValue == curValue) {
res.add(curValue);
} else {
preValue = curValue;
}
} return res;
}
}
package y2019.Algorithm.array.medium; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: MaxAreaOfIsland
* @Author: xiaof
* @Description: TODO 695. Max Area of Island
* Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land)
* connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
*Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
*
* [[0,0,1,0,0,0,0,1,0,0,0,0,0],
* [0,0,0,0,0,0,0,1,1,1,0,0,0],
* [0,1,1,0,1,0,0,0,0,0,0,0,0],
* [0,1,0,0,1,1,0,0,1,0,1,0,0],
* [0,1,0,0,1,1,0,0,1,1,1,0,0],
* [0,0,0,0,0,0,0,0,0,0,1,0,0],
* [0,0,0,0,0,0,0,1,1,1,0,0,0],
* [0,0,0,0,0,0,0,1,1,0,0,0,0]]
* Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
*
* 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
* 找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/max-area-of-island
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* @Date: 2019/7/16 9:00
* @Version: 1.0
*/
public class MaxAreaOfIsland { public int solution(int[][] grid) {
//寻找聚集度最高的和,遍历所有的岛屿,然后对附近的所有1求和,每次求和探索四个位置的和,上下左右
int maxIsland = 0;
for(int i = 0; i < grid.length; ++i) {
for(int j = 0; j < grid[i].length; ++j) {
//求出最大的岛屿
maxIsland = Math.max(maxIsland, areaOfIsLand(grid, i, j));
}
}
return maxIsland;
} private int areaOfIsLand(int[][] grid, int x, int y) {
if(x >= 0 && x < grid.length && y >=0 && y < grid[x].length && grid[x][y] == 1) {
//设置标记
grid[x][y] &= 0;
return 1 + areaOfIsLand(grid, x - 1, y) + areaOfIsLand(grid, x + 1, y) + areaOfIsLand(grid, x, y - 1) + areaOfIsLand(grid, x, y + 1);
} return 0;
} }
【LEETCODE】57、数组分类,适中级别,题目:969、442、695的更多相关文章
- 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:数组中的第K个最大元素【215】
		
LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...
 - LeetCode: 57. Insert Interval(Hard)
		
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
 - LeetCode一维数组的动态和
		
一维数组的动态和 题目描述 给你一个数组 nums.数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]...nums[i]). 请返回 nums 的动态和. 示例 1: ...
 - 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73
		
这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...
 - 【LEETCODE】60、数组分类,适中级别,题目:75、560、105
		
package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...
 - 【LEETCODE】58、数组分类,适中级别,题目:238、78、287
		
package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...
 - 【LEETCODE】55、数组分类,适中级别,题目:79、611、950
		
第950题,这题我是真的没想到居然会说使用队列去做,大神的答案,拿过来瞻仰一下 package y2019.Algorithm.array; import java.util.HashMap; imp ...
 
随机推荐
- Python 下载超大文件
			
使用python下载超大文件, 直接全部下载, 文件过大, 可能会造成内存不足, 这时候要使用requests 的 stream模式, 主要代码如下 iter_content:一块一块的遍历要下载的内 ...
 - 【洛谷】P1449 后缀表达式
			
P1449 后缀表达式 分析: 简单的模拟题. 熟练容器stack的话很容易解决. stack,栈,有先进后出的特性. 比如你有一个箱子,你每放进第一个数时,就往箱底放,放第二个数时就在第一个数的上面 ...
 - Js 之正则验证手机号、QQ、身份证等
			
// 常见的 正则表达式 校验 // QQ号.手机号.Email.是否是数字.去掉前后空格.是否存在中文.邮编.身份证.URL.日期格式.IP let myRegExp = { // 检查字符串是否为 ...
 - 如果要对img里面的值做特殊处理,可以直接写方法
			
html <img :src="getMore('up')" alt=""> data里面定义的 one: 'http://p1.fishqc.ne ...
 - 关于js中onload事件的部分报错。
			
当使用onload获取元素时,建议在onload事件之前定义需要获取的元素名称,在onload里面只执行获取操作,这样获取到的元素在后面才能顺利使用. <!DOCTYPE html> &l ...
 - Linux 文件系统磁盘空间与连接文件
			
磁盘与目录的容量 我们知道磁盘的整体数据hi在superblock块中,但是各文件的容量则在inode中记载. df:列出文件系统的整体磁盘使用量 由于df主要读取的数据几乎都是针对整个文件系统,因此 ...
 - elementui 走马灯图片自适应
			
点击单元格后弹出对话框轮播图片,用Carousel 走马灯实现. 希望图片无论分辨率多少,都能在一屏内显示,这时就要用图片自适应. 图片外层容器,使用 flex 布局,设置对齐方式为主轴.交叉轴居中 ...
 - axios post 400 状态码
			
1.400状态码 400的主要有两种形式: (1).bad request意思是“错误的请求": (2).invalid hostname意思是"不存在的域名”. 2.axio ...
 - canvas api 速记
			
基本骨骼 <canvas id="canvas" width=1000 height=1000 style="border: 1px black dotted&qu ...
 - 自定义Spring Boot内置tomcat的404页面
			
spring boot 的相关404页面配置都是针对项目路径下的(如果配置了 context-path) 在context-path不为空的情况下,如果访问路径不带context-path,这时候会显 ...