LC.P2600[K件物品的最大和]
LC.P2600[K件物品的最大和]
方法一:贪心1234567class Solution { public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) { if (numOnes >= k) return k; else if (numOnes + numZeros >= k) return numOnes; else return numOnes - (k - numOnes - numZeros); }}
时间复杂度:$O(1)$
空间复杂度:$O(1)$
LC.P65[有效数字]
LC.P65[有效数字]
方法一:模拟123456789101112131415161718192021222324252627282930313233343536373839404142class Solution { public boolean isNumber(String s) { char[] cs = s.toCharArray(); int index = -1, n = cs.length; for (int i = 0; i < n; ++i) { if (cs[i] == 'e' || cs[i] == 'E') { if (index == -1) index = i; else return false; } } boolean ans = true; if (in ...
LC.P2679[矩阵中的和]
LC.P2679[矩阵中的和]
方法一:暴力123456789101112131415161718192021222324class Solution { public int matrixSum(int[][] nums) { int ans = 0, m = nums.length, n = nums[0].length; int[] score = new int[m]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int maxIndex = 0; for (int k = 0; k < n; ++k) { if (nums[j][maxIndex] < nums[j][k]) { maxIndex = k; ...
LC.P393[UTF-8编码验证]
LC.P393[UTF-8编码验证]
方法一:模拟12345678910111213141516171819class Solution { public boolean validUtf8(int[] data) { int n = data.length; for (int i = 0; i < n; ) { int t = data[i], j = 7; while (j >= 0 && (((t >> j) & 1) == 1)) --j; int cnt = 7 - j; if (cnt == 1 || cnt > 4) return false; if (i + cnt - 1 >= n) return false; for (int k = i + 1; k < i + cnt; ++k) { ...
LC.P385[迷你语法分析器]
LC.P385[迷你语法分析器]
方法一:栈123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * public interface NestedInteger { * // Constructor initializes an empty nested list. * public NestedInteger(); * * // Constructor initializes a single integer. * public NestedInte ...
LC.P284[顶端迭代器]
LC.P284[顶端迭代器]
方法一:模拟12345678910111213141516171819202122232425262728293031// Java Iterator interface reference:// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.htmlclass PeekingIterator implements Iterator<Integer> { Iterator<Integer> iterator; Integer next; public PeekingIterator(Iterator<Integer> iterator) { // initialize any member here. this.iterator = iterator; if (iterator.hasNext()) next = iterator.next(); } ...
LC.P445[两数相加II]
LC.P445[两数相加II]
方法一:栈+迭代1234567891011121314151617181920212223242526272829303132333435/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Deque<Integer> s1 = new ...
LC.P400[第N位数字]
LC.P400[第N位数字]
方法一:模拟12345678910111213141516171819202122232425class Solution { // 基本规律: // [1, 9] 有9 * 1个数字 => 9 * 1 * 1 => 9 * 10^0 * 1 // [10, 99] 有90 * 2个数字 => 9 * 10 * 2 => 9 * 10^1 * 2 // [100, 999] 有900 * 3个数字 => 9 * 100 * 3 => 9 * 10^2 * 3 // [1000, 9999] 有9000 * 4个数字 => 9 * 1000 * 4 => 9 * 10^3 * 4 // => 9 * 10^(n-1) * n // ... public int findNthDigi ...
LC.P166[分数到小数]
LC.P166[分数到小数]
方法一:模拟1234567891011121314151617181920212223242526272829class Solution { public String fractionToDecimal(int numerator, int denominator) { long a = numerator, b = denominator; if (a % b == 0) return String.valueOf(a / b); StringBuilder builder = new StringBuilder(); // 其中一个数为负数,先加负号 if (a * b < 0) { builder.append('-'); a = Math.abs(a); b = Math.abs(b); } // 计算小数点前的 ...
LC.P2[两数相加]
LC.P2[两数相加]
方法一:递归123456789101112131415161718192021222324252627282930/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { return dfs(l1, l2, 0); } private Li ...