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".

字符串的弯曲转换。

要做的首先就是发现每一行的规律,能够发现,每一行有2 * nRows - 2个字符。把结果的列先增加到新的字符串中。再增加行。

    public String convert(String s, int nRows) {
if(nRows <= 1)
return s;
StringBuffer buf = new StringBuffer();
int period = 2 * nRows - 2;
for(int i=0;i<nRows;i++){
for(int j=i;j<s.length();j+=period){
buf.append(s.charAt(j));
if(i !=0 && i != nRows - 1 && j+period - 2*i <s.length())
buf.append(s.charAt(j+period - 2*i));
}
}
return buf.toString();
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode——ZigZag Conversion的更多相关文章

  1. 6.[leetcode] ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

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

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  3. LeetCode: ZigZag Conversion 解题报告

    ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...

  4. [leetcode]ZigZag Conversion @ Python

    原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...

  5. [LeetCode]ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  6. [LeetCode] ZigZag Conversion [9]

    称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. C++ leetcode::ZigZag Conversion

    mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...

  8. Leetcode:ZigZag Conversion分析和实现

    问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A          G      M B      F  H    ...

  9. 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 ...

随机推荐

  1. 华为C8816电信版ROOT过程

    华为C8816电信版ROOT方法, 网上的方法都不太靠谱.. 昨天弄了好久, 最终搞定.. 整理了一下.. 实用到的就方便多了. <方法不再啰嗦, 都有说明> 1. 获取手机解锁passw ...

  2. Cocos2d-x教程(28)-ttf 字体库的使用

    欢迎增加 Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/37650843 通常为 ...

  3. Verifying Checksum ... Bad Data CRC 错误解决

    1.问题描述:使用SAM9X25  内核版本是2.6.39  在启动内核时会出现Verifying Checksum ... Bad Data CRC 错误 2.解决办法: 查看原先uboot参数: ...

  4. CentOS 7 安装MySql Server 5.6

    1. 安装MySql Server 在/etc/yum.repos.d/目录下添加以下文件mysql-community.repo文件,内容如下: [mysql56-community] name=M ...

  5. POJ3050 Hopscotch 【DFS】

    Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2113   Accepted: 1514 Descrip ...

  6. WPF界面设计技巧(1)—不规则窗体图文指南

    原文:WPF界面设计技巧(1)-不规则窗体图文指南 初到园子,奉上第一篇入门级教程,请勿见笑. 以往WinForm编程中,实现不规则窗体是有一定难度的,更难的是不规则窗体的边缘抗锯齿及局部透明处理.而 ...

  7. Eclipse 快捷键整理

    Alt+/:代码提示Ctrl+/:注释/取消注释Ctrl+D:删除光标所在行Ctrl+K:将光标停留在变量上,按Ctrl+K键可以查找到下一个同样的变量Shift+Ctrl+K:和Ctrl+K查找的方 ...

  8. Java EE (11) - 影响性能的因素

    垂直层(Tier)影响性能的因素 资源层数据库性能通常考虑以下方面的优化(MySQL为例):--使用哪种存储引擎:MyISAM vs. InnoDB, MERGE, MEMORY, Federated ...

  9. Oracle练习

    --声明一个变量,并给它赋值 declare  v_bonus number(8); begin select id*6 into v_bonus from A where Id=5; DBMS_OU ...

  10. 【Android Training - UserInfo】记住登入用户的信息[Lesson 1 - 使用AccountManager来记住用户]

    Remembering Your User[记住你的用户] 每一个人都非常喜欢自己的名字能被人记住.当中最简单,最有效的使得你的app让人喜欢的方法是记住你的用户是谁,特别是当用户升级到一台新的设备或 ...