自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(31)
  • 收藏
  • 关注

原创 每日一道Leetcode - 941. 有效的山脉数组 【双指针】

class Solution { public boolean validMountainArray(int[] arr) { // 双指针,一个在数组前端,一个在数组后端 // 两个指针碰到一起即为成功 if(arr.length<3) return false; int i = 0; int j = arr.length-1; while(i+1<arr.length-1 &&.

2020-12-31 09:35:16 68

原创 每日一道Leetcode - 939. 最小面积矩形

参考官方题解,整体来说思路较简单,40001不加则会报错。class Solution { public int minAreaRect(int[][] points) { // 知道矩形对角线,就可以看另外两个点是否存在 Set<Integer> pointSet = new HashSet(); for(int[] point:points){ pointSet.add(40001*point[0]+poin.

2020-12-30 10:38:06 148

原创 每日一道Leetcode - 937. 重新排列日志文件【排序】

class Solution { public String[] reorderLogFiles(String[] logs) { Arrays.sort(logs,(log1,log2) -> { // 对于任意一对日志(log1,log2) // 每个log按空格拆分为两个字符串存入数组 String[] split1 = log1.split(" ",2); String[.

2020-12-29 10:37:21 134

原创 每日一道Leetcode - 935. 骑士拨号器【动态规划】

要注意的是数据类型,不然会出错这点真的没有python好啊class Solution { public int knightDialer(int n) { // 动态规划 // 按拨号键,除了5以外,每个数字的下一步都有2种可能的结果 // 1: 6 8 // 2: 7 9 // 3: 4 8 // 4: 3 9 // 5: 无 // 6: 1 7 .

2020-12-28 10:19:41 105

原创 每日一道Leetcode - 934. 最短的桥【广度遍历|深度遍历】

参考别人的做法,第一次碰到广度遍历/深度遍历的题目class Solution { public int shortestBridge(int[][] A) { int R = A.length; int C = A[0].length; int[][] direction = new int[][]{{1,0},{-1,0},{0,1},{0,-1}}; // 定义双端队列 // 创建一个队列,保存的是访问到的格子.

2020-12-27 11:38:55 175 1

原创 每日一道Leetcode - 933. 最近的请求次数【队列】

// 主要是题目的意思不太好读懂class RecentCounter { // 定义队列 Queue<Integer> q; public RecentCounter() { q = new LinkedList(); } public int ping(int t) { q.add(t); // 判断t-3000的起始位置,不满足条件的弹出 while(q.peek()&lt.

2020-12-26 09:32:03 85 1

原创 每日一道Leetcode -932. 漂亮数组【分治算法】

奇数在左,偶数在右class Solution { Map<Integer,int[]> memo; public int[] beautifulArray(int N) { // 数组内元素:1..N // 分治算法 memo = new HashMap(); return f(N); } public int[] f(int N){ if(memo.containsKey(N).

2020-12-25 09:47:40 136

原创 每日一道Leetcode - 931. 下降路径最小和【动态规划】

class Solution { public int minFallingPathSum(int[][] A) { // 动态规划问题 // 可选择的路径只有上一行的j,j-1.j+1位置,取最小 // 注意是方形数组,所以只需要有一个变量N即可 int N = A.length; int ans = Integer.MAX_VALUE; for(int i = N-1-1;i&gt.

2020-12-24 09:53:49 149 1

原创 每日一道Leetcode - 剑指 Offer 14- I. 剪绳子【动态规划】

class Solution { public int cuttingRope(int n) { int[] dp = new int[n+1]; for(int i = 2;i<=n;i++){ for(int j = 1;j<i;j++){ dp[i] = Math.max(dp[i],Math.max(j*(i-j),j*dp[i-j])); } } .

2020-12-23 10:44:14 173

原创 每日一道Leetcode - 面试题 08.05. 递归乘法

感觉就是用数学的思路class Solution { public int multiply(int A, int B) { if(A==0 || B==0){ return 0; } if(B == 1) return A; if(B<0){ B = -B; return -fun(A,B); } return fun(A.

2020-12-22 11:00:38 107

原创 每日一道Leetcode - 938. 二叉搜索树的范围和【中序遍历】

主要思路:中序遍历/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode .

2020-12-21 10:06:04 69

原创 每日一道Leetcode - 930. 和相同的二元子数组【双指针】

写的比较复杂了。class Solution { public int numSubarraysWithSum(int[] A, int S) { // 双指针 start = 0 end = start+1 // start固定,end找到不满足条件的1之后(当前和大于S),停止end指针的遍历 int count = 0; for(int start = 0;start<A.length;start++){ .

2020-12-20 09:26:07 139 1

原创 每日一道Leetcode - 923. 三数之和的多种可能【排序|三指针|排列组合】

class Solution { public int threeSumMulti(int[] A, int target) { // 计算每个数字出现的次数 // 计算三个数字为8的数字 三个数字 排序 三指针 // 每个数字选择 :C n k 几个里面选几个 (排列组合) // 定义字典存储每个数字的出现个数 Map<Integer,Integer> number = new HashMap<.

2020-12-19 14:32:26 419 1

原创 每日一道Leetcode-904. 水果成篮【双指针|滑动窗口】

class Solution { public int totalFruit(int[] tree) { // 只能选两种类型的水果 // 保证这两种类型的果树最多 // 看只有两种类型水果的一个区间中果树有多少 // 遍历记录前一个水果类型和当前的水果类型,num>2记录一下区间? // 滑动窗口类型:使用双指针,遍历,不一样的数字如果大于2就结束,确定区间,计算其中的果树 int ans = .

2020-12-18 10:53:20 236

原创 每日一道Leetcode - 897. 递增顺序查找树 【中序遍历】

简单的思路就是,中序遍历存储每个节点于数组中,再遍历数组,构建新的树,右子树,一直遍历下去。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeN.

2020-12-17 09:48:30 138

原创 每日一道Leetcode - 894. 所有可能的满二叉树【满二叉树】

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { Map<Integer,List<TreeNode>> memo = new HashMap(.

2020-12-16 11:13:58 94

原创 每日一道Leetcode-881. 救生艇 【双指针】

class Solution { public int numRescueBoats(int[] people, int limit) { // 先排序,双指针 //让最重的和最轻的一起 // 刚开始没看到每艘船最多载两个人.. Arrays.sort(people); int i = 0; int j = people.length-1; int ans = 0; w.

2020-12-15 10:59:47 133 1

原创 每日一道Leetcode - 845. 数组中的最长山脉 【动态规划】

class Solution { public int longestMountain(int[] arr) { if(arr.length<3) return 0; // 最初思路: //找到一个数使左边都小于它右边都大于它? // 锚点左边的数:左边都小于它,右边都大于它 // 锚点右边的数:左边都大于它,右边都小于它 // 锚点:大于左右两边的数字 // 找这样数字的边界,计.

2020-12-14 16:27:05 125

原创 每日一道Leetcode - 844. 比较含退格的字符串【双指针+字符串反向遍历】

class Solution { public boolean backspaceCompare(String S, String T) { int i = S.length()-1; int j = T.length()-1; int s1 = 0; int t1 = 0; while(i>=0 || j>=0){ // 对S字符串进行处理 .

2020-12-13 13:52:14 110

原创 每日一道Leetcode - 826. 安排工作以达到最大收益 【排序】

class Solution { public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { int N = difficulty.length; // 捆绑difficulty和profit数组 Work[] works = new Work[N]; for(int i =0;i<N;i++){ works.

2020-12-12 17:18:06 138

原创 每日一道Leetcode - 783. 二叉搜索树节点最小距离【中序遍历】

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { Integer pre,ans; public int minDiffInBST(TreeNode root).

2020-12-11 19:41:21 182

原创 每日一道Leetcode - 779. 第K个语法符号【递归】

class Solution { public int kthGrammar(int N, int K) { // 第N行有2^(N-1)个数字 // 每一行数字左右对称像回文字符串 // 比如第三行:01 10 // 如果K小于2^(N-1)的一半的话,直接找第2^(N-2)的前半部分的第K个即可【就是当前行的上一行】,大于则找第2^(N-2)行的K个取反即可 // base case if(N=.

2020-12-10 09:50:46 71

原创 每日一道Leetcode - 763. 划分字母区间 【双指针】【贪心算法】

使用双指针+贪心算法的思路先找到每个字符出现的最后位置再遍历的时候,找正好满足i == end的情况的分区class Solution { public List<Integer> partitionLabels(String S) { // 一共26个字母 int[] all_letter = new int[26]; // 遍历字符串记录每个字符的最后出现的位置 for(int i =0;i<S.lengt.

2020-12-09 12:42:45 71

原创 每日一道Leetcode - 713. 乘积小于K的子数组【双指针+滑动窗口】

自己写的(超时了):class Solution { public int numSubarrayProductLessThanK(int[] nums, int k) { if(k<=1) return 0; int ans = 0; for(int i=0;i<nums.length;i++){ int acc = nums[i]; if(acc<k){ .

2020-12-08 09:53:01 90

原创 每日一道Leetcode - 15. 三数之和 【双指针】

收获:学会List的嵌套使用List<List> result = new LinkedList<>();class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new LinkedList<>(); if(nums.length==0) ret.

2020-12-07 15:14:36 100

原创 每日一道Leetcode - 14. 最长公共前缀【字符串】

class Solution { public String longestCommonPrefix(String[] strs) { // 如果字符串数组为空,返回“” if(strs.length==0) return ""; // 数组第一个字符串设置为ans的默认值 String ans = strs[0]; // 让之后的每一个字符串都和ans取子串更新 // 如果这个过程中ans为“”了,直.

2020-12-06 08:59:33 1129

原创 每日一道Leetcode - 13. 罗马数字转整数【字符串】

看到题解中的一个有意思的思路:直接使用字符串的replace以及switch。我的思路是使用HashMap, 再使用字符串的截取,有一点不好处理的就是字符串的边界问题。class Solution { public int romanToInt(String s) { int ans = 0; // 直接使用字符串替换 s = s.replace("IV","a"); s = s.replace("IX","b"); .

2020-12-05 09:48:01 115

原创 每日一道Leetcode -12. 整数转罗马数字【字符串】

class Solution { // 思路 // 定义两个数组变量分别存储【字符,数值】, 然后遍历 // 如果大于i, 当前值减去i,字符串加值,只要不等于0就继续往前滑动 // 因为4,9,40,90,400,900是特例,所以也存到之前的地方 // 从大到小存入不同数值 int[] keys = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; // 将上面对应数值存入字符数组 String[].

2020-12-04 09:16:33 92

原创 每日一道Leetcode - 11. 盛最多水的容器【双指针】

刚开始做的思路知道是使用双指针,但是思维有点局限于只能移动某一个指针,靠着固定指针i,滑动指针j,来寻找最大值。class Solution { public int maxArea(int[] height) { // 同时移动两个指针 int n = height.length; int ans = 0; int i = 0; int j = n-1; while(i!=j){ .

2020-12-03 09:43:01 70

原创 每日一道Leetcode - 3. 无重复字符的最长子串【滑动窗口】

主要的思路:字符串从前往后遍历,用到HashMap, 遍历的过程中存放当前值以及它对应的下标。固定一个值slide作为字符串的起点,另一个指针i记录当前列表的位置,如果不在HashMap中,就存进去,如果碰到一样的字符(在HashMap中),就将slide放到前一个相同的字符的后一个开始,同时更新当前节点的下标。class Solution { public int lengthOfLongestSubstring(String s) { if(s.length()==0){ .

2020-12-02 10:14:29 72

原创 每日一道Leetcode - 16. 最接近的三数之和 【数组-双指针】

对数组进行排序,遍历数组,固定当前值,用双指针,left:从当前值的后一个值开始,right:从数组的最后一个节点开始,相向而行,【对三个值进行相加sum比较与target的距离】 和 【当前最小的结果ans与target的距离相比大小】,选择最小的更新ans,然后再进一步看当前sum和target的大小,如果更大,可以考虑right指针左移,再找小点的值;如果更小考虑left指针右移,两个指针如果相遇,就继续往数组的下一个值走,固定下一个值,再去确定left指针与right指针,循环往复即可。cla.

2020-12-01 09:58:02 106

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除