这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigzag的方式重排序到这4行的字符串里,什么意思呢? 看例子大概就懂了:

      m    e     e      o      i

      y  t  r  h  m  l  v  g  r

      s  s  i  t  o   t  e  y  l

      i      s     s      l

  第一列开始往下走,走到第四行就往上走(第二列),走回第一行就重新往下走,如此类推,最后的输出就是每一行的叠加,也就是:

      "meeoiytrhmlvgrssitoteylissl"

  思路其实很简单,就是用一个index来表示走到哪一行,用一个flag来表示往下还是往上走,当index>4和index < 0的时候,改变flag就可以了。代码如下:

  

public class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) return s;
String[] stringArray = new String[numRows];
//初始化字符串。
for (int i = 0; i < numRows; i++) {
stringArray[i] = "";
}
//index指向哪一行需要添加字母。
int index = 0;
//flag==1代表往下走(初始),flag==0代表往下走。
int flag = 1;
for (int i = 0; i < s.length(); i++) {
if (flag == 1) {
stringArray[index++] += s.charAt(i);
//当index越界时
if (index == numRows) {
//-2是因为边界在index-1的时候已经写过一遍了
index = index - 2;
flag = 0;
}
}
else {
stringArray[index--] += s.charAt(i);
//与上面同理
if (index < 0) {
index = index + 2;
flag = 1;
}
}
}
StringBuilder sb = new StringBuilder(stringArray[0]);
//把每一行加起来,选择用StringBuilder而不用String是因为StringBuilder要快一些
//为什么?读者可以查阅相关资料就能够了解string往后面添加字符串是一个什么样的机制。
for (int i = 1; i < numRows; i++) {
sb.append(stringArray[i]);
}
return sb.toString(); }
}

LeetCode(6) - ZigZag Conversion的更多相关文章

  1. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  2. 《LeetBook》leetcode题解(6): ZigZag Conversion[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. 【一天一道LeetCode】#6 ZigZag Conversion

    一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...

  4. 【LeetCode】6. ZigZag Conversion Z 字形变换

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...

  5. 【LeetCode】6 - ZigZag Conversion

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

  6. leetcode problem 6 ZigZag Conversion

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

  7. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

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

  8. 【LeetCode】006. ZigZag Conversion

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

  9. 【LeetCode】6. ZigZag Conversion 锯齿形转换

    题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...

随机推荐

  1. Oracle —— 表结构相关的SQL

    1.表基本信息(Table) select * from user_tables t, user_tab_comments c where c.table_name = t.table_name an ...

  2. Python 中 os.path模板

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  3. Html 修改placeholder的颜色属性css样式

    项目需求需要修改文本框的placeholder 的文本颜色, 百度下, 备忘,我使用的是这种方法, ::-webkit-input-placeholder { /* WebKit browsers * ...

  4. STL笔记(3) copy()之绝版应用

    STL笔记(3) copy()之绝版应用 我选用了一个稍稍复杂一点的例子,它的大致功能是:从标准输入设备(一般是键盘)读入一些整型数据,然后对它们进行排序,最终将结果输出到标准输出设备(一般是显示器屏 ...

  5. XML Schema使用技巧——unique

    XML Schema使用技巧——unique   XML Scheam允许指定某个元素或属性的值在一定得范围内是唯一的.为了指定元素或属性值的唯一性,可以使用<xs:unqiue>元素,使 ...

  6. HTML5中的localStorage用法

    存储数据的方法就是直接给window.localStorage添加一个属性,例如:window.localStorage.name 或者 window.localStorage["name& ...

  7. UVa 409 Excuses, Excuses!

    哈哈,虽然是一道字符串水题,可是拿到一个1A还是很开心的! 题意就是给一些keywords(子串)和Excuse(母串),然后输出包含keywords最多的Excuse,如果相等的话,按任意顺序全部输 ...

  8. hdu4638 group 树状数组

    连接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:就给给你n个数(大小在1-n里),然后给你连续的可以构成一个块,再给你N个询问,每个询问一个l ...

  9. iOS 8 AutoLayOut入门

    http://blog.csdn.net/asdfg13697116596/article/details/42562565 iOS 8 AutoLayOut入门自从iOS6带来Auto Layout ...

  10. OC 设计模式——单例模式

    单例模式的作用:可以保证在程序运行过程,一个类只有一个实例,而且这个实例易于供外界访问.永远只分配一次内存给这个类.由于在调用alloc方法的时候,都会调用allocWithZone,所以要重写这个方 ...