第一题:
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.
For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint..
译文:
给定的阵列Ñ正整数,且为正整数小号,找到的最小长度的连续子阵列,其总和≥ 小号。如果没有,返回0。
例如,给定的阵列[2,3,1,2,4,3]和s = 7,
所述子阵列[4,3]有问题的约束下的最小长度。
题解:
实现一:
int minSubArrayLen(int s, vector<int>& nums) {
int n = nums.size(), start = 0, sum = 0, minlen = INT_MAX;
for (int i = 0; i < n; i++) {
sum += nums[i];
while (sum >= s) {
minlen = min(minlen, i - start + 1);
sum -= nums[start++];
}
}
return minlen == INT_MAX ? 0 : minlen;
}
实现二:
int minSubArrayLen(int s, vector<int>& nums)
{
int l = 0;
int r = -1;
int sum = 0;
int res = nums.size() + 1;
while (l < nums.size())
{
if (r+1<nums.size()&&sum < s)
{
sum += nums[++r];
}
else
{
sum -= nums[l];
l++;
}
if (sum >= s)
{
res = min(res, r-l + 1);
}
}
if (res == nums.size() + 1)
{
return 0;
}
return res;
}
第二题:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
译文:
给定一个字符串,找到最长子串的长度,而不重复字符。
例子:
给定”abcabcbb”的答案是”abc”,长度是3。
给定”bbbbb”的答案是”b”,长度为1。
给定”pwwkew”的答案是”wke”,长度为3.请注意,答案必须是子字符串,”pwke”是子序列,而不是子字符串。
解法一:
int lengthOfLongestSubstring(string s) {
char ch[256] = {0};
int l = 0; int r = 0;
int size = s.size() + 1;
while (l < s.size())
{
if (ch[s[r]] == 0)//如果该元素没有被访问过
{
ch[s[r]]++;
r++;
}
else//已经被访问过
{
ch[s[l]]--;
l++;
}
if (r - l + 1 > size)
{
size = r - l + 1;
}
}
return size;
}
解法二:优化了解法一,在基础上加上了起始位置,直接跳转到对应位置
int lengthOfLongestSubstring(string s)
{
vector<int>dict(256, -1);
int res = 0;
int start = 0;
for (int i = 0; i < s.length(); i++)
{
if (dict[s[i]]>start)//如果字符串中某个字符已经出现了,则让起始位置改为上次出现的位置
{
start = dict[s[i]];
}
dict[s[i]] = i;
res = max(res,i-start);
}
return res;
}
第三题
Given a string s and a non-empty string p, find all the start indices
of p’s anagrams in s.Strings consists of lowercase English letters only and the length of
both strings s and p will not be larger than 20,100.The order of output does not matter.
Example 1:
Input: s: “cbaebabacd” p: “abc”
Output: [0, 6]
Explanation: The substring with start index = 0 is “cba”, which is an
anagram of “abc”. The substring with start index = 6 is “bac”, which
is an anagram of “abc”.
实现:
vector<int> findAnagrams(string s, string p)
{
vector<int> pv(256, 0), sv(256, 0), res;
if (s.size() < p.size())
{
return res;
}
for (int i = 0; i < p.size(); ++i)
{
++pv[p[i]];
++sv[s[i]];
}
if (pv == sv)
{
res.push_back(0);
}
for (int i = p.size(); i < s.size(); i++)
{
++sv[s[i]];
--sv[s[i - p.size()]];
if (sv == pv)
{
res.push_back(i-p.size()+1);//起始位置为--位置的下一个位置,故+1
}
}
return res;
}
第四题
Given a string S and a string T, find the minimum window in S which
will contain all the characters in T in complexity O(n).For example, S = “ADOBECODEBANC” T = “ABC” Minimum window is “BANC”.
Note: If there is no such window in S that covers all characters in T,
return the empty string “”.If there are multiple such windows, you are guaranteed that there will
always be only one unique minimum window in S.
译文:
给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符。
例如, S = “ADOBECODEBANC” T =”ABC” 最小窗口是”BANC”。
注意: 如果S中没有覆盖T中所有字符的窗口,则返回空字符串”“。
如果有多个这样的窗口,您可以确保在S中始终只有一个唯一的最小窗口。
string minWindow(string S, string T)
{
if (S.empty() || T.empty())
{
return "";
}
int count = T.size();
int require[128] = { 0 };
bool chSet[128] = {false};
for (int i = 0; i < count; i++)
{
require[T[i]]++;
chSet[T[i]] = true;
}
int i = -1;
int j = 0;
int minLen = INT_MAX;
int minIdx = 0;
while (i < (int)S.size() && j < S.size())
{
if (count)//count巧妙解决了遍历的问题
{
i++;
require[S[i]]++;
if (chSet[S[i]] && require[S[i]] >= 0)
{
count--;
}
}
else
{
if (minLen > i - j + 1)//窗口的区间为 i-j+1
{
minLen = i - j + 1;
minIdx = j;
}
require[S[j]]++;
if (chSet[S[j]] && require[S[j]] > 0)//只有T中的字符才会进来
{
count++;
}
j++;
}
}
if (minLen == INT_MAX)
{
return "";
}
return S.substr(minIdx,minLen);
}