leetcode练习
每日更新leetcode练习题,欢迎关注
路Louis
这个作者很懒,什么都没留下…
展开
-
2023.2.1-----leetcode.2325
public String decodeMessage(String key, String message) { int next='a'; int[] cs=new int[26]; // int idx=0; Arrays.fill(cs,-1); for(int i=0;i原创 2023-02-01 20:21:55 · 151 阅读 · 0 评论 -
2023.1.31-----leetcode.2319
public boolean checkXMatrix(int[][] grid) { int n = grid.length; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j || (i + j) == (n - 1)) { if (grid[i][j] == 0) {原创 2023-01-31 21:56:57 · 106 阅读 · 0 评论 -
2023.1.30-----leetcode.1669
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { ListNode p = list1, q = list1; while (--a > 0) { p = p.next; } while (b-- > 0) { q = q.next; } p.next =原创 2023-01-30 21:26:43 · 88 阅读 · 0 评论 -
2023.1.29-----leetcode.2315
public int countAsterisks(String s) { int ans = 0; for (int i = 0, ok = 1; i < s.length(); ++i) { char c = s.charAt(i); if (c == '*') { ans += ok; } else if (c == '|') { ok原创 2023-01-29 22:01:41 · 101 阅读 · 0 评论 -
2023.1.28-----leetcode.1664
public int waysToMakeFair(int[] nums) { int n = nums.length - 1; if (n == 0) return 1; int[] presum1 = new int[nums.length+1];//偶数 int[] presum2 = new int[nums.length+1];//奇数 presum1[0] = nums[0]; presum1[1]原创 2023-01-28 21:32:13 · 87 阅读 · 0 评论 -
2023.1.27-----leetcode.2309
public String greatestLetter(String s) { Set ht = new HashSet(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); ht.add(c); } for (int i = 25; i >= 0; i--) {原创 2023-01-27 21:09:45 · 83 阅读 · 0 评论 -
2023.1.26-----leetcode.1663
public String getSmallestString(int n, int k) { StringBuilder ans = new StringBuilder(); for (int i = 1; i原创 2023-01-26 20:22:19 · 81 阅读 · 0 评论 -
2023.1.24-----leetcode.1828
public int[] countPoints(int[][] points, int[][] queries) { int m = queries.length; int[] ans = new int[m]; for (int k = 0; k < m; ++k) { int x = queries[k][0], y = queries[k][1], r = queries[k][2]; for (var原创 2023-01-24 20:45:06 · 77 阅读 · 0 评论 -
2023.1.23-----leetcode.2303
public double calculateTax(int[][] brackets, int income) { int n=brackets.length; if(income原创 2023-01-23 19:35:39 · 80 阅读 · 0 评论 -
2023.1.20-----leetcode.1817
public int[] findingUsersActiveMinutes(int[][] logs, int k) { Map d = new HashMap(); for (var log : logs) { int i = log[0], t = log[1]; d.computeIfAbsent(i, key -> new HashSet()).add(t);原创 2023-01-20 20:38:03 · 85 阅读 · 0 评论 -
2023.1.19-----leetcode.2299
public boolean strongPasswordCheckerII(String password) { if (password.length() < 8) { return false; } int mask = 0; for (int i = 0; i < password.length(); ++i) { char c = password.charAt(i);原创 2023-01-19 21:26:17 · 81 阅读 · 0 评论 -
2023.1.18-----leetcode.1825
private int m, k; private Queue q; private TreeMap s1; private TreeMap s2; private TreeMap s3; private int size1, size2, size3; private long sum2; public MKAverage原创 2023-01-18 21:26:13 · 80 阅读 · 0 评论 -
2023.1.17-----leetcode.1814
public int countNicePairs(int[] nums) { Map cnt = new HashMap(); for (int x : nums) { int y = x - rev(x); cnt.merge(y, 1, Integer::sum); } final int mod = (int) 1e9 + 7; lo原创 2023-01-17 20:25:25 · 105 阅读 · 0 评论 -
2023.1.16-----leetcode.1813
public boolean areSentencesSimilar(String sentence1, String sentence2) { var words1 = sentence1.split(" "); var words2 = sentence2.split(" "); if (words1.length < words2.length) { var t = words1; words1 = wor原创 2023-01-16 21:11:17 · 87 阅读 · 0 评论 -
2023.1.15-----leetcode.2293
public int minMaxGame(int[] nums) { for (int n = nums.length; n > 1;) { n >>= 1; for (int i = 0; i < n; ++i) { int a = nums[i原创 2023-01-15 20:36:39 · 75 阅读 · 0 评论 -
2023.1.13-----leetcode.2287
public int rearrangeCharacters(String s, String target) { int[] cnt1 = new int[26]; int[] cnt2 = new int[26]; for (int i = 0; i < s.length(); ++i) { ++cnt1[s.charAt(i) - 'a']; } for (int i = 0; i < target原创 2023-01-13 21:19:42 · 86 阅读 · 0 评论 -
2023.1.12-----leetcode.1807
public String evaluate(String s, List knowledge) { Map d = new HashMap(knowledge.size()); for (var e : knowledge) { d.put(e.get(0), e.get(1)); } StringBuilder ans = new StringBuild原创 2023-01-12 20:58:20 · 81 阅读 · 0 评论 -
2023.1.11-----leetcode.2283
public boolean digitCount(String num) { Map h = new HashMap(); int n = num.length(); for (int i = 0; i < n; i++) { int digit = num.charAt(i) - '0'; h.put(digit, h.getOrDef原创 2023-01-11 20:13:35 · 79 阅读 · 0 评论 -
2023.1.8-----leetcode.2185
public int prefixCount(String[] words, String pref) { int res = 0; for (String word : words) { if (word.startsWith(pref)) { res++; } } return res; }原创 2023-01-08 20:41:28 · 59 阅读 · 0 评论 -
2023.1.7-----leetcode.1658
public int minOperations(int[] nums, int x) { int target = -x; for (int num : nums) target += num; if (target < 0) return -1; // 全部移除也无法满足要求 int ans = -1, left = 0, sum = 0, n = nums.length; for (int right = 0; righ原创 2023-01-07 20:28:50 · 71 阅读 · 0 评论 -
2023.1.6-----leetcode.2180
public int countEven(int num) { int y = num / 10, x = num % 10; int res = y * 5, ySum = 0; while (y != 0) { ySum += y % 10; y /= 10; } if (ySum % 2 == 0) { res += x / 2 + 1;原创 2023-01-06 20:41:59 · 82 阅读 · 0 评论 -
2023.1.4-----leetcode.1802
public int maxValue(int n, int index, int maxSum) { int left = 1, right = maxSum; while (left < right) { int mid = (left + right + 1) >>> 1; if (sum(mid - 1, index) + sum(mid, n - index)原创 2023-01-04 19:21:25 · 76 阅读 · 0 评论 -
2023.1.3-----leetcode.2042
public boolean areNumbersAscending(String s) { int pre = 0, pos = 0; while (pos < s.length()) { if (Character.isDigit(s.charAt(pos))) { int cur = 0; while (pos < s.length() && Character.isDigit(s原创 2023-01-03 19:35:14 · 105 阅读 · 0 评论 -
2023.1.2-----leetcode.1801
public int getNumberOfBacklogOrders(int[][] orders) { final int MOD = 1000000007; PriorityQueue buyOrders = new PriorityQueue((a, b) -> b[0] - a[0]); PriorityQueue sellOrders = new PriorityQueue((a, b)原创 2023-01-02 20:46:29 · 63 阅读 · 0 评论 -
2023.1.1-----leetcode.2351
public char repeatedCharacter(String s) { Set seen = new HashSet(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (!seen.add(ch)) { return ch; }原创 2023-01-01 19:03:46 · 96 阅读 · 0 评论 -
2022.12.31-----leetcode.2037
public int minMovesToSeat(int[] seats, int[] students) { Arrays.sort(seats); Arrays.sort(students); int res = 0; for (int i = 0; i < seats.length; i++) { res += Math.abs(seats[i] - students[i]); }原创 2022-12-31 21:18:00 · 95 阅读 · 0 评论 -
2022.12.30-----leetcode.855
int N; TreeSet students; public ExamRoom(int N) { this.N = N; students = new TreeSet(); } public int seat() { //Let's determine student, the position of the next //student to sit down. int原创 2022-12-30 19:38:12 · 77 阅读 · 0 评论 -
2022.12.29-----leetcode.2032
public List twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) { Map map = new HashMap(); for (int i : nums1) { map.put(i, 1); } for (int i : nums2) { ma原创 2022-12-29 20:13:03 · 94 阅读 · 0 评论 -
2022.12.28-----leetcode.1750
public int minimumLength(String s) { int n = s.length(); int left = 0, right = n - 1; while (left < right && s.charAt(left) == s.charAt(right)) { char c = s.charAt(left); while (left原创 2022-12-28 20:22:47 · 101 阅读 · 0 评论 -
2022.12.27-----leetcode.2027
public int minimumMoves(String s) { int ans = 0; for (int i = 0; i < s.length(); ++i) { if (s.charAt(i) == 'X') { ++ans; i += 2; } } return ans; }原创 2022-12-27 19:49:26 · 89 阅读 · 0 评论 -
2022.12.26-----leetcode.1759
public int countHomogenous(String s) { final int MOD = 1000000007; long res = 0; char prev = s.charAt(0); int cnt = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == pre原创 2022-12-26 20:24:49 · 64 阅读 · 0 评论 -
2022.12.25-----leetcode.1739
public int minimumBoxes(int n) { int x = (int) Math.cbrt(6L * n); int ans = x * (x + 1) / 2; int maxN = (int) ((long) x * (x + 1) * (x + 2) / 6); if (maxN > n) { maxN -= ans; ans -= x; }原创 2022-12-25 20:11:10 · 98 阅读 · 0 评论 -
2022.12.24-----leetcode.1754
public String largestMerge(String w1, String w2) { StringBuilder sb = new StringBuilder(); while (w1.length() + w2.length() > 0) { if (w1.compareTo(w2) > 0) { sb.append(w1.charAt(0)); w1 = w1.subs原创 2022-12-24 19:59:10 · 99 阅读 · 0 评论 -
2022.12.23-----leetcode.2011
public int finalValueAfterOperations(String[] operations) { int x = 0; for (String op : operations) { if ("X++".equals(op) || "++X".equals(op)) { x++; } else { x--; }原创 2022-12-23 21:20:07 · 61 阅读 · 0 评论 -
2022.12.21-----leetcode.1753
public int maximumScore(int a, int b, int c) { int sum = a + b + c; int maxVal = Math.max(Math.max(a, b), c); return Math.min(sum - maxVal, sum / 2); }原创 2022-12-22 20:16:58 · 69 阅读 · 0 评论 -
2022.12.20-----leetcode.1760
public int minimumSize(int[] nums, int maxOperations) { int left = 1, right = Arrays.stream(nums).max().getAsInt(); int ans = 0; //在不超过给定次数情况下,找出最小的值 while (left原创 2022-12-22 20:29:43 · 64 阅读 · 0 评论 -
2022.12.19-----leetcode.1971
class Solution { public boolean validPath(int n, int[][] edges, int source, int destination) { List[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList(); } for (int[]原创 2022-12-20 19:53:05 · 69 阅读 · 0 评论 -
2022.12.18-----leetcode.1703
public int minMoves(int[] nums, int k) { var p = new ArrayList(); for (int i = 0; i < nums.length; ++i) if (nums[i] != 0) p.add(i - p.size()); int m = p.size(); int[] s = new int[m + 1]; // p 的前缀和原创 2022-12-22 21:02:12 · 106 阅读 · 0 评论 -
2022.12.17-----leetcode.1764
public boolean canChoose(int[][] groups, int[] nums) { int i = 0; for (int k = 0; k < nums.length && i < groups.length;) { if (check(groups[i], nums, k)) { k += groups[i].length; i++;原创 2022-12-17 20:31:45 · 68 阅读 · 0 评论 -
2022.12.16-----leetcode.1785
public int minElements(int[] nums, int limit, int goal) { long sum = 0; for (int x : nums) { sum += x; } long diff = Math.abs(sum - goal); return (int) ((diff + limit - 1) / limit); }原创 2022-12-16 20:16:42 · 74 阅读 · 0 评论