标签:draw container 长度 ret tor inline apple vector white
ZigZag Conversion:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N A P L S I I G Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
一个字符串被画成锯齿形按照给定的行数,像这样:(你可以认为字体大小一致)
P A H N
A P L S I I G
Y I R
然后你要按行读取它:"PAHNAPLSIIGYIR"
。。。?所以锯齿形是什么形?最后找到了解释:
Zigzag:即循环对角线结构(
| 0 | 8 | 16 | |||||||||
| 1 | 7 | 9 | 15 | 17 | |||||||
| 2 | 6 | 10 | 14 | 18 | |||||||
| 3 | 5 | 11 | 13 | 19 | |||||||
| 4 | 12 | 20 |
)
所以leetcode你不会找一个好点的样例么?
class Solution {
public:
string convert(string s, int numRows) {
int len=s.length();
queue<char> q[numRows];
int idx=0;
string ans=s;
while(idx<len)
{
for(int i=0;i<numRows;i++)
{
if(idx>=len) break;
q[i].push(s[idx]);
idx++;
}
for(int i=numRows-2;i>=1;i--)
{
if(idx>=len) break;
q[i].push(s[idx]);
idx++;
}
}
idx=0;
for(int i=0;i<numRows;i++)
{
while(!q[i].empty())
{
ans[idx]=q[i].front();
q[i].pop();
idx++;
}
}
return ans;
}
};
string ans要初始化才行,没初始化蜜汁re。
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个整数a1,a2..an,每个整数代表着一个坐标(i,a[1]),n个垂直的线段端点是(i,a[i])和(i,0)。
找两条线段,使得他们和x区域一起组成的容器装尽量多的水
木板盛水不是取决于最短的一条边吗,给定两个直线是i,j,答案不就是min(a[i],a[i+1],...a[j])*(j-i)吗
事实证明答案其实是min(a[i],a[j])*(j-i)。。
。。。?你这题目有歧义啊喂,with x-axis。。我怎么知道x-axis是不是包括其他线段啊
这样的话思路其实很简单。如果要先假设最短木板是谁,那势必要向左和向右查找,很复杂
于是不如就枚举木板的长度,让它在端点尽可能大的情况下尽可能的长。我们有一个贪心手段,就是每次剔除两端较小的一个端点
class Solution {
public:
int maxArea(vector<int>& height) {
int len=height.size();
int left=0,right=len-1;
int ans=0;
while(left<right)
{
int sum=(right-left)*min(height[left],height[right]);
if(sum>ans)
ans=sum;
if(height[left]<height[right])
left++;
else
right--;
}
return ans;
}
};
LeetCode.ZigZag Conversion ,Container With Most Water
标签:draw container 长度 ret tor inline apple vector white
原文地址:http://www.cnblogs.com/bitch1319453/p/6656797.html