【LeetCode】双指针 two_pointers(共47题)

【3】Longest Substring Without Repeating Characters 

【11】Container With Most Water 

【15】3Sum 

【16】3Sum Closest 

【18】4Sum 

【19】Remove Nth Node From End of List 

【26】Remove Duplicates from Sorted Array 

【27】Remove Element 

【28】Implement strStr() 

【30】Substring with Concatenation of All Words 

【42】Trapping Rain Water 

【61】Rotate List 

【75】Sort Colors 

【76】Minimum Window Substring 

【80】Remove Duplicates from Sorted Array II 

【86】Partition List 

【88】Merge Sorted Array 

【125】Valid Palindrome 

【141】Linked List Cycle 

【142】Linked List Cycle II 

【159】Longest Substring with At Most Two Distinct Characters 

【167】Two Sum II - Input array is sorted 

【209】Minimum Size Subarray Sum 

【234】Palindrome Linked List 

【259】3Sum Smaller 

【283】Move Zeroes 

【287】Find the Duplicate Number 

【344】Reverse String (2018年12月3日,第一次review,ko)

逆序一个字符串。

题解:见string分类:https://www.cnblogs.com/zhangwanying/p/9885334.html

【345】Reverse Vowels of a String 

【349】Intersection of Two Arrays (2018年11月6日,算法群相关题)

hash-table 里面有这题,我就不重复写了。hash-table:https://www.cnblogs.com/zhangwanying/p/9886262.html

【350】Intersection of Two Arrays II (2018年11月6日,算法群)

hash-table 里面有这题,我就不重复写了。hash-table:https://www.cnblogs.com/zhangwanying/p/9886262.html

【360】Sort Transformed Array 

【487】Max Consecutive Ones II (2018年11月27日)

给了一个0/1数组,问如果最多只能把一个 0 变成 1 的话,那么这个数组里面最长的连续 1 有几个?

题解:我是对于每一个 0 都求出了它前面连续 1 的个数 和后面连续 1 的个数。然后再遍历一边原数组,求长度,比较。注意如果全 1 的情况需要考虑。

discuss里面有更好的解法,不用多两个数组。

 1 class Solution {
 2 public:
 3     int findMaxConsecutiveOnes(vector<int>& nums) {
 4         const int n = nums.size();
 5         vector<int> pre(n, -1), next(n, -1);
 6         int cnt = 0;
 7         for (int i = 0; i < n; ++i) {
 8             if (nums[i] == 0) {
 9                 pre[i] = cnt;
10                 cnt = 0;
11             } else {
12                 cnt++;
13             }
14         }
15         cnt = 0;
16         for (int i = n-1; i >= 0; --i) {
17             if (nums[i] == 0) {
18                 next[i] = cnt;
19                 cnt = 0;
20             } else {
21                 cnt++;
22             }
23         }
24         int ret = 0;
25         cnt = 0;
26         for (int i = 0; i < n; ++i) {
27             if (nums[i] == 0) {
28                 ret = max(ret, pre[i] + next[i] + 1);
29                 cnt = 0;
30             } else {
31                 cnt++;
32                 ret = max(cnt, ret);
33             }
34         }
35         return ret;
36     }
37 };
View Code

follow-up:What if the input numbers come in one by one as an infinite stream? In other words, you can't store all numbers coming from the stream as it's too large to hold in memory. Could you solve it efficiently?

【524】Longest Word in Dictionary through Deleting 

【532】K-diff Pairs in an Array 

【567】Permutation in String 

【632】Smallest Range 

【713】Subarray Product Less Than K 

【723】Candy Crush 

【763】Partition Labels 

【826】Most Profit Assigning Work 

【828】Unique Letter String 

【838】Push Dominoes 

【844】Backspace String Compare 

【845】Longest Mountain in Array 

【881】Boats to Save People 

猜你喜欢

转载自www.cnblogs.com/zhangwanying/p/9886712.html