1 Integer to Roman
Given an integer, convert it to a roman numeral.
根据罗马数字的定义规范,3999以内的整数可以由如下罗马数字组合:
"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"
.它们对应的整数大小为:
1000,900,500,400,100,90,50,40,10,9,5,4,1
.
string intToRoman(int num) {
vector<int> values{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
vector<string> numerals{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string result="";
for (int i = 0; i < values.size(); i++) {
while (num>= values[i]) {
num-= values[i];
result+=numerals[i];
}
}
return result;
}
2 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
该题可采用俩指针方法,开始时首指针和尾指针分别指向第一和最后一条垂直线,计算此时围成的容器体积,然后循环移动指针。
int maxArea(vector<int>& height) {
int len=height.size();
if(len<2) return 0;
int start=1,end=len;
int res=min(height[start-1],height[end-1])*(end-start);
while(start<end)
{
if(height[start-1]<height[end-1])
{
start++;
}
else
{
end--;
}
if(start<end)
res=max(res,min(height[start-1],height[end-1])*(end-start));
else
return res;
}
return res;
}
3 3Sum
Given an array
S
of
该题同样可以采用两指针方法,先将数组排序,先定位一个元素,该元素从数组的第一位开始遍历,将该元素的后一位作为首指针,数组的最后一位作为尾指针,当该元素与首指针和尾指针所指向的数之和为0时,将这三个数加入容器,小于0时首指针向后移动,大于0时尾指针向前移动。
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int len= nums.size();
vector<vector<int> > res;
vector<int> mid;
for(int i=0;i<len-2;i++)
{
if (nums[i]>0) return res;
if (i> 0 && nums[i] == nums[i - 1])
continue;//防止重复计算
int start= i+1, end = len-1;
while (start<end)
{
if ((nums[start] + nums[end] +nums[i])<0) start++;
else if ((nums[start] + nums[end] +nums[i])>0) end--;
else
{
mid.push_back(nums[i]);
mid.push_back(nums[start++]);
mid.push_back(nums[end--]);
res.push_back(mid);
mid.clear();
while (start<end&&nums[start] == nums[start-1])
start++;//防止重复计算
while (start<end&&nums[end] == nums[end+1])
end--;//防止重复计算
}
}
}
return res;
}
4 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
该题与上一题思想一样,只是将上一题三个数的和与target比较,如果相等则立刻返回,不等的时候循环比较得到最近的和。
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int len= nums.size();
int res=0,result=0;
int dist=INT_MAX;
for(int i=0;i<len-2;i++)
{
if (i> 0 && nums[i] == nums[i - 1]) continue;//防止重复计算
int start= i+1, end = len-1;
while (start<end)
{
res=nums[start] + nums[end] +nums[i];
if (res<target)
{
if(target-res<dist)
{
result=res;
dist=target-res;
}
start++;
while (start<end&&nums[start] == nums[start-1]) start++; //防止重复计算
}
else if (res>target)
{
if(res-target<dist)
{
result=res;
dist=res-target;
}
end--;
while(start<end&&nums[end] == nums[end+1]) end--; //防止重复计算
}
else
return res;
}
}
return result;
}