class Solution {
public:intmaxArea(vector<int>& height){int l =0, r = height.size()-1, ans =0;while(l < r){
ans =max(ans,(r - l)*(min (height[l], height[r])));if(height[l]< height[r]) l ++;else r --;}return ans;}};
12. 整数转罗马数字
class Solution {
public:
string intToRoman(int num){
map<int, string> mp;
mp[1]="I"; mp[5]="V";
mp[10]="X"; mp[50]="L";
mp[100]="C"; mp[500]="D";
mp[1000]="M"; mp[4]="IV";
mp[9]="IX"; mp[40]="XL";
mp[90]="XC"; mp[400]="CD";
mp[900]="CM";
string s;for(auto base :{1000,900,500,400,100,90,50,40,10,9,5,4,1}){while(num >= base){
s += mp[base];
num -= base;}}return s;}};
13. 罗马数字转整数
class Solution {
public:intromanToInt(string s){
map<int, string> mp;
mp[1]="I"; mp[5]="V";
mp[10]="X"; mp[50]="L";
mp[100]="C"; mp[500]="D";
mp[1000]="M"; mp[4]="IV";
mp[9]="IX"; mp[40]="XL";
mp[90]="XC"; mp[400]="CD";
mp[900]="CM";int ans =0;auto t = s.begin();auto check =[&](string s){if(s.size()==1){if(*t == s[0]){++ t;return true;}}if(s.size()==2){if(*t == s[0]&&*(t +1)== s[1]){++ t,++ t;return true;}}return false;};for(auto base :{1000,900,500,400,100,90,50,40,10,9,5,4,1}){while(check(mp[base]))
ans += base;}return ans;}};
14. 最长公共前缀
case1
class Solution {
public:
string longestCommonPrefix(vector<string>& strs){int ans =0, f =1;for(int l =1; f;++ l){for(int i =0; i < strs.size()&& f;++ i)if(strs[i].size()< l || strs[i][l -1]!= strs[0][l -1]) f =0;if(!f)break;
ans = l;}return strs[0].substr(0, ans);}};case2
class Solution {
public:
string longestCommonPrefix(vector<string>& strs){int base[4222][26]={0}, p =0, idx =0;int cnt[4222]={0}, ans =0;for(auto& i : strs){
p =0;int dep =0;for(auto& j : i){if(!base[p][j -'a']) base[p][j -'a']=++ idx;
p = base[p][j -'a'];
cnt[p]++; dep ++;if(cnt[p]== strs.size()) ans = max (ans, dep);}}return strs[0].substr(0, ans);}};
15. 三数之和
class Solution {
public:
vector<vector<int>>threeSum(vector<int>& n){
vector<vector<int>> ve;if(n.size()<3)return ve;sort(n.begin(), n.end());for(int i =0; i < n.size(); i ++){if(n[i]>0)break;if(i >0&& n[i]== n[i -1])continue;int l= i +1;int r= n.size()-1;while(l < r){int s = n[i]+ n[r]+ n[l];if(!s){
ve.push_back({n[i],n[l],n[r]});while(l < r && n[l]== n[l+1]) l ++;while(l < r && n[r]== n[r-1]) r --;
l ++;r --;}elseif(s <0) l ++;elseif(s >0) r --;}}return ve;}};
16. 最接近的三数之和
class Solution {
public:intthreeSumClosest(vector<int>& nums,int target){sort(nums.begin(), nums.end());int ans = nums[0]+ nums[1]+ nums[2];for(int i =0; i < nums.size();++ i){int l = i +1, r = nums.size()-1;while(l < r){int sum = nums[i]+ nums[l]+ nums[r];if(abs(sum - target)< abs (ans - target))
ans = sum;if(sum > target) r--;elseif(sum < target) l ++;elsereturn target;}}return ans;}};
17. 电话号码的字母组合
class Solution {
public:
vector<string> ans;
map<char, string> mp {{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl"},{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}};void dfs (string& digits,int cur, string val){if(digits.size()&& cur > digits.size()){
ans.push_back(val);return;}for(auto& i : mp[digits[cur -1]]){
val.push_back(i);
dfs (digits, cur +1, val);
val.pop_back();}}
vector<string>letterCombinations(string digits){
dfs (digits,1,"");return ans;}};
18. 四数之和
class Solution {
public:
vector<vector<int>>fourSum(vector<int>& nums,int target){
vector<vector<int>> ans;#define g numsif(nums.size()<4)return{};
sort (begin(nums),end(nums));for(int i =0; i < nums.size();++ i){if(i && g[i]== g[i -1])continue;for(int j = i +1; j < nums.size()-2;++ j){if(j != i +1&& g[j]== g[j -1])continue;long s =((long)g[i])+ g[j]+ g[j +1]+ g[j +2];if(s > target)continue;int l = j +1, r = nums.size()-1;while(l < r){long s =((long)g[i])+ g[j]+ g[l]+ g[r];if(s == target){
ans.push_back({g[i], g[j], g[l], g[r]});while(l < r && nums[l]== nums[l +1]) l ++;while(l < r && nums[r]== nums[r -1]) r --;
l ++, r --;}elseif(s > target) r --;elseif(s < target) l ++;}}}return ans;}};
19. 删除链表的倒数第 N 个结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode*removeNthFromEnd(ListNode* head,int n){
ListNode* h = new ListNode(0, head);
ListNode* p = h;
ListNode* t = head;for(int i =0; i < n;++ i) t = t->next;while(t){
p = p->next;
t = t->next;}
p->next = p->next->next;return h->next;}};