问题描述:

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

解题思路:

注意到zigzag的字符串按照读写的顺序是来回分配给不同行的,所以存在取模值的问题,对于正着往下读的字符串,对(nRows-1)取模值的结果刚好是它对应的行数,而对于往上读取的情况,对(nRows-1)取模的结果加上行号刚好等于(nRows-1)。所以考虑用boolean变量记录方向。对于每一行设立StringBuilder对象,适合向其末尾追加值。

代码如下:

public class Solution {
public String convert(String s, int nRows) {
StringBuilder[] stringBuilder = new StringBuilder[nRows];
for(int i = 0; i < nRows; i++)
stringBuilder[i] = new StringBuilder();
boolean reverse = true;
if(nRows == 1)
return s;
if (s.length() == 1)
return s;
for(int i = 0; i < s.length(); ++i){
if(i % (nRows - 1) == 0)
reverse = !reverse;
if(! reverse){
stringBuilder[i % (nRows - 1)].append(s.charAt(i));
}
else{
stringBuilder[nRows - 1 -(i % (nRows - 1))].append(s.charAt(i));
}
}
for(int i = 1; i < nRows; i++){
stringBuilder[0].append(stringBuilder[i]);
}
return stringBuilder[0].toString();
}
}

Java [leetcode 6] ZigZag Conversion的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. 【JAVA、C++】LeetCode 006 ZigZag Conversion

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

  4. leetcode 6. ZigZag Conversion [java]

    自己写的: if(numRows == 1) return s; int ll = s.length() / 2 + 1; Character tc[] = new Character[numRows ...

  5. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

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

  6. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  7. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  8. leetcode:ZigZag Conversion 曲线转换

    Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  9. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

随机推荐

  1. Codeforces Round #363 (Div. 2)->B. One Bomb

    B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  2. log4j记录exception异常有价值信息及log4j级别

    try{ }catch(Exception e){ e.printStackTrace(); log.error("配件导出excel错误:", e.fillInStackTrac ...

  3. Chp10: Scalability and Memory Limits

    The Step-by-Step Approach break down a tricky problem and to solve problems using what you do know. ...

  4. ElasticSearch小操之Marvel,Sense

    慢慢弄弄,说不好马上就要用呢,,, 嘿嘿 参考网址: http://es.xiaoleilu.com/ Elasticsearch 权威指南(中文版) 阅读地址:Elasticsearch权威指南(中 ...

  5. java使用正则表达式——实例

    Java代码   import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author        Der *  ...

  6. maven_Error building POM (may not be this project's POM)错误

    如果maven项目在执行编译等操作时报如题错误的话,请仔细检查pom.xml,一般是由pom的语法错误导致的,例如我的项目是因为: dependencies 元素下不应该有properties元素导致 ...

  7. Session过期,跳出iframe等框架

    //在你想控制跳转的页面,如login.jsp中的<head>与</head>之间加入以下代码:    if(window != top){        //解决Sessio ...

  8. PROPAGATION_REQUIRED事务管理

    转载:http://blog.csdn.net/l345480242/article/details/7588393 采用编程式事务 1. getCurrentSession()与openSessio ...

  9. 动态调整对话框属性(去掉标题栏,去掉边框,修改类似成Border:NONE样式)(调用ModifyStyle和ModifyStyleEx,然后调用SetWindowPos重新显示)

    // 动态修改对话框属性,去掉对话框标题栏,设置Border为NONE属性. if(dlg.GetSafeHwnd()) { dlg.ModifyStyle(WS_CAPTION, 0); // 去掉 ...

  10. 261. Graph Valid Tree

    题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...