双指针
最长连续不重复子序列
https://www.acwing.com/problem/content/801/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream>
using namespace std;
const int N = 100010; int a[N], count[N]; int n, ans;
int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; }
for (int i = 0, j = 0; j < n; j++) { count[a[j]]++; while (count[a[j]] > 1) { count[a[i]]--; i++; } ans = max(ans, j - i + 1); }
cout << ans << endl;
return 0; }
|
按奇偶排序数组
https://leetcode.cn/problems/sort-array-by-parity-ii/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: vector<int> sortArrayByParityII(vector<int>& arr) { int even = 0; int odd = 1; int n = arr.size(); for (int i = 0; i < n && even < n && odd < n;) { if ((arr[n - 1] & 1) == 1) { swap(arr[n - 1], arr[odd]); odd += 2; } else { swap(arr[n - 1], arr[even]); even += 2; } } return arr; } };
|