
每日一题
leetcode每日一题
Chamberlain T
尼蝶
展开
-
LeetCode.1470. 重新排列数组
【代码】LeetCode.1470. 重新排列数组。原创 2022-08-30 02:54:43 · 150 阅读 · 0 评论 -
LeetCode.1656. 设计有序流
LeetCode.1656. 设计有序流原创 2022-08-16 10:25:34 · 147 阅读 · 0 评论 -
LeetCode.1422. 分割字符串的最大得分
LeetCode.1422. 分割字符串的最大得分原创 2022-08-14 11:42:26 · 301 阅读 · 0 评论 -
LeetCode.1282. 用户分组
LeetCode.1282. 用户分组原创 2022-08-12 14:00:19 · 187 阅读 · 0 评论 -
LeetCode.806. 写字符串需要的行数
LeetCode.806. 写字符串需要的行数难度:easyclass Solution { public int[] numberOfLines(int[] widths, String s) { int len = s.length(); int[] ans = {1, 0}; int eachWidth = 100; int sum = 0; for (int i = 0; i < len;原创 2022-04-12 14:31:12 · 320 阅读 · 0 评论 -
LeetCode.172. 阶乘后的零
LeetCode.172. 阶乘后的零难度:medium方法一:统计阶乘的各项中有多少个5的倍数:class Solution { public int trailingZeroes(int n) { // 即阶乘的各项中有多少个5的倍数 int count = 0; for (int i = 5; i <= n; i++) { for (int j = i; j % 5 == 0; j /= .原创 2022-03-27 20:46:40 · 480 阅读 · 0 评论 -
LeetCode.2028. 找出缺失的观测数据
LeetCode.2028. 找出缺失的观测数据难度:medium模拟法:class Solution { public int[] missingRolls(int[] rolls, int mean, int n) { int sumTotal = mean * (n + rolls.length); int sumMissing = sumTotal; for (int i = 0; i < rolls.le...原创 2022-03-27 09:37:08 · 460 阅读 · 0 评论 -
LeetCode.682. 棒球比赛
682.棒球比赛难度:easyclass Solution { public int calPoints(String[] ops) { List<Integer> list = new ArrayList<>(); int ans = 0; for (String op: ops) { int len = list.size(); switch (op.charAt(0原创 2022-03-26 10:13:26 · 181 阅读 · 0 评论 -
LeetCode.504.7进制数
LeetCode.504.7进制数难度:easyJava:class Solution { public String convertToBase7(int num) { if (num == 0) { return "0"; } StringBuilder sb = new StringBuilder(); boolean flag = num < 0; num .原创 2022-03-07 20:09:22 · 147 阅读 · 0 评论 -
LeetCode.1706.球会落何处
LeetCode.1706.球会落何处难度:medium思路:模拟class Solution { public int[] findBall(int[][] grid) { int len = grid.length; int width = grid[0].length; int[] ans = new int[width]; // 从i出发 for (int i = 0; i <..原创 2022-02-24 22:43:38 · 242 阅读 · 0 评论 -
LeetCode.1688. 比赛中的配对次数
LeetCode.1688. 比赛中的配对次数难度:easy简单题,用模拟的方法,n记录剩余待比赛的队伍,n为n除以2的商和余数的和,直至n为表示比赛结束,count记录每次除法的商的和;Java:class Solution { public int numberOfMatches(int n) { int count = 0; int num = n; while (n > 1) { int a.原创 2022-01-25 11:54:50 · 289 阅读 · 0 评论 -
LeetCode.332. 删除回文子序列
LeetCode.332. 删除回文子序列难度:easy模拟+双指针:看哥么的注释Java:class Solution { public int removePalindromeSub(String s) { // 删除的是子序列,即删除的序列中的元素不一定是连续的 // 因为字符串s中只有两个元素,所以最多只需要两次删除 // 若s本身就是回文序列,则只需要一次删除 int len = s.le..原创 2022-01-22 21:02:03 · 485 阅读 · 0 评论 -
LeetCode.219. 存在重复元素 II
LeetCode.219. 存在重复元素 II难度:easy首先尝试用暴力法,结果超时了:class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { int len = nums.length; if (len < 2) { return false; } for (int i = 0; i &l原创 2022-01-19 22:58:50 · 159 阅读 · 0 评论 -
LeetCode.539. 最小时间差
LeetCode.539. 最小时间差难度:medium思路:先进行升序排序,然后最小时间差一定存在在两个相邻的时间差里面,可以通过鸽巢原理进行剪枝:如果timesPoints列表的时间数大于24*60个,那么就一定有两个相同的时间在列表里面,此时最小时间差为0;Java:class Solution { public int findMinDifference(List<String> timePoints) { //鸽巢原理+排序原创 2022-01-18 13:56:20 · 356 阅读 · 0 评论 -
LeetCode.382. 链表随机节点
LeetCode.382. 链表随机节点方法一:动态整数数组Java:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode n..原创 2022-01-16 23:01:30 · 300 阅读 · 1 评论 -
LeetCode.1078.Bigram分词
LeetCode.1078.Bigram分词难度:easyJava:class Solution { public String[] findOcurrences(String text, String first, String second) { String[] stext = text.split(" "); List<String> third = new ArrayList<>(); fo..原创 2021-12-26 12:25:38 · 184 阅读 · 0 评论 -
LeetCode.506.相对名次
难度:easyJava:使用hashmap或者二维数组来存储score排序前的顺序就可以啦;class Solution { public String[] findRelativeRanks(int[] score) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < score.length; i++) { ..原创 2021-12-24 20:09:10 · 347 阅读 · 0 评论 -
LeetCode.1154.一年中的第几天
LeetCode.1154.一年中的第几天难度:easy需要注意的就两个地方:如何提取字符串中的数字; 闰年:能别4整除,或者能被400整除,但不能被100整除的年份;Java:class Solution { public int dayOfYear(String date) { //保存日历月份天数 int[] calendar = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};..原创 2021-12-21 14:19:08 · 233 阅读 · 0 评论 -
LeetCode.1446.连续字符
LeetCode.1446.连续字符 18 / 100难度:easy每日一题:Java:一次遍历,可以用双指针,但那样时间复杂度会更高;class Solution { public int maxPower(String s) { int max = 1; int count = 1; // if (s.length() ==1) { // return max; // } ..原创 2021-12-21 10:31:34 · 219 阅读 · 0 评论 -
LeetCode.475.供暖器
LeetCode.475.供暖器难度:medium可以采用双指针分别遍历houses和heaters,哥么的注释很详细;Java:排序+双指针:class Solution { public int findRadius(int[] houses, int[] heaters) { //排序 Arrays.sort(houses); Arrays.sort(heaters); //加热半径 i.原创 2021-12-20 23:59:25 · 308 阅读 · 0 评论 -
LeetCode.997.找到小镇法官
LeetCode.997.找到小镇法官2021.12.19的每日一题难度:easy图中出度入度的应用;入度是指向该节点的边的数量:统计相信 i 的人的数量;出度是该节点出发的边的数量:统计 i 相信的人的数量;法官是被n-1个人相信,而相信0个人的人;Java:class Solution { public int findJudge(int n, int[][] trust) { //入度:相信i的人数为trustIn[i] .原创 2021-12-19 01:12:36 · 365 阅读 · 0 评论 -
LeetCode.1518.换酒问题
力扣2021.12.17的每日一题难度:easy目前想到两种方法:模拟法和数学法:Java:模拟法:class Solution { public int numWaterBottles(int numBottles, int numExchange) { int totalNum = numBottles; //统计最多能喝多少瓶酒 while(numBottles >= numExchange) { ..原创 2021-12-17 09:44:20 · 489 阅读 · 0 评论 -
LeetCode.419.甲板上的战舰
LeetCode.419.甲板上的战舰难度:medium这道题建议看英文版,翻译的有歧义,统计的是战舰群(battleships)的数量而不是战舰(battleship)的数量; 这道题是岛屿问题的小变形:岛屿问题 有了思路5min就可以ACJava:class Solution { public int countBattleships(char[][] board) { int count = 0; for (int i = 0;..原创 2021-12-18 20:46:20 · 410 阅读 · 0 评论