LeetCode.ZigZag Conversion ,Container With Most Water

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 a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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;
    }
};
时间: 2024-10-25 06:59:25

LeetCode.ZigZag Conversion ,Container With Most Water的相关文章

LeetCode——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: "PAHNAPLSII

LeetCode ZigZag Conversion(将字符串排成z字型)

1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 string a=""; 5 int len=s.length(); 6 if(len<=nRows||nRows==1) return s; //只有n个,不足一个周期||排成一行 7 int teams=len/(nRows*2-2); //teams个完整的周期 8 int rest=len%(nRows*2-2); //最后一个不足

LeetCode(11)题解: Container With Most Water

https://leetcode.com/problems/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)

【LeetCode】11. Container With Most Water 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937 Subject 出处:https://leetcode.com/problems/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

LeetCode ZigZag Conversion C++ 解题思路

一个难度为Easy的题,看了好多人的题解都没想明白,最后使劲想使劲想,才想的差不多..太弱了,要找不到工作了.. 题目描述: 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

LeetCode解题报告--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

【LeetCode】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 cont

LeetCode ZigZag Conversion 解题报告

对输入字符串,做蛇形变化,然后按行输出. https://oj.leetcode.com/problems/zigzag-conversion/ 例如:The string "PAYPALISHIRING"  的蛇形变化如下: P        A           H        N A   P   L    S     I     I   G Y         I            R 最后要求输出的就是:"PAHNAPLSIIGYIR" Write

LeetCode 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: "PAHNAPLSII