滑动窗口
| int min(int a, int b) { |
| return a > b ? b : a; |
| } |
| |
| int minSubArrayLen(int target, int *nums, int numsSize) { |
| int res = 0x7fffffff; |
| for (int l = 0, r = 0, sum = 0; r < numsSize; r++) { |
| |
| sum += nums[r]; |
| |
| while (sum - nums[l] >= target) { |
| sum -= nums[l]; |
| l++; |
| } |
| |
| if (sum >= target) res = min(res, r - l + 1); |
| } |
| return res == 0x7fffffff ? 0 : res; |
| } |
| int max(int a, int b) { |
| return a > b ? a : b; |
| } |
| |
| int lengthOfLongestSubstring(char *s) { |
| int len = strlen(s); |
| if (len < 2) return len; |
| |
| |
| int *hashSet = (int *) calloc(128, sizeof(int)); |
| int left = 0, right = 0; |
| hashSet[s[right]] = 1; |
| int res = 1; |
| |
| while (right + 1 < len) { |
| |
| char nextChar = s[right + 1]; |
| if (hashSet[nextChar] == 0) { |
| |
| hashSet[nextChar] = 1; |
| right++; |
| |
| res = max(res, right - left + 1); |
| } else { |
| |
| while (left <= right && s[left] != nextChar) { |
| hashSet[s[left]] = 0; |
| left++; |
| } |
| |
| left++; |
| right++; |
| } |
| } |
| return res; |
| } |
| int max(int a, int b) { |
| return a > b ? a : b; |
| } |
| |
| int lengthOfLongestSubstring(char *s) { |
| int len = strlen(s); |
| int res = 0; |
| |
| int last[128]; |
| memset(last, -1, sizeof(int) * 128); |
| for (int l = 0, r = 0; r < len; r++) { |
| |
| l = max(l, last[s[r]] + 1); |
| |
| res = max(res, r - l + 1); |
| |
| last[s[r]] = r; |
| } |
| return res; |
| } |
| |
| char *minWindow(char *s, char *t) { |
| int lenS = strlen(s); |
| int lenT = strlen(t); |
| if (lenS < lenT) { |
| char *res = ""; |
| return res; |
| } |
| |
| int map[256] = {0}; |
| for (int i = 0; i < lenT; ++i) map[t[i]]--; |
| |
| int len = 0x7fffffff; |
| |
| int start = 0; |
| |
| int count = lenT; |
| for (int r = 0, l = 0; r < lenS; ++r) { |
| |
| if (map[s[r]] < 0) count--; |
| |
| map[s[r]]++; |
| |
| if (count == 0) { |
| |
| |
| while (map[s[l]] > 0) { |
| map[s[l]]--; |
| l++; |
| } |
| if (r - l + 1 < len) { |
| |
| len = r - l + 1; |
| |
| start = l; |
| } |
| } |
| } |
| if (len == 0x7fffffff) { |
| char *res = ""; |
| return res; |
| } else { |
| char *res = (char *) malloc(sizeof(char) * (len + 1)); |
| res[len] = '\0'; |
| for (int i = 0; i < len; ++i) res[i] = s[start++]; |
| return res; |
| } |
| } |
| |
| int canCompleteCircuit(int *gas, int gasSize, int *cost, int costSize) { |
| |
| for (int i = 0; i < gasSize; ++i) { |
| |
| int count = gasSize; |
| |
| int pos = i; |
| |
| int cur = 0; |
| while (count > 0) { |
| |
| cur += gas[pos]; |
| |
| if (cost[pos] > cur) break; |
| |
| cur -= cost[pos]; |
| pos = (pos + 1) % gasSize; |
| count--; |
| } |
| if (count == 0) return i; |
| } |
| return -1; |
| } |
| |
| int canCompleteCircuit(int *gas, int gasSize, int *cost, int costSize) { |
| int arr[gasSize]; |
| |
| for (int i = 0; i < gasSize; ++i) |
| arr[i] = gas[i] - cost[i]; |
| |
| for (int i = 0; i < gasSize; ++i) { |
| |
| int count = gasSize; |
| |
| int pos = i; |
| |
| int prefix = 0; |
| while (count > 0) { |
| |
| prefix += arr[pos]; |
| |
| if (prefix < 0) break; |
| |
| pos = (pos + 1) % gasSize; |
| count--; |
| } |
| if (count == 0) return i; |
| } |
| return -1; |
| } |
| |
| int canCompleteCircuit(int *gas, int gasSize, int *cost, int costSize) { |
| |
| for (int i = 0; i < gasSize; ++i) { |
| |
| int count = gasSize; |
| |
| int pos = i; |
| |
| int prefix = 0; |
| while (count > 0) { |
| |
| prefix += gas[pos] - cost[pos]; |
| |
| if (prefix < 0) break; |
| |
| pos = (pos + 1) % gasSize; |
| count--; |
| } |
| if (count == 0) return i; |
| } |
| return -1; |
| } |
| |
| int canCompleteCircuit(int *gas, int gasSize, int *cost, int costSize) { |
| |
| int prefixSum = 0; |
| |
| int len = 0; |
| |
| for (int left = 0, right; left < gasSize; ++left) { |
| while (prefixSum >= 0) { |
| if (len == gasSize) return left; |
| |
| right = (left + len) % gasSize; |
| |
| len++; |
| prefixSum += gas[right] - cost[right]; |
| } |
| |
| len--; |
| prefixSum -= gas[left] - cost[left]; |
| } |
| return -1; |
| } |
| |
| |
| int min(int a, int b) { |
| return a > b ? b : a; |
| } |
| |
| bool ok(int *counts, int len, int require) { |
| for (int i = 0; i < 4; ++i) { |
| |
| if (counts[i] > require) return false; |
| |
| len -= require - counts[i]; |
| } |
| |
| return len == 0; |
| } |
| |
| int balancedString(char *s) { |
| int lenS = strlen(s); |
| |
| int res = lenS; |
| |
| int require = lenS / 4; |
| |
| int arr[lenS]; |
| |
| int counts[4] = {0}; |
| for (int i = 0; i < lenS; ++i) { |
| arr[i] = s[i] == 'Q' ? 0 : (s[i] == 'W' ? 1 : (s[i] == 'E' ? 2 : 3)); |
| counts[arr[i]]++; |
| } |
| |
| |
| for (int l = 0, r = 0; l < lenS; ++l) { |
| while (!ok(counts, r - l, require) && r < lenS) { |
| |
| counts[arr[r]]--; |
| r++; |
| } |
| if (ok(counts, r - l, require)) |
| res = min(res, r - l); |
| |
| counts[arr[l]]++; |
| } |
| return res; |
| } |
| int counts[20001]; |
| |
| |
| int lower(int *nums, int numsSize, int k) { |
| memset(counts, 0, sizeof(int) * 20001); |
| int res = 0; |
| for (int l = 0, r = 0, types = 0; r < numsSize; r++) { |
| |
| |
| if (counts[nums[r]] == 0) types++; |
| |
| counts[nums[r]]++; |
| |
| |
| while (types > k) { |
| |
| counts[nums[l]]--; |
| |
| if (counts[nums[l]] == 0) types--; |
| l++; |
| } |
| res += r - l + 1; |
| } |
| return res; |
| } |
| |
| int subarraysWithKDistinct(int *nums, int numsSize, int k) { |
| return lower(nums, numsSize, k) - lower(nums, numsSize, k - 1); |
| } |
| int max(int a, int b) { |
| return a > b ? a : b; |
| } |
| |
| int longestSubstring(char *s, int k) { |
| int len = strlen(s); |
| int counts[256]; |
| int res = 0; |
| |
| for (int require = 1; require < 256; ++require) { |
| memset(counts, 0, sizeof(int) * 256); |
| |
| int types = 0; |
| |
| int satisfy = 0; |
| for (int l = 0, r = 0; r < len; r++) { |
| counts[s[r]]++; |
| |
| if (counts[s[r]] == 1) types++; |
| |
| if (counts[s[r]] == k) satisfy++; |
| |
| |
| while (types > require) { |
| if (counts[s[l]] == 1) types--; |
| if (counts[s[l]] == k) satisfy--; |
| |
| counts[s[l]]--; |
| l++; |
| } |
| |
| if (satisfy == require) res = max(res, r - l + 1); |
| } |
| } |
| return res; |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18012441
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步