Showing posts with label Two Pointers. Show all posts
Showing posts with label Two Pointers. Show all posts

Friday, May 15, 2015

209 Minimum Size Subarray Sum

来源:Leetcode

原帖:https://leetcode.com/problems/minimum-size-subarray-sum/

题目:
Given an array of n positive integers and a positive integer s, find the minimal length of a 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.

代码:
 class Solution {  
 public:  
   int minSubArrayLen(int s, vector<int>& nums) {  
     if (nums.empty()) return 0;  
     int res = INT_MAX, sum = 0;  
     int start = 0, end = 0;  
     while (end < nums.size()) {  
       sum += nums[end];  
       if (sum < s) {  
         end++; continue;  
       }  
       while (start <= end) {  
         if (sum - nums[start] >= s) {  
           sum -= nums[start++];  
         } else {  
           break;  
         }  
       }  
       res = min(res, end-start+1);  
       end++;  
     }  
     return sum < s ? 0 : res;  
   }  
 };  

Wednesday, May 13, 2015

159 Longest Substring with At Most Two Distinct Characters

来源:Leetcode

原帖:https://oj.leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/

题目:
Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
For example, Given s = “eceba”,
T is "ece" which its length is 3.

代码:
 class Solution {  
   int lengthOfLongestSubstringTwoDistinct(string s) {  
     int res = 0, count = 0;  
     vector<int> need(256, 0);  
     int i = 0, j = 0;  
     while (j < s.size()) {  
       if (need[s[j]] == 0) count++;  
       need[s[j]]++;  
       if (count <= 2) {  
         j++;   
         continue;  
       }  
       res = max(res, j - i);  
       while (i < j && count == 3) {  
         need[s[i]]--;  
         if (need[s[i]] == 0) count--;  
         i++;  
       }  
       j++;  
     }  
     res = max(res, j - i);  
     return res;  
   }  
 }  

Minimum Window Substring

来源:Leetcode

原帖:http://oj.leetcode.com/problems/minimum-window-substring/

题目:
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.
Solution: Use two pointers: start and end.
               First, move 'end'. After finding all the needed characters, move 'start'.
               Use array as hashtable.

代码:
 class Solution {  
 public:  
   string minWindow(string S, string T) {  
     int N = S.size(), M = T.size();  
     if (N < M) return "";  
     int need[256] = {0}, find[256] = {0};  
     for (int i = 0; i < M; ++i) need[T[i]]++;        
     int count = 0;   
     int resStart = -1, resEnd = N;  
     for (int start = 0, end = 0; end < N; ++end) {  
       if (need[S[end]] == 0) continue;    
       if (++find[S[end]] <= need[S[end]]) count++;  
       if (count != M) continue;       
       // move 'start'  
       for (; start < end; ++start) {  
         if (need[S[start]] == 0) continue;  
         if (find[S[start]] == need[S[start]]) break;  
         find[S[start]]--;  
       }        
       // update min window  
       if (end - start < resEnd - resStart) {  
         resStart = start;  
         resEnd = end;  
       }  
     }  
     return (resStart == -1) ? "" : S.substr(resStart, resEnd - resStart + 1);  
   }  
 };  

Longest Substring Without Repeating Characters

来源:Leetcode

原帖:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/

题目:
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
Solution: Pay attention when moving the 'start' pointer forward.

代码:
 class Solution {  
 public:  
   int lengthOfLongestSubstring(string s) {  
     int res = 0;  
     int start = 0, end = 0;  
     int N = s.size();  
     vector<bool> found(256, false);  
     while (end < N) { // slinding window (2 pointers)  
       if (!found[s[end]]) {  
         found[s[end++]] = true;  
         continue;  
       }  
       res = max(end - start, res);  
       while (found[s[end]]) {  
         found[s[start++]] = false;  
       }  
     }  
     res = max(res, end - start);  
     return res;  
   }  
 };