自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 每日一道Leetcode——汇总区间

题目:我的解法:class Solution { public List<String> summaryRanges(int[] nums) { int n = nums.length; List<String> ans = new ArrayList<String>(); if(n==0){ return ans; } int start = 0;

2021-01-10 00:33:57 140

原创 每日一道Leetcode——旋转数组

题目:我的解法:class Solution { public void rotate(int[] nums, int k) { int temp = 0; int n = nums.length; while(k>0){ temp = nums[n-1]; for(int i=n-1; i>0; i--){ nums[i] = nums[i-1];

2021-01-08 14:11:27 185

原创 每日一道Leetcode——省份数量

题目:我的解法:Folyd算法class Solution { public int findCircleNum(int[][] isConnected) { int n = isConnected.length; for(int k=0; k<n; k++){ for(int i=0; i<n; i++){ for(int j=0; j<n; j++){

2021-01-07 15:54:04 133

原创 每日一道Leetcode——较大分组的位置(1.5)

题目:我的解法:class Solution { public List<List<Integer>> largeGroupPositions(String s) { int n = s.length(); List<List<Integer>> list = new ArrayList<List<Integer>>(); if(n==0){ retu

2021-01-06 23:25:33 88

原创 每日一道Leetcode——除法求值

题目:我的解法:参考题解Floyd 算法class Solution { public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) { int nums = 0; HashMap<String, Integer> map = new HashMap<

2021-01-06 23:20:23 71

原创 每日一道Leetcode——分隔链表

题目:我的解法:创建两个新链表,合并/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode partition(ListNode head, int x) { List

2021-01-04 17:33:28 59

原创 每日一道Leetcode——斐波那契数

题目:我的解法:动态规划class Solution { public int fib(int n) { if(n==0 || n==1){ return n; } int last_two = 0; int last_one = 1; int cur = -1; for(int i=2; i<n+1; i++){ cur = last_one

2021-01-04 17:15:07 55

原创 每日一道Leetcode——无重叠区间

题目:我的解法:贪心class Solution { public int eraseOverlapIntervals(int[][] intervals) { if(intervals.length==0){ return 0; } Arrays.sort(intervals, new Comparator<int[]>(){ public int compare(int[]

2020-12-31 13:58:44 71

原创 每日一道Leetcode——最后一块石头的重量

题目:我的解法:最大堆class Solution { public int lastStoneWeight(int[] stones) { PriorityQueue<Integer> pq = new PriorityQueue<Integer>( new Comparator<Integer>(){ public int compare(Integer stone1, Intege

2020-12-30 16:57:37 63

原创 每日一道Leetcode——按要求补齐数组

题目:官方题解:class Solution { public int minPatches(int[] nums, int n) { int patches = 0; long x = 1; int length = nums.length, index = 0; while (x <= n) { if (index < length && nums[index] <=

2020-12-29 11:36:58 96

原创 每日一道Leetcode——买卖股票的最佳时机(有最大交易次数)

题目:我的解法:动态规划class Solution { public int maxProfit(int k, int[] prices) { int n = prices.length; if(n==0){ return 0; } // have[i][j]表示第i天有股票在手里,且交易次数为j;nohave[i][j]表示第i天没有股票在手里,交易次数为j int[][] have =

2020-12-29 09:18:21 201 1

原创 每日一道Leetcode——最大矩形

题目:我的解法:参考官方题解——柱状图class Solution { public int maximalRectangle(char[][] matrix) { int ans = 0; int rows = matrix.length; if(rows==0){ return ans; } int cols = matrix[0].length; // left矩阵记录

2020-12-26 19:04:24 169

原创 每日一道Leetcode——分发糖果(12.24)

题目:我的解法:正向遍历+反向遍历class Solution { public int candy(int[] ratings) { int n = ratings.length; int[] left = new int[n]; for(int i=0; i<n; i++){ if(i>0 && ratings[i] > ratings[i-1]){ l

2020-12-25 15:28:53 68

原创 每日一道Leetcode——分发饼干

题目:我的解法:排序class Solution { public int findContentChildren(int[] g, int[] s) { int max = 0; boolean have = false; Arrays.sort(g); Arrays.sort(s); for(int i=0; i<g.length; i++){ int start = 0;

2020-12-25 15:18:22 72

原创 每日一道Leetcode——字符串中的第一个唯一字符

题目:我的解法:计数class Solution { public int firstUniqChar(String s) { int[] num = new int[26]; for(int i=0; i<s.length(); i++){ char c = s.charAt(i); num[c-'a']++; } for(int j=0; j<s.length();

2020-12-23 09:51:14 76

原创 每日一道Leetcode——二叉树的锯齿形层序遍历

题目:我的解法:广度优先搜索思路:先按照层序遍历将每一层节点值存入一个list,然后将奇数层的list进行反转,最后存入答案list/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solut

2020-12-22 10:00:22 107 1

原创 每日一道Leetcode——使用最小花费爬楼梯

题目:我的解法:动态规划class Solution { public int minCostClimbingStairs(int[] cost) { int ans = 0; int n = cost.length; int[] sum = new int[n]; if(n==1){ return cost[0]; } if(n==2){ retu

2020-12-21 10:24:36 98

原创 每日一道Leetcode——去除重复字母

题目:我的解法:参考官方题解——贪心+栈class Solution { public String removeDuplicateLetters(String s) { // 记录字母剩余的个数 int[] num = new int[26]; // 记录字母是否存在与栈中 boolean[] exist = new boolean[26]; int d = s.length(); for(int

2020-12-20 17:36:46 259

原创 每日一道Leetcode——找不同

题目:我的解法:排序class Solution { public char findTheDifference(String s, String t) { char[] array1 = s.toCharArray(); char[] array2 = t.toCharArray(); Arrays.sort(array1); Arrays.sort(array2); int i=0; while

2020-12-18 19:08:52 122

原创 买卖股票的最佳时机II(没有手续费)

题目:我的解法:动态规划class Solution { public int maxProfit(int[] prices) { int d = prices.length; int buy = -prices[0], nobuy = 0; for(int i=1; i<d; i++){ nobuy = Math.max(nobuy, buy + prices[i]); buy = M

2020-12-17 15:25:10 65

原创 每日一道Leetcode——买卖股票的最佳时机含手续费

题目:我的解法:动态规划class Solution { public int maxProfit(int[] prices, int fee) { int d = prices.length; int[][] money = new int[d][2]; money[0][0] = 0; money[0][1] = -prices[0]; for(int i=1; i<d; i++){

2020-12-17 15:06:58 111 1

原创 每日一道Leetcode——单词规律

题目:我的解法:HashMapclass Solution { public boolean wordPattern(String pattern, String s) { Map<Character, String> map = new HashMap<Character, String>(); String[] array = s.split(" "); if(pattern.length()!=array.lengt

2020-12-16 09:57:35 62

原创 每日一道Leetcode——单调递增的数字

题目:我的解法:class Solution { public int monotoneIncreasingDigits(int N) { Deque<Integer> queue = new LinkedList<Integer>(); int num = N; int len = 0; int ans = 0; // 将整数N的每一位存到队列里 while(num!=0)

2020-12-15 11:48:50 141

原创 每日一道Leetcode——Dota2参议院(2020.12.11)

题目:我的解法:class Solution { public String predictPartyVictory(String senate) { char[] array = senate.toCharArray(); int R_num = 0; int D_num = 0; List<Character> list = new ArrayList<Character>(); for

2020-12-14 18:04:34 70

原创 每日一道Leetcode——摆动序列(2020.12.12)

题目:官方题解:方法一:动态规划class Solution { public int wiggleMaxLength(int[] nums) { int n = nums.length; if (n < 2) { return n; } int[] up = new int[n]; int[] down = new int[n]; up[0] = down[0]

2020-12-14 15:36:13 61 1

原创 每日一道Leetcode——存在重复元素(2020.12.13)

题目:我的解法:hashsetclass Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<Integer>(); for(int num: nums){ if(!set.add(num)){ return true; }

2020-12-14 15:07:44 75 1

原创 每日一道Leetcode——字母异位词分组(2020.12.14)

题目:我的解法:排序class Solution { public List<List<String>> groupAnagrams(String[] strs) { List<List<String>> ans = new ArrayList<List<String>>(); Map<String, List<String>> map = new HashMap&l

2020-12-14 14:57:50 68

原创 每日一道Leetcode——柠檬水找零

题目:我的解法:class Solution { public boolean lemonadeChange(int[] bills) { int[] money = new int[2]; for(int bill: bills){ if(bill==5){ // 如果给的5块,不用找 money[0]++; }else if(bill==10){ // 如果给的10块,只能找

2020-12-10 10:01:00 70

原创 每日一道Leetcode——不同路径

题目:我的解法一:递归(超时了)class Solution { int path_num = 1; public int uniquePaths(int m, int n) { goOneStep(0, 0, m, n); return path_num; } public void goOneStep(int x, int y, int m, int n){ if(x+1<m && y+1&lt

2020-12-09 13:54:56 50

原创 每日一道Leetcode——将数组拆分成斐波那契序列(12.8)

题目:官方题解:回溯+剪枝class Solution { public List<Integer> splitIntoFibonacci(String S) { List<Integer> list = new ArrayList<Integer>(); backtrack(list, S, S.length(), 0, 0, 0); return list; } public boole

2020-12-08 11:33:27 76

原创 每日一道Leetcode——任务调度器(12.5)

题目:我的解法:超时了class Solution { public int leastInterval(char[] tasks, int n) { int d = tasks.length; if(d==0){ return 0; } // 用数组记录每个任务出现的次数 int[] count = new int[26]; for(int i=0; i<d; i++

2020-12-07 21:05:03 135

原创 每日一道Leetcode——分割数组为连续子序列(12.4)

题目:官方题解:思路:用哈希表维护以x为结尾的序列长度最小堆,哈希表的键表示结尾数字x,哈希表的值表示最小堆(最小堆中是以x为结尾的序列长度)。class Solution { public boolean isPossible(int[] nums) { Map<Integer, PriorityQueue<Integer>> map = new HashMap<Integer, PriorityQueue<Integer>&gt

2020-12-07 18:32:32 64

原创 每日一道Leetcode——计数质数(12.3)

题目:我的解法:暴力法,超出时间限制class Solution { public int countPrimes(int n) { if(n<2){ return 0; } int count = 0; for(int i=2; i<n; i++){ if (judge_prime(i)){ count++; }

2020-12-07 15:33:42 44

原创 每日一道Leetcode——翻转矩阵后的得分

题目:我的解法:class Solution { public int matrixScore(int[][] A) { int[][] last_A = A; int[][] transed_A = trans(A); int max_sum = calculateSum(last_A); // 判断矩阵翻转后总和是否变大 while(calculateSum(transed_A)>max_sum){

2020-12-07 11:38:51 78

原创 每日一道Leetcode——杨辉三角(12.6)

题目:我的解法:class Solution { public List<List<Integer>> generate(int numRows) { List<Integer> cur_row = new ArrayList<Integer>(); List<List<Integer>> ans = new ArrayList<List<Integer>>();

2020-12-06 19:49:23 59

原创 每日一道Leetcode——拼接最大数

题目:官方题解:class Solution { public int[] maxNumber(int[] nums1, int[] nums2, int k) { int m = nums1.length, n = nums2.length; int[] maxSubsequence = new int[k]; int start = Math.max(0, k - n), end = Math.min(k, m); // 两

2020-12-02 19:45:14 322

原创 每日一道Leetcode——在排序数组中查找元素的第一个和最后一个位置

题目:我的解法:双指针class Solution { public int[] searchRange(int[] nums, int target) { int start = -1; int end = -1; int d = nums.length; for(int i=0; i<d; i++){ if(nums[i]==target){ if(start==-

2020-12-01 10:40:52 66

原创 每日一道Leetcode——重构字符串(2020.11.30)

题目:官方题解一:基于最大堆的贪心算法整体思路:维护最大堆存储字母,计算每个字母出现次数,注意出现次数不能超过(len+1)/2,否则怎样排序都无法满足要求,按出现次数在堆中对字母由大到小排序。当堆中字母个数大于1时,每次从堆顶取出两个字母,加入StringBuilder,相应次数减1,如果出现次数不为0,则继续将该字母放回堆中;当堆中只剩下1个字母时,取出最后一个字母添加到StringBuilder。class Solution { public String reorganizeStr

2020-12-01 09:53:19 85

原创 每日一道Leetcode——四数相加II

题目:我的解法:暴力法,遍历所有四个数的组合,超时了。官方题解:

2020-11-27 15:56:21 73

原创 每日一道Leetcode——最大间距

题目:我的解法:sort排序,但是时间复杂度超了。class Solution { public int maximumGap(int[] nums) { int d = nums.length; if(d < 2){ return 0; } Arrays.sort(nums); int max = 0; for(int i=0; i+1<d; i++){

2020-11-26 10:19:36 102

空空如也

空空如也

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

TA关注的人

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