当然,我们还需要记录有多少字符符合要求(出现次数不少于 k),当区间内所有字符都符合时更新答案。 class Solution { public int longestSubstring(String s, int k) { int ans = 0; int n = s.length(); char[] cs = s.toCharArray(); int[] cnt = new int[26]; for (int p = 1; p <= 26;...
class Solution { var result = 0 var countA = 0 var countB = 0 val distance = mutableListOf<Int>() fun twoCitySchedCost(costs: Array<IntArray>): Int { // 先让所有人都选择距离最近的城市 chooseShortestCity(costs) // 判断到A城市的人数和到B城市的人数相差的数量 if (countA =...
//采用备忘录的方式来存子问题的解以避免大量的重复计算 class Solution { int[] memo; public int climbStairs(int n) { memo = new int[n+1]; return calcWays(n); } private int calcWays( int n ){ if ( n == 1) return 1; if ( n == 2) return...
A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2]] public ListListInteger fourSum(int[] nums, int target) { ListListInteger ans = new ArrayList(); int n = nums.length, sum = 0; Arrays.sort(nums); for (int i = 0; i n - 3;) { for (...
classSolution{publicintminimumLength(String s){int n=s.length();char[]cs=s.toCharArray();int ans=n;for(int i=0,j=n-1;i<j;i++,j--){if(cs[i]!=cs[j])break;while(i+1<j&&cs[i+1]==cs[i])i++;while(j-1>i&&cs[j-1]==cs[j])j--;ans=Math.max(0,j-i-1);}return...
按照不同类别进行分类,参考自 CyC2018/CS-Notes、LeetCode 分类顺序表。 算法思想相关 01.双指针 #English TitleChinese TitleSolutionDifficulty 3 Longest Substring Without Repeating Characters 无重复字符的最长子串 Java Medium 11 Container With Most Water 盛最多水的容器 Java Medium 15 3Sum 三数之和 Java...
cs1707/leetcodemaster 1 Branch0 Tags Code Folders and filesLatest commit cs1707 [189. Rotate Array][] af61e82· Sep 23, 2016 History201 Commits 001-two-sum [1. Two Sum][Accepted] Sep 11, 2016 002-add-two-numbers [2. Add Two Numbers][Accepted] Sep 11, 2016...
class Solution { fun climbStairs(n: Int): Int { // 前面第二个数 var lastTwo = 0 // 前一个数 var lastOne = 1 repeat(n) { lastOne += lastTwo lastTwo = lastOne - lastTwo } return lastOne } } 对于每一个题目,不要仅仅满足于通过。最好是不断地优化代码,使得其时间复杂度和空间复...
class Solution { public int climbStairs(int n) { return calcWays(n); } priv...
//采用备忘录的方式来存子问题的解以避免大量的重复计算 class Solution { int[] memo; ...