11-15
- 盛水最多的容器
从两边开始往中间缩小,较小的一条边移动 因为面积是由较小一边决定的(较大一边移动的话不可有比现在面积更大的了,因为高最高是较小边,宽还在缩小)
int maxArea(vector<int>& height) {
int len = height.size();
int max = 0;
int area = 0;
int left = 0, right = len - 1;
while (left < right) {
area = min(height[left], height[right]) * (right - left);
if (area > max) max = area;
if (height[left] > height[right]) right--;
else left++;
}
return max;
}
- 整数转罗马数字
1000 900 500 400
100 90 50 40
10 9 5 4
各判断一次
string intToRoman(int num) {
string ans;
int temp = num;
int cnt = temp / 1000;
temp = temp - cnt * 1000;
while (cnt--) ans.push_back('M');
if (temp >= 900) {
ans.push_back('C');
ans.push_back('M');
temp -= 900;
}
if (temp >= 500) {
ans.push_back('D');
temp -= 500;
}
if (temp >= 400) {
ans.push_back('C');
ans.push_back('D');
temp -= 400;
}
cnt = temp / 100;
temp = temp - cnt * 100;
while (cnt--)ans.push_back('C');
if (temp >= 90) {
ans.push_back('X');
ans.push_back('C');
temp -= 90;
}
if (temp >= 50) {
ans.push_back('L');
temp -= 50;
}
if (temp >= 40) {
ans.push_back('X');
ans.push_back('L');
temp -= 40;
}
cnt = temp / 10;
temp = temp - cnt * 10;
while (cnt--)ans.push_back('X');
if (temp >= 9) {
ans.push_back('I');
ans.push_back('X');
temp -= 9;
}
if (temp >= 5) {
ans.push_back('V');
temp -= 5;
}
if (temp >= 4) {
ans.push_back('I');
ans.push_back('V');
temp -= 4;
}
while (temp--) ans.push_back('I');
return ans;
}
- 罗马数字转整数
不写题解了
- 最长公共前缀
不写
- 三数之和

浙公网安备 33010602011771号