class Solution {
public:
int maxArea(vector<int>& height) {
int res = -1;
int i = 0, j = height.size() - 1;
while(i < j)
{
res = max(res, (j - i) * min(height[i], height[j]));
if(height[i] < height[j]) i ++;
else j --;
}
return res;
}
};
class Solution {
public:
string intToRoman(int num) {
int vals[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
string romans[] = {"I", "IV", "V", "IX", "X","XL", "L", "XC", "C", "CD", "D", "CM", "M"};
string res;
for(int i = 12; ~i; i --)
while(num >= vals[i])
{
num -= vals[i];
res += romans[i];
}
return res;
}
};
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> m = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
int pos = 0, neg = 0;
for(int i = 0; i < s.size() - 1; i++)
if(m[s[i]] < m[s[i + 1]])
neg -= m[s[i]];
else
pos += m[s[i]];
pos += m[s.back()];
return pos + neg;
}
};
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.empty()) return "";
string res = "";
for(int i = 0; ; i ++)
{
bool flag = false;
string &first = strs[0];
for(auto &now : strs)
if(i >= first.size() || i >= now.size() || first[i] != now[i])
{
flag = true;
break;
}
if(flag) break;
res += first[i];
}
return res;
}
};
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int sum;
vector<vector<int>>res;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i ++)
{
if(i > 0 && nums[i] == nums[i - 1]) continue;
if(nums[i] > 0) break;
int l = i + 1, r = nums.size() - 1;
while(l < r){
sum = nums[l] + nums[r] + nums[i];
if(sum < 0) l ++;
else if(sum > 0) r--;
else{
res.push_back({nums[i], nums[l], nums[r]});
l ++, r --;
while(l < r && nums[l] == nums[l - 1]) l ++;
while(l < r && nums[r] == nums[r + 1]) r--;
}
}
}
return res;
}
};
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int closestNum = 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 threeNum = nums[i] + nums[l] + nums[r];
if(abs(threeNum - target) < abs(closestNum - target))
closestNum = threeNum;
if(threeNum > target)
r --;
else if(threeNum < target)
l ++;
else
return closestNum;
}
}
return closestNum;
}
};
class Solution {
public:
char ops[10][10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> res;
vector<string> letterCombinations(string digits) {
dfs(digits, 0, "");
return res;
}
void dfs(string digits, int u, string path)
{
if(u == digits.size())
{
if(path.size()) res.push_back(path);
return;
}
int v = digits[u] - '0';
for(int i = 0; ops[v][i]; i ++)
{
dfs(digits, u + 1, path + ops[v][i]);
}
}
};
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int sum;
vector<vector<int>> res;
if(nums.size() < 4) return res;
sort(nums.begin(), nums.end());
for(int a = 0; a < nums.size() - 3; a ++)
{
if(a > 0 && nums[a] == nums[a - 1]) continue;
for(int b = a + 1; b < nums.size() - 2; b ++)
{
if(b > a + 1 && nums[b] == nums[b - 1]) continue;
int l = b + 1, r = nums.size() - 1;
while(l < r)
{
sum = nums[a] + nums[b] + nums[l] + nums[r];
if(sum < target) l ++;
else if(sum > target) r--;
else{
res.push_back({nums[a], nums[b], nums[l], nums[r]});
l ++, r --;
while(l < r && nums[l] == nums[l - 1]) l ++;
while(l < r && nums[r] == nums[r + 1]) r --;
}
}
}
}
return res;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(-1);
dummy->next = head;
ListNode *a = dummy, *b = dummy;
for(int i = 0; i < n; i ++) a = a->next;
while(a->next)
{
a = a->next;
b = b->next;
}
b->next = b->next->next;
return dummy->next;
}
};
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for(auto c : s)
{
if(c == ')')
{
if(stk.empty() || stk.top() != '(')
return false;
stk.pop(); //匹配 弹出去
}
else if(c == ']')
{
if(stk.empty() || stk.top() != '[')
return false;
stk.pop();
}
else if(c == '}')
{
if(stk.empty() || stk.top() != '{')
return false;
stk.pop();
}
else stk.push(c);
}
return stk.empty();
}
};