leetcode 6 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”.
解决方案: The idea is, the first row and last row has no offset. Each element has a fixed difference of 2(nRows-1); For the rows in between, there is a incremental offset of 2;
0 6 12 -> distance = 2(nRows-1) = 6 offset = 0 1 5 7 11 -> offset = distance - 2 = 4 2 4 8 10 -> offset = distance -2 -2 = 2 3 9 -> distance = 2(nRows-1) = 6 offset = 0
Easy to observe. There is a catch, that you need to add the offset element with previous regular element. 5 follows 1, 4 follows 2. Otherwise, you will miss the tail if there is no vertical column in the end.Looks like a CS homework:)
class Solution {
public:
string convert(string s, int nRows) {
if(s.length() == 0 ||
s.length()/nRows < 1 ||
nRows == 1)
{
return s;
}
int distance = 2*(nRows-1);
string result;
int offset = 0;
for (int row = 0; row < nRows; row++)
{
for (int index = row; index < s.length(); index += distance)
{
result+=s[index];
if (offset != 0 && index + distance - offset < s.length())
{
result+=s[index + distance - offset];
}
}
offset += 2;
offset = offset % distance;
}
return result;
}
};
解决方案2:
The problem statement itself is unclear for many. Especially for 2-row case. “ABCD”, 2 –> “ACBD”. The confusion most likely is from the character placement. I would like to extend it a little bit to make ZigZag easy understood.
The example can be written as follow: 1.P…….A……..H…….N 2…A..P….L..S….I…I….G 3…..Y………I……..R
Therefore,
class Solution {
public:
string convert(string s, int numRows)
{
if (numRows <= 1)
return s;
const int len = (int)s.length();
string *str = new string[numRows];
int row = 0, step = 1;
for (int i = 0; i < len; ++i)
{
str[row].push_back(s[i]);
if (row == 0)
step = 1;
else if (row == numRows - 1)
step = -1;
row += step;
}
s.clear();
for (int j = 0; j < numRows; ++j)
{
s.append(str[j]);
}
delete[] str;
return s;
}
};
python解决方案: The idea is to use the remainder (index%period) to determine which line the character at the given index will be. The period is calculated first based on nRows. A dictionary with remainder:line as key:value is then created (this can also be done with a list or a tuple). Once these are done, we simply go through s, assign each character to its new line, and then combine these lines to get the converted string.
The code may be further shortened by using dict comprehension:
d={i:i if i
def convert(self, s, nRows):
if nRows==1:
return s
period= 2*(nRows -1)
lines=["" for i in range(nRows)]
d={} # dict remainder:line
for i in xrange(period):
if i<nRows:
d[i]=i
else:
d[i]=period-i
for i in xrange(len(s)):
lines[ d[i%period] ] +=s[i]
return "".join(lines)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 使用Azure Functions玩转Serverless
- Best practice on when to use the wrapper class and primitive type in Java
- Flutter基础widgets教程-BottomNavigationBar篇
- 直播系统介绍
- Azure内容审查器之羞羞图审查
- Clickhouse简介和性能对比
- Clickhouse创建分布式表以及表引擎介绍
- Azure 内容审查器之文本审查
- Redis中String数据类型原理实现
- Clickhouse分布式集群搭建
- Redis过期策略以及淘汰机制
- 几行代码就可以轻松给你的程序加上进度条
- git禁止在master分支push和commit
- 记录一次mybatis缓存和事务传播行为导致ut挂的排查过程
- appium教程_3.启动appium-server