题目:

思路:

以图为例:s={'A','B','C','D','E','F','G','H'.....}

1.先不考虑中间元素F、G、H、N...,每一行前后元素在数组中对应下标相差size=2*numRows-2(其中numRows是行数)

例如:第1行起始元素s[0]=A,在不大于数组长度情况下,size=2*5-2=8,该行往后依次是s[0+8]=s[8],s[0+8+8]=s[16].....

2.考虑加入中间元素,j表示当前元素所在数组的下标,i为该元素所在行(从0开始),则除第一行和最后一行外,每行前后元素间加的元素对应数 组s中下标为:j-i+(size-i)。j-i表示起始位置,size-i可理解为该行前后元素相差size,而i可看成这个size长度的倒数第i个位置

例如:第2行(即i=1)中:

若当前元素下标j=1,即s[1]=B,下一个元素为s[1+8]=s[9]=J,中间要加的元素s[1-1+8-1]=s[7]=H;

public class Solution {
public String convert(String s, int numRows) {
if(s==null||s.length()==0||numRows<=0){
return "";
}
if(numRows==1){
return s;
}
int size=2*numRows-2;
StringBuffer sb=new StringBuffer();
for(int i=0;i<numRows;i++){
for(int j=i;j<s.length();j+=size){
sb.append(s.charAt(j));
if(i!=0 && i!=numRows-1 && j-i+size-i<s.length()){//不是第一行和最后一行,考虑加入中间的元素
sb.append(s.charAt(j-i+size-i));
}
}
}
return sb.toString();
}
}

【LeetCode】6. ZigZag Conversion 锯齿形转换的更多相关文章

  1. LeetCode:二叉树的锯齿形层次遍历【103】

    LeetCode:二叉树的锯齿形层次遍历[103] 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 ...

  2. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  3. LeetCode 6. ZigZag Conversion & 字符串

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

  4. [LeetCode] 103. 二叉树的锯齿形层次遍历

    题目链接 : https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/ 题目描述: 给定一个二叉树,返回其节 ...

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

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

  6. Java实现 LeetCode 103 二叉树的锯齿形层次遍历

    103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...

  7. leetcode:ZigZag Conversion 曲线转换

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

  8. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

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

  9. [leetcode]6. ZigZag Conversion字符串Z形排列

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

随机推荐

  1. 【转】 远程到服务器安装visualSVN server,出现Service 'VisualSVN Server' failed to start的解决方法

    在帮助远程到服务器上安装visualSVN server的时候,出现Service 'VisualSVN Server' failed to start. 解决方法(先不要关闭安装弹出的错误窗口): ...

  2. (C#) Lock - 将对象上锁,互斥多个线程,使同步。

    C# Lock 原文:http://www.dotnetperls.com/lock Locking is essential in threaded programs. It restricts c ...

  3. Top 6 Programming Languages for Mobile App Development

    Mobile application development industry in the last five years have multiplied in leaps and bounds, ...

  4. DBA_Oracle DBA常用表汇总(概念)

    2014-06-20 Created By BaoXinjian

  5. linux命令(2):df 磁盘占用

    在这里先讲讲linux命令df的资料: df 命令: linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信 ...

  6. [物理学与PDEs]第1章 电动力学

    [物理学与PDEs]第1章第1节 引言 [物理学与PDEs]第1章第2节 预备知识 2.1 Coulomb 定律, 静电场的散度与旋度 [物理学与PDEs]第1章第2节 预备知识 2.2 Ampere ...

  7. Linux Deepin 2014安装Lenovo LJ2600D驱动

    双11老师给实验室抢了个Lenovo LJ2600D的USB接口打印机,并用旧的HP-1080MFP网络打印服务器连接上了.Windows下就不多说了,官方给的驱动就是针对它的,同学们都用得爽爆了,因 ...

  8. Android NDK 构建 以及一些错误

    首先下载NDK 然后在最外层的 local.properties文件夹中 写上  ndk的路径 ndk.dir=/Users/wyj/Desktop/androidStudioSdk/android- ...

  9. html table单双行颜色间隔(转载)

    直接上代码: <html> <head> <meta http-equiv="Content-Type" content="text/htm ...

  10. 用block响应button的点击事件

    1.继承UIButton : 2.在自己定义的button类中的方法 addTarget:(id)target action:(SEL)action forControlEvents:(UIContr ...