题目描述:

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 s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Explanation:
P   A   H   N
A P L S I I G
Y I R

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P     I    N
A L S I G
Y A H R
P I
 

My Solution(19ms,38.4MB)

首先想到的办法,有点绕,用一个二维数组按照之字形储存起来,然后逐行逐列地去读取,然后拼接成一个字符串。

class Solution {
public String convert(String s, int numRows) {
if(numRows<2){return s;}//防止除数为零
int index = 0,row=numRows-2;
//columns实际上是指二维数组列下表的最大值
int columns = s.length()/(2*numRows-2)*(numRows-1);
int remainder = s.length()%(2*numRows-2);
if(remainder>numRows){columns = remainder - numRows + columns;}
String[][] zigzag = new String[numRows][columns+1];
//开始逐行填充数组
for(int j = 0;j<columns+1;j++){
if(j%(numRows-1)==0){//竖笔
for(int i = 0;i<numRows;i++){
if(index<s.length()){
zigzag[i][j] = s.substring(index,index+1);index++;
row = numRows-2;
}
}
}else{
if(index<s.length()){
zigzag[row][j] = s.substring(index,index+1);index++;
row--;
}
}
}
//开始拼接结果字符串
String result = "";
for(int i =0 ; i<numRows ;i++){
for(int j=0;j<columns+1;j++){
if(zigzag[i][j]!=null){
result = result + zigzag[i][j];
}
}
}
return result;
}
}

Visit By Row(3ms,36MB)

发现数字间的规律,直接逐行去读取(效率最高)

class Solution {
public String convert(String s, int numRows) { if (numRows == 1) return s; StringBuilder ret = new StringBuilder();
int n = s.length();
int cycleLen = 2 * numRows - 2;
//逐行去读取
for (int i = 0; i < numRows; i++) {
for (int j = 0; j + i < n; j += cycleLen) {
ret.append(s.charAt(j + i));
//排除第一行和最后一行,因为两竖笔之间没有数字
if (i != 0 && i != numRows - 1 && j + cycleLen - i < n)
ret.append(s.charAt(j + cycleLen - i));
}
}
return ret.toString();
}
}

LeetCode第六题—— ZigZag Conversion(字符串的“之”字形转换)的更多相关文章

  1. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  2. leetcode第六题--ZigZag Conversion

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

  3. LeetCode 6. ZigZag Conversion & 字符串

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

  4. Leetcode 6 ZigZag Conversion 字符串处理

    题意:将字符串排成Z字形. PAHNAPLSIIGYIR 如果是5的话,是这样排的 P     I AP   YR H L G N  SI A    I 于是,少年少女们,自己去找规律吧 提示:每个Z ...

  5. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

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

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

  7. LeetCode(6) ZigZag Conversion

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

  8. LeetCode(6)ZigZag Conversion

    题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...

  9. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

随机推荐

  1. 净心诀---python3装饰器

    python3装饰器 装饰器作用 简单理解:可以为已有函数添加额外功能 例: 已有2个函数如下 def MyFunc1(): print("This is a print function1 ...

  2. 实体类Json串转成DataTable

    private DataTable GetJsonToDataTable(string json) { List<Object_DeclareInfo> arrayList = JsonC ...

  3. 2018-2-13-win10-uwp-音频

    title author date CreateTime categories win10 uwp 音频 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...

  4. idea 在新建一个class的时候可以选择继承的父类

    1.把光标放在父类名称上按alt+enter可以生成子类 2. 选中implement abstract class

  5. const 命令

    const 命令声明一个只读的常量,声明后值不可以改变 const 变量不可以重复声明 const一旦声明变量,就必须立即初始化,不能留到以后赋值. const命令声明的常量也是不提升,同样存在暂时性 ...

  6. SpringCloud+Eureka+Feign+Ribbon+zuul的简化搭建流程和CRUD练习

    环境:win10--idea2019--jdk8 1.搭建Eureka服务模块 1.1 新建eureka服务模块(Sping Initializr) 取名为eureka-server,并添加如下Dep ...

  7. leetcood学习笔记-107-二叉树的层次遍历二

    题目描述: 方法一: class Solution(object): def levelOrderBottom(self, root): """ :type root: ...

  8. AbstractQueuedSynchronizer 详解

    package java.util.concurrent.locks; 基本介绍 AbstractQueuedSynchronizer(队列同步器)可以看作是并发包(java.util.concurr ...

  9. sql语句创建表

    create table `search_custom_mall` ( `id` ) NOT NULL PRIMARY KEY AUTO_INCREMENT, `uid` ) NOT NULL, `n ...

  10. bzoj1029题解

    [解题思路] 贪心,先按结束时间排序,从左到右扫描过去,如果当前建筑可以修复则入大根堆,否则,若其修复时间比堆顶小则弹出堆顶并入堆,处理完堆后则更新总时间.复杂度O(nlog2n). [参考代码] # ...