006 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".
详见:https://leetcode.com/problems/zigzag-conversion/description/
解题思路:
1. 每一组 V 字形的长度为 size = 2 * line - 2 (斜向上的部分没有第一行和最后一样的元素),line 是行数
2. 对于垂直向下的一列元素来说,每一组向下的列之间间隔 size 大小,即一组元素的个数。
3. 对于斜向上的元素来说,它们的位置位于当前组 size - i(i 为该元素所在的行数,一组有 size 个字符,倒数第 i 个,位置为 size -i)。当前组的第一个字符所在位置为 j - i (j 为与斜向上的元素同在一行的,垂直向下的列的元素在字符串中的序号,i 为它们共同的行号)。
即:j-i就是zigzag的起始字符,然后我们是要倒数第i个,因为一个zigzag有size个字符,所以倒数第i个就是size-i,我们要赋值的字符就是(j-i)+(size-i)=j+size-2*i
class Solution {
public:
string convert(string s, int numRows) {
if(s.empty()||numRows==1)
{
return s;
}
string res="";
int len=s.size();
int size=numRows*2-2;
for(int i=0;i<numRows;++i)
{
for(int j=i;j<len;j+=size)
{
res+=s[j];
if(i!=0&&i!=numRows-1&&j+size-2*i<len)
{
res+=s[j+size-2*i];
}
}
}
return res;
}
};
006 ZigZag Conversion的更多相关文章
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [Leetcode]006. ZigZag Conversion
public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
随机推荐
- 恢复到特定点(时间点、scn、日志序列号),rman不完全恢复
将数据库.表空间.数据文件等恢复至恢复备份集保存时间中的任何一个时间点/SCN/日志序列(一般是日志挖掘找到误操作点),但须谨慎,操作前一定需要做好备份,具备条件的情况下最好先恢复到异机,避免业务停机 ...
- windows拾遗
Files has invalid value "<<<<<<< .mine". Illegal characters in path.在 ...
- 洛谷【P1009】阶乘之和
题目传送门:https://www.luogu.org/problemnew/show/P1009 高精度加法:https://www.cnblogs.com/AKMer/p/9722610.html ...
- selenium webdriver frame操作,跳进跳出
如果有两个平级的frame,跳进一个以后操作完成再操作第二个,这种情况要先跳出来,再跳进另外一个frame 跳出语句:browser.switch_to_default_content() #codi ...
- Dubbo注册中心的四种配置方式详解
Dubbo目前支持4种注册中心,(multicast,zookeeper,redis,simple) 推荐使用Zookeeper注册中心. 一.Multicast注册中心 不需要启动任何中心节点,只要 ...
- C# 将html实体编码转换到正常字符 & #40;格式
获取到html实体编码字符后,通过正则获取其中的html实体编码,再统一强制转换到正常字符: 代码如下: string strformat = item.value7; //将html实体编码转换到正 ...
- python 字典 get 小例子
语法 get()方法语法: dict.get(key, default=None) 参数 key -- 字典中要查找的键. default -- 如果指定键的值不存在时,返回该默认值值. 返回值 返回 ...
- python fabric的安装与使用
背景:fabric主要执行远程的shell命令,包括文件的上传.下载,以及提示用户输入等辅助其它功能. 测试系统:ubuntu16 要求:python //系统有自带,ubuntu16 通常自带pyt ...
- ctime、atime、mtime时间
区分一个文件或者目录的更改时间(change time,ctime),访问时间(access time,atime)以及修改时间(modify time,mtime)很重要. ctime——在Unix ...
- p1098 逆序对
传送门 题目 输入格式: 第一行,一个数n,表示序列中有n个数. 第二行n个数,表示给定的序列. 输出格式: 给定序列中逆序对的数目. 数据范围: 对于50%的数据,n≤2500 对于100%的数据, ...