## 1093 Count PAT's

The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.

Now given any string, you are supposed to tell the number of PAT's contained in the string.

Input Specification:
Each input file contains one test case. For each case, there is only one line giving a string of no more than 10
5
characters containing only P, A, or T.

Output Specification:
For each test case, print in one line the number of PAT's contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.

Sample Input:
APPAPT
Sample Output:
2

# 方法DP

dp0 表示i之前有多少个P
dp1 表示i之前有多少个组成的PA
dp2 表示i之前有多少个组成的PAT

dp1 = 0
dp2 = 0

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
string s;
getline(cin, s);
if(s.length() == 0) {
cout << 0 << endl;
return 0;
}

long long dp[3][s.length()];
for(int i = 0; i < 3; i++){
for(int j = 0; j < s.length(); j++){
dp[i][j] = 0;
}
}

if(s[0] == 'P'){
dp[0][0] = 1;
}

for(int i = 1; i < s.length(); i++){
dp[0][i] = dp[0][i-1];
dp[1][i] = dp[1][i-1];
dp[2][i] = dp[2][i-1];
if(s[i] == 'P'){
dp[0][i] += 1;
} else if(s[i] == 'A') {
dp[1][i] += dp[0][i];
} else if(s[i] == 'T'){
dp[2][i] += dp[1][i];
}
dp[0][i] = dp[0][i] % 1000000007;
dp[1][i] = dp[1][i] % 1000000007;
dp[2][i] = dp[2][i] % 1000000007;
}
cout << dp[2][s.length()-1];
return 0;
}

## 15. 三数之和-双指针

0 <= nums.length <= 3000
-105 <= nums[i] <= 105

# 双指针法

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
set<int> sets;
if(nums.size() <= 2){
return res;
}
sort(nums.begin(), nums.end());
for(int k = 2; k < nums.size(); k++){
if(k < nums.size() - 1 && nums[k] == nums[k+1]){
continue;
}
int i = 0, j = k-1;
while(i < j){
int v = nums[i] + nums[k] + nums[j];
if(v < 0){
i++;
} else if(v > 0){
j--;
} else {
vector<int> tmp = {nums[i], nums[k], nums[j]};
res.push_back(tmp);
do {
i++;
} while(i < j && nums[i] == nums[i-1]);
do {
j--;
} while(i < j && nums[j] == nums[j+1]);
}
}
}
return res;
}
};

## 33. 搜索旋转排序数组 - 二分

1 <= nums.length <= 5000
-10^4 <= nums[i] <= 10^4
nums 中的每个值都 独一无二

-10^4 <= target <= 10^4

# 方法1 单次二分

class Solution {
public:
int search(vector<int>& nums, int target) {
int start = 0;
int end = nums.size() - 1;
while(start <= end) {
int mid = (start + end) / 2;
if(nums[mid] == target){
return mid;
}
// 前半段顺序
if(nums[mid] >= nums[start]){
if(nums[mid] > target && target >= nums[start]){
end = mid - 1;
} else {
start = mid + 1;
}
} else {
if(target > nums[mid] && target <= nums[end]){
start = mid + 1;
} else {
end = mid - 1;
}
}
}
return -1;
}
};

# 方法2 二次二分

class Solution {
public:
int search(vector<int>& nums, int target) {
int k = findK(nums);
if(target >= nums[0] && target <= nums[k]){
return find(nums, 0, k, target);
} else {
return find(nums, k+1, nums.size()-1, target);
}
}

int find(vector<int>& nums, int start, int end, int target){
while(start <= end){
int mid = (start + end) / 2;
if(target < nums[mid]){
end = mid - 1;
} else if(target > nums[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}

int findK(vector<int>& nums){
int start = 0, end = nums.size() - 1;
while(start < end){
int mid = (start + end) / 2;
if(nums[start] < nums[mid]){
start = mid;
} else {
end = mid;
}
}
return start;
}
};

## 11. 盛最多水的容器 - 贪心

n == height.length
2 <= n <= 105
0 <= height[i] <= 104

# 解法1 宽度优先贪心

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> height = {1, 2, 1};
vector<int> height_b(height);
sort(height_b.begin(), height_b.end());
int mmax = 0;
for(int i = 0; i < height_b.size(); i++){
if(i != 0 && height_b[i] == height_b[i-1]){
continue;
}
int h = height_b[i];
int l = 0, r = 0;
for(int j = 0; j < height.size(); j++){
if(height[j] >= h) {
l = j;
break;
}
}
for(int j = height.size() - 1; j > l; j--){
if(height[j] >= h) {
r = j;
break;
}
}
if(r == l){
continue;
}
mmax = max(mmax, (r-l)*h);
}
cout << mmax;
return 0;
}


# 解法2 双指针法

class Solution {
public:
int maxArea(vector<int>& height) {
int mmax = 0;
int l = 0, r = height.size()-1;
while(l < r){
mmax = max(mmax, (r - l) * min(height[l], height[r]));
if(height[l] < height[r]){
l++;
} else {
r--;
}
}
return mmax;
}
};

# 题干

-231 <= x <= 231 - 1

# 数学推导

class Solution {
public:
int reverse(int x) {
int a = x;
int s = 0;
while(a){
int digit = a%10;
s += digit;
a = a/10;
if(a){
if(x > 0){
if(s > (INT_MAX - 1 - digit) / 10){
return 0;
}
} else {
if(s < (INT_MIN-digit)/10){
return 0;
}
}
s = s*10;
}
}
return s;
}
};

$$-2^{31} \le rev*10+digit \le 2^{31}-1$$

$$\left \lfloor \frac{-2^{31}-digit}{10} \right \rfloor \le rev \le \left \lfloor \frac{2^{31}-1-digit}{10} \right \rfloor$$