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

这道题就是看坐标的变化,找规律并分块处理。参考:爱做饭的小莹子

规律:第一行和最后一行,就是按照2n-2的顺序一点点加的。斜着那条线的字的位置是当前列j+(2n-2)-2i(i是行的index)。

Java:

public String convert(String s, int nRows) {
if(s == null || s.length()==0 || nRows <=0)
return "";
if(nRows == 1)
return s; StringBuilder res = new StringBuilder();
int size = 2*nRows-2;
for(int i=0;i<nRows;i++){
for(int j=i;j<s.length();j+=size){
res.append(s.charAt(j));
if(i != 0 && i != nRows - 1){//except the first row and the last row
int temp = j+size-2*i;
if(temp<s.length())
res.append(s.charAt(temp));
}
}
}
return res.toString();
}

Python:

class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
step, zigzag = 2 * numRows - 2, ""
for i in xrange(numRows):
for j in xrange(i, len(s), step):
zigzag += s[j]
if 0 < i < numRows - 1 and j + step - 2 * i < len(s):
zigzag += s[j + step - 2 * i]
return zigzag

C++:

class Solution {
public:
string convert(string s, int nRows) {
if (nRows <= 1) return s;
string res = "";
int size = 2 * nRows - 2;
for (int i = 0; i < nRows; ++i) {
for (int j = i; j < s.size(); j += size) {
res += s[j];
int tmp = j + size - 2 * i;
if (i != 0 && i != nRows - 1 && tmp < s.size()) res += s[tmp];
}
}
return res;
}
};

  

  

All LeetCode Questions List 题目汇总

  

[LeetCode] 6. ZigZag Converesion 之字型转换字符串的更多相关文章

  1. [LeetCode] ZigZag Converesion 之字型转换字符串

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

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

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

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

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

  4. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  5. leetcode 6 ZigZag Converesion

    class Solution { public: string convert(string s, int nRows) { if (nRows <= 1) return s; string r ...

  6. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  7. 算法:Z字型(Zigzag)编排

    问题:给定 n 行和 m 列的二维数组矩阵.如图所示,以 ZIG-ZAG 方式打印此矩阵. 从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形. 主要思想:算法从(0, 0) ...

  8. lintcode:Matrix Zigzag Traversal 矩阵的之字型遍历

    题目: 矩阵的之字型遍历 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历. 样例 对于如下矩阵: [ [1, 2, 3, 4], [5, 6, 7, 8], [9 ...

  9. LeetCode(6):Z字形转换

    Medium! 题目描述: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:(下面这样的形状) P A H N A P L S I I G Y I R 之后按 ...

随机推荐

  1. APP测试之MONKEY安装、使用

    1.先下载java的jdk;配置java变量 安装好之后会有两个文件夹一个是jdk 一个是jre(运行)然后配置好java环境变量:JAVA_HOME:C:\Program Files\Java\jd ...

  2. python测试开发django-rest-framework-61.权限认证(permission)

    前言 用户登录后,才有操作当前用户的权限,不能操作其它人的用户,这就是需要用到权限认证,要不然你登录自己的用户,去操作别人用户的相关数据,就很危险了. authentication是身份认证,判断当前 ...

  3. 使用js对社会信用代码进行正则验证

    注:参考了该博客(https://blog.csdn.net/qq_37142340/article/details/80695187)进行了一些修改,本文验证使用在微信小程序上. 直接贴代码: va ...

  4. drf框架(2)

    drf框架 """接口: 接口规范: drf的生命周期: 序列化组件: 三大认证:过滤,删选,排序组件 请求,响应,解析,异常 jwt:json web tooken & ...

  5. Linux 安装Anaconda 提示“bunzip2: command not found”

    问题: 安装Anaconda 过程中提示缺少“bunzip2” 解决思路: 由于缺少bunzip2 包,需要通过yum 方式安装bzip2 yum install -y bzip2 Linux bun ...

  6. Basic concepts of docker/kubernete/kata-container

    Kubereters An open-source system for automating deployment, scaling, and management of containerized ...

  7. 持续集成学习7 jenkins自动化代码构建

    一.整体功能 1.触发上下游构建 2.我们在触发一个job的时候顺便丢一些参数过去,这些参数有可能是我这次编译过程中产生的一些地址,版本号或动态的一些东西丢到下游作为下游的构建参数 3.不同种类的视图

  8. learning scala akka ask_pattern

    package com.example import akka.actor._ import akka.util.Timeout object Tutorial_03_Ask_Pattern exte ...

  9. Why We Changed YugaByte DB Licensing to 100% Open Source

    转自:https://blog.yugabyte.com/why-we-changed-yugabyte-db-licensing-to-100-open-source/ 主要说明了YugaByte ...

  10. vue transition实现页面切换效果

    我们都知道vue可以做成单页应用 点击的时候就能切换  如果我们要添加一些视觉效果 比如页面切换的时候有一个缓冲效果 这个时候就需要用到vue里的transition这个标签 在使用这个标签之前需要了 ...