题目:

思路:

以图为例: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. 剑指offer系列23---字符串排列(不是很理解)

    [题目]输入一个字符串,按字典序打印出该字符串中字符的所有排列. 例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 结果请按字母顺 ...

  2. Install and configure Intel NIC teaming on R420

    OS env: windows2008 R2 std 1. Download NIC driver from Dell Website http://www.dell.com/support/home ...

  3. .net下MD5算法和加盐

    MD5方法: public static string GetMD5(string sDataIn)        {            MD5CryptoServiceProvider md5 ...

  4. asp.net和脚本获取当前的URL、IP地址

    介绍一下ASP.NET取得当前页面的完整URL 的方放,icech做成了函数,直接用吧! private string GetPath() { string strPath = "http: ...

  5. working copy locked 问题

    解法 1 :  右键svn-->clean up 解法 2 :  被lock的文件夹进入控制台 del lock /q/s [转载解法] SVN 本地更新时,由于一些操作中断更新,如磁盘空间不够 ...

  6. BIP_开发案例06_以RB.RDF为数据源BIP.RTF为模板的简单例子(案例)

    2014-05-31 Created By BaoXinjian

  7. angular.js form

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. pythomn

    等我学号数据结构,明年就去找三胖 前端,写js相关代码.了解前端架构 而非页面设计 主要使用的是脚本语言 了解http web相关技术等 知道页面调优 浏览器加载方式等

  9. GridView--scroolview嵌套listview和gridview

    我们在真实项目中通常会遇到ListView或者GridView嵌套在ScrollView中问题.但是做的时候会发现,一旦两者进行嵌套,即会发生冲突.得不到我们希望的效果.由于ListView和Grid ...

  10. 微信支付开发若干问题总结,API搞死人(谢谢ζั͡ޓއއއ๓http://www.thinkphp.cn/code/1620.html)血淋淋的教训,第二次栽这里了

    近日,我研究了微信支付的API,我是用简化版的API,首先简述一下流程: 1.通过APP_ID,APP_SCRECT获取网页授权码code, 2.利用code获取用户openid/userinfo 3 ...