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.
给出一行木板的高度,相邻木板距离为1,选两个木板使其体积最大class Solution
{
public:
int maxArea(vector<int>& height)
{
int l,r,h,ans;
l=0;
r=height.size()-1;
h=min(height[l],height[r]);
ans=h*(r-l);
while (l<r)
{
if (height[l]<height[r]) l++;
else r--;
h=min(height[l],height[r]);
ans=max(ans,h*(r-l));
}
return ans;
}
};
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)
{
string s;
string roman[4][10]=
{
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
s="";
int k=0;
while (num!=0)
{
s=roman[k++][num%10]+s;
num/=10;
}
return s;
}
};
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)
{
if (s.size()==0) return 0;
int ans=0,x,y;
x=check(s[0]);
ans=x;
for (int i=1;i<s.size();i++)
{
y=check(s[i]);
ans+=y;
if (x<y) ans-=2*x;
x=y;
}
return ans;
}
private:
int check(char ch)
{
if (ch=='I') return 1;
if (ch=='V') return 5;
if (ch=='X') return 10;
if (ch=='L') return 50;
if (ch=='C') return 100;
if (ch=='D') return 500;
if (ch=='M') return 1000;
}
};
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
class Solution
{
public:
string longestCommonPrefix(vector<string>& strs)
{
string ans;
int flag=0;
int k=0;
if (strs.size()==0) return "";
while (1)
{
flag=1;
char ch=strs[0][k];
for (int i=0;i<strs.size();i++)
{
if (strs[i].size()==k){flag=0; break;}
if (strs[i][k]!=ch){flag=0; break;}
}
if (flag==0) break;
k++;
}
ans=strs[0].substr(0,k);
return ans;
}
};
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:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- 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)在一串数中找出所有的A+B+C==0;
class Solution
{
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int> > ans;
vector<int> temp;
int j,k,i;
int flag=0;
if (nums.size()<=2) return ans;
sort(nums.begin(),nums.end());
for (i=0;i<nums.size()-2;i++)
if (i==0 || nums[i]!=nums[i-1])
{
flag=0;
j=i+1;
k=nums.size()-1;
while (j<k)
{
while (flag==1 && nums[j]==nums[j-1] && j<k) j++;
flag=0;
if (j>=k) break;
if (nums[i]+nums[j]==-nums[k])
{
temp.clear();
temp.push_back(nums[i]);
temp.push_back(nums[j]);
temp.push_back(nums[k]);
ans.push_back(temp);
flag=1;
j++;
}
else if (nums[i]+nums[j]+nums[k]<0)
j++;
else
k--;
}
}
return ans;
}
};