6.[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: "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/
Analysis & Solution
There exits pattern in the ZigZag Conversion. The indexs at i row follows patterns:
Let DiffConstant = 2*row - 2
i = 0: the difference = DiffConstant
i = 1: the differences = 2(row-1) - 2, DiffConstant - (2(row-1) - 2).
i = 2: the differences = 2(row-2) - 2, DiffConstant - (2(row-2) - 2).
i = k: the differences = 2(row-k) - 2, DiffConstant - (2(row-k) - 2) = 2k
public String convert(String s, int numRows) {
if(numRows == 1){
return s;
}
int diffConstant = 2*numRows - 2, i, index;
StringBuffer res = new StringBuffer();
for(i = 0; i < numRows; i++){
index = i;
boolean isEven = true;
while(index < s.length()){
res.append(s.charAt(index));
if(i == 0 || i == numRows -1){
index += diffConstant;
}else{
if(isEven){
index += 2*(numRows-i) - 2;
}else{
index += 2*i;
}
isEven = !isEven;
}
}
}
return res.toString();
}
6.[leetcode] ZigZag Conversion的更多相关文章
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode: ZigZag Conversion 解题报告
ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...
- [leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...
- [LeetCode]ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] ZigZag Conversion [9]
称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- C++ leetcode::ZigZag Conversion
mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...
- Leetcode:ZigZag Conversion分析和实现
问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A G M B F H ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
随机推荐
- 分布式任务系统gearman的python实战
Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在调用其它语言的函数的系统.Gearman是一个分发任务的程序框架,可以 ...
- c/c++(hiredis)异步调用redis【转】
hiredis是redis官方推荐的C/C++客户端代码库.使用hiredis库很简易方便的进行redis的相关开发. 同步方式 不过大多数情况下,我们采用的都是同步的调用方式. 1 2 3 4 ...
- 超实用的JavaScript代码段 Item5 --图片滑动效果实现
先上图 鼠标滑过那张图,显示完整的哪张图,移除则复位: 简单的CSS加JS操作DOM实现: <!doctype html> <html> <head> <me ...
- 五个最佳RSS新闻阅读器
文章出自http://www.williamlong.info/archives/1591.html 在博客和在线新闻充斥的互联网上,大量信息已经使得用户阅读量过载,幸运的是,通过RSS Feed提供 ...
- ATM机
ATM 要求 示例代码: https://github.com/triaquae/py_training/tree/master/sample_code/day5-atm
- client_v2.go
} return false }
- BZOJ_3894_文理分科&&BZOJ_2127_happiness_最小割
BZOJ_3894_文理分科_最小割 Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进 ...
- 从壹开始 [ Nuxt.js ] 之一 || 为开源收录Bug之 TiBug项目 开篇讲
缘起 哈喽大家周二好呀,刚刚经历过了几天火车抢票,整个人都不好了,不知道小伙伴对今年的春节是否还一如既往的期待呢,眼看都要春节了,本来也想写篇2018总结篇,但是怕不免会出现鸡汤文的窠臼嫌疑,想想还是 ...
- Redis in .NET Core 入门:(5) Sorted SET
第1篇:https://www.cnblogs.com/cgzl/p/10294175.html 第2篇 String:https://www.cnblogs.com/cgzl/p/10297565. ...
- MySQL 复制 - 性能与扩展性的基石 3:常见问题及解决方案
主备复制过程中有很大可能会出现各种问题,接下来我们就讨论一些比较普遍的问题,以及当遇到这些问题时,如何解决或者预防问题发生. 1 数据损坏或丢失 问题描述:服务器崩溃.断电.磁盘损坏.内存或网络错误等 ...