11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
题意:给你n个点,选择两个点,求能装水的最大面积。假如选择a[i],a[j],面积为 (j-i) * min(a[i], a[j])
解析:有两个变量,咱可以控制一个变量,变化另外一个,从两边开始一直往里面缩,更新最大值就好了
代码:
class Solution {
public:
int maxArea(vector<int>& height) {
int ans = 0;
int l = 0, r = height.size() - 1;
while(l < r)
{
ans = max(ans, (r - l) * min(height[l], height[r]));
if(height[l] < height[r]) l++;
else r--;
}
return ans;
}
};
12
.
Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
题意:整数变成罗马数字,一直模拟就好了,很恶心
代码:
class Solution {
public:
string intToRoman(int num) {
map<int, string> mp;
mp[1] = "I"; mp[4] = "IV"; mp[5] = "V"; mp[9] = "IX";
mp[10] = "X"; mp[40] = "XL";mp[50] = "L"; mp[90] = "XC";
mp[100] = "C"; mp[400] = "CD"; mp[500] = "D"; mp[900] = "CM";
mp[1000] = "M";
string ans = "";
while(num >= 1000) num -= 1000, ans += mp[1000];
while(num >= 900) num -= 900, ans += mp[900];
while(num >= 500) num -= 500, ans += mp[500];
while(num >= 400) num -= 400, ans += mp[400];
while(num >= 100) num -= 100, ans += mp[100];
while(num >= 90) num -= 90, ans += mp[90];
while(num >= 50) num -= 50, ans += mp[50];
while(num >= 40) num -= 40, ans += mp[40];
while(num >= 10) num -= 10, ans += mp[10];
while(num >= 9) num -= 9, ans += mp[9];
while(num >= 5) num -= 5, ans += mp[5];
while(num >= 4) num -= 4, ans += mp[4];
while(num >= 1) num -= 1, ans += mp[1];
return ans;
}
};
13
.
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题意:罗马数字变成整数,同上题模拟哈~
代码:
class Solution {
public:
int romanToInt(string s) {
map<string, int> mp;
mp["I"] = 1; mp["IV"] = 4; mp["V"] = 5; mp["IX"] = 9;
mp["X"] = 10; mp["XL"] = 40; mp["L"] = 50; mp["XC"] = 90;
mp["C"] = 100; mp["CD"] = 400; mp["D"] = 500; mp["CM"] = 900;
mp["M"] = 1000;
int ans = 0;
int i = 0, len = s.length();
while(i < len)
{
while(i < len && s[i] == 'M') ans += 1000, i++;
if(i + 1 < len && s[i] == 'C' && s[i+1] == 'M') ans += 900, i += 2;
if(i < len && s[i] == 'D') ans += 500, i++;
if(i+1 < len && s[i] == 'C' && s[i+1] == 'D') ans += 400, i += 2;
while(i < len && s[i] == 'C') ans += 100, i++;
if(i + 1 < len && s[i] == 'X' && s[i+1] == 'C') ans += 90, i += 2;
if(i < len && s[i] == 'L') ans += 50, i++;
if(i+1 < len && s[i] == 'X' && s[i+1] == 'L') ans += 40, i += 2;
while(i < len && s[i] == 'X') ans += 10, i++;
if(i + 1 < len && s[i] == 'I' && s[i+1] == 'X') ans += 9, i += 2;
if(i < len && s[i] == 'V') ans += 5, i++;
if(i+1 < len && s[i] == 'I' && s[i+1] == 'V') ans += 4, i += 2;
while(i < len && s[i] == 'I') ans++, i++;
}
return ans;
}
};
14
.
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
题意:n个字符串,求最长的公共部分
代码:
//暴力比较下就可以了
class Solution {
public:
int get_pre(string &a, string &b, int len)
{
for(int i = 0; i < len; i++)
{
if(a[i] != b[i]) return i;
}
return len;
}
string longestCommonPrefix(vector<string>& strs) {
if(strs.size() == 0) return "";
int res = 0;
for(int i = 1; i < strs.size(); i++)
{
if(strs[res].length() > strs[i].length()) res = i;
}
int len = strs[res].length();
for(int i = 0; i < strs.size(); i++)
{
if(i == res) continue;
len = get_pre(strs[i], strs[res], len);
}
string ans = strs[res].substr(0, len);
return ans;
}
};
15
.
3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
题意:求三个数和为0的所有组合,不能重复哈~
解析:见代码;
代码1:
//四种情况,两负一正, 两正一负,三个0,一负一正一个0,map去下重
class Solution {
public:
vector<vector<int>> ans;
inline void work(vector<int> &s, int a, int b, int c){
s.clear();
s.push_back(a); s.push_back(b); s.push_back(c);
}
vector<vector<int>> threeSum(vector<int>& nums) {
vector<int> a, b, c, s;
sort(nums.begin(), nums.end());
unordered_map<int, int> mp;
for(int i = 0; i < nums.size(); i++)
{
if(nums[i] < 0) a.push_back(nums[i]);
else if(nums[i] == 0) b.push_back(nums[i]);
else c.push_back(nums[i]);
mp[nums[i]] = 1;
}
int len1 = a.size(), len2 = b.size(), len3 = c.size();
if(len2 >= 3)
{
vector<int> s; work(s, 0, 0, 0); ans.push_back(s);
}
unordered_map<long long, int> mp1;
long long now;
for(int i = 0; i < len1; i++)
{
now = a[i]*1000000;
if(!mp1[now] && mp[-a[i]] && len2)
{
work(s, a[i], 0, -a[i]);
mp1[now] = 1;
ans.push_back(s);
}
for(int j = i + 1; j < len1; j++)
{
int cur = a[i] + a[j];
now = a[i]*1000000+a[j];
if(mp[-cur] && !mp1[now])
{
work(s, a[i], a[j], -cur);
mp1[now] = 1;
ans.push_back(s);
}
}
}
for(int i = 0; i < len3; i++)
{
for(int j = i + 1; j < len3; j++)
{
int cur = c[i] + c[j];
now = c[i]*1000000+c[j];
if(mp[-cur] && !mp1[now])
{
work(s, -cur, c[i], c[j]);
mp1[now] = 1;
ans.push_back(s);
}
}
}
return ans;
}
};
代码2:
//法2:dml学长写的,枚举一个找另外两个和为target的数(leetcode第一题)
class Solution {
public:
vector<vector<int>> ans;
inline void work(vector<int> &s, int a, int b, int c) {
s.clear();
s.push_back(a); s.push_back(b); s.push_back(c);
}
void Two_sum(int l, int r, int target, vector<int> &nums) {
vector<int> s;
while(l < r)
{
if(nums[l] + nums[r] == -nums[target])
{
work(s, nums[target], nums[l], nums[r]);
ans.push_back(s);
while(l < r && nums[l+1] == nums[l]) l++;
while(l < r && nums[r-1] == nums[r]) r--;
l++;
r--;
}
else if(nums[l] + nums[r] < -nums[target]) l++;
else r--;
}
}
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
if(nums.size() < 3) return ans;
for(int i = 0; i < nums.size(); i++)
{
if(i > 0 && nums[i] == nums[i-1]) continue;
Two_sum(i+1, nums.size()-1, i, nums);
}
return ans;
}
};