自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 LeetCode1071. 字符串的最大公因子

思路:使用辗转相除法求出两个字符串的最大公约数class Solution { public String gcdOfStrings(String str1, String str2) { //利用欧几里得辗转相除法求最大公约数 if(!(str1+str2).equals(str2+str1)){ return ""; ...

2020-03-12 15:02:06 94

原创 Leetcode1013. 将数组分成和相等的三个部分

主要是充分利用总数和的1/3这个条件class Solution { public boolean canThreePartsEqualSum(int[] A) { int p1 = 0; int p2 = 0; int sum = 0; for(int i = 0;i < A.length;i++){ ...

2020-03-11 12:14:41 257 1

原创 Leetcode322.零钱兑换

思路:一开始想起来的就是给现有的零钱进行排序,然后选取最大面值的零钱,进行回溯,找到就立即退出,这样的做法没有考虑到1,7,10 amount = 14这种情况,10,1,1,1,1与7,7肯定是最少两个硬币。如果说遍历所有的情况,不提前退出,会出现超时的情况。那么回溯的时候不用每次都只减一个coins[i],而是每次减个amount/coins[i]coins[i],这样的话也不行,...

2020-03-08 18:29:44 200

原创 LeetCode面试题11. 旋转数组的最小数字

时间复杂度为O(N)的很好达到,这里主要是列出二分查找的方法,时间复杂度是O(logN)numbers[mid] == numbers[r]时,r = r - 1是关键。class Solution { public int minArray(int[] numbers) { if(numbers.length < 1){ return ...

2020-03-07 20:31:04 116

原创 LeetCode面试题10- I. 斐波那契数列

要注意a%k + b%k = (a+b)%k这道题要在里面就对k取余,否则会溢出class Solution { public int fib(int n) { if(n <= 0){ return 0; } if(n == 1){ return 1; } ...

2020-03-07 18:39:23 223

原创 LeetCode面试题59.队列的最大值

存储最大值的用双向队列,从后面压入,从前面弹出class MaxQueue { Queue<Integer> queue; LinkedList<Integer> maxqueue; public MaxQueue() { queue = new LinkedList<>(); maxqueu...

2020-03-07 14:38:54 85

原创 LeetCode785.判断二分图

思路:是标色题,二种颜色class Solution { public boolean isBipartite(int[][] graph) { //标色题,DFS来标 int[] color = new int[graph.length]; for(int i = 0;i < graph.length;i++){ ...

2020-03-06 19:27:43 221

原创 LeetCode74. 搜索二维矩阵

思路:讲二维数组想象成一维数组进行二分查找主要是索引,行索引是i/n,列索引是i%nclass Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length < 1 || matrix[0].length < 1...

2020-03-06 18:35:15 80

原创 Leetcode面试题57 - II. 和为s的连续正数序列

思路:我只想到了暴力的方法,但是其实双指针才是正解class Solution { public int[][] findContinuousSequence(int target) { //双指针法 int p1 = 1,p2 = 2; ArrayList<int[]> list = new ArrayList<&gt...

2020-03-06 17:49:36 111

原创 LeetCode565. 数组嵌套

思路:在遍历一组集合的过程中,这个集合就是一个循环的关系,所以遍历过一个元素就不需要再遍历了。时间复杂度是O(N),空间复杂度是O(1)的做法class Solution { public int arrayNesting(int[] nums) { int max = 0; for(int i = 0;i < nums.leng...

2020-03-02 12:08:27 154

原创 LeetCode769. 最多能完成排序的块

思路:就是找规律的题,找不到规律就麻爪class Solution { public int maxChunksToSorted(int[] arr) { int max = 0,count = 0; for(int i = 0;i < arr.length;i++){ max = Math.max(arr[i],max);...

2020-03-02 12:02:39 155

原创 LeetCode667. 优美的排列 II

思路:是一道找规律的题代码:class Solution { public int[] constructArray(int n, int k) { int[] res = new int[n]; for(int i = 1;i < n - k;i++){ res[i-1] = i; } ...

2020-03-01 16:00:09 185

原创 LeetCode5345. 通过投票对团队排名

思路:统计每个字母从1-players排名出现的次数,然后自定义排序函数进行排序class Solution { public String rankTeams(String[] votes) { int voters = votes.length; int players = votes[0].length(); int[][] vot...

2020-03-01 14:53:12 285

原创 LeetCode5346.二叉树中的列表

思路:很明显是使用DFS,但是当我使用DFS的时候,竟然有两个用例没有通过看代码没看出来问题,后来发现当head.val != root.val的时候,跳过了树的当前层,但是链表并没有回到起点,相当于是找树的子序列了,这样遍历下去便不连续了,所以这是错误的/** * Definition for singly-linked list. * public class ListNode {...

2020-03-01 13:11:35 96

原创 LeetCode287. 寻找重复数

思路:要求比较严苛首先先介绍二分法,一种时间复杂度是O(nlogn),空间复杂度是O(1)的做法。class Solution { public int findDuplicate(int[] nums) { int left = 1,right = nums.length - 1; while(left < right){ ...

2020-03-01 10:30:40 137 1

空空如也

空空如也

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

TA关注的人

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