摘要: ###对撞双指针(有贪心那味了) ####代码 class Solution { //对撞双指针 public int numRescueBoats(int[] people, int limit) { Arrays.sort(people); int n = people.length; int 阅读全文
posted @ 2020-12-19 09:39 浅滩浅 阅读(199) 评论(0) 推荐(0) 编辑
摘要: ##暴力 class Solution { //前缀和 public int minSubArrayLen(int s, int[] nums) { int n = nums.length; int res = n; //[i,j) int[] preSum = new int[n+1]; for( 阅读全文
posted @ 2020-11-10 21:20 浅滩浅 阅读(186) 评论(0) 推荐(0) 编辑
摘要: 实际上就是c++中函数next_permutation的实现(包含重复元素) ##c++源码实现 网址:https://en.cppreference.com/w/cpp/algorithm/next_permutation template<class BidirIt> bool next_per 阅读全文
posted @ 2020-11-10 15:40 浅滩浅 阅读(167) 评论(0) 推荐(0) 编辑
摘要: ##最粗暴的方法,自定义数据结构+重新定义排序规则 class Solution { public int[][] kClosest(int[][] points, int K) { int m = points.length; Node[] nodes = new Node[m]; for(int 阅读全文
posted @ 2020-11-09 16:04 浅滩浅 阅读(174) 评论(0) 推荐(0) 编辑
摘要: 经典dfs将当前位置传入,标记和当前点一类的点,同时进行计数 class Solution { public int[] pondSizes(int[][] land) { List<Integer> res = new LinkedList<>(); int m = land.length,n = 阅读全文
posted @ 2020-11-08 20:10 浅滩浅 阅读(193) 评论(0) 推荐(0) 编辑
摘要: ##错误代码 ##Arrays.sort(arr,(l,r)->l-r)后面自定义必须是类 ##正确代码 class Solution { public int[] sortByBits(int[] arr) { int n = arr.length; int[] res = new int[n]; 阅读全文
posted @ 2020-11-06 21:45 浅滩浅 阅读(197) 评论(0) 推荐(0) 编辑
摘要: 思路:递增序列长度问题简化版 ##我的题解 class Solution { public boolean validMountainArray(int[] a) { int l = 0, r = 0; int aSize = a.length; //从左往右找以第一位元素开头的递增序列长度 for 阅读全文
posted @ 2020-11-03 19:33 浅滩浅 阅读(157) 评论(0) 推荐(0) 编辑
摘要: 思路:是1边长+4,判断临近有无1的块,如果有就进行减 需要注意两点:判断方向只需要判断两个方向(横轴和纵轴)就可以了,否则由于相对性会导致一种情况被重复判断。 ##代码 class Solution { public int islandPerimeter(int[][] grid) { //左上 阅读全文
posted @ 2020-10-31 18:39 浅滩浅 阅读(145) 评论(0) 推荐(0) 编辑
摘要: ##前序遍历+判断 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x 阅读全文
posted @ 2020-10-29 09:07 浅滩浅 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 暴力 将除过最后一个和第一个元素外都当作山顶进行遍历 class Solution { public int longestMountain(int[] A) { //1-n-1可以作为中心点 int aSize = A.length; int maxLen = 0; for(int mid=1;m 阅读全文
posted @ 2020-10-25 15:28 浅滩浅 阅读(194) 评论(0) 推荐(0) 编辑