【转载请注明】https://www.cnblogs.com/igoslly/p/9017638.html

来看一下题目:

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"

Example 2:

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

题目意思:

给出字符串和行数

设计出一种“竖-斜”式的显示形式

结果给出按横行读取的数值

下面的图可能能更好的解释图示法:

总的来说:

1、显示法

利用代码实现zigzag这样的变形,再按读取方向输出

class Solution {
public:
string convert(string s, int numRows) {
if(numRows==){return s;}
int length = s.size(),count=;
char zigzag[numRows][length]; // 设定行、列矩阵
fill(zigzag[],zigzag[]+numRows*length,'#'); // 初始化二维数组,首位置为zigzag[0]
enum{down,linear}; // 设定方向
int dir=down,x=,y=;
while(count<s.size()){
switch(dir){ // 判断方向
case down:
zigzag[x++][y]=s[count++];
// 当竖行越界后,已经到达末行+1,需要进行位置调整x-2,y+1
if(x>=numRows){dir=linear;x-=;y++;}
break;
case linear:
zigzag[x--][y++]=s[count++];
// 当斜行越界后,到达首行-1,需要进行位置调整x+2,y-1
if(x<){dir=down;x+=;y--;}
break;
}
}
string result;
for(int i=;i<numRows;i++){
for(int j=;j<length;j++){
if(zigzag[i][j]!='#'){
result+=zigzag[i][j];
}
}
}
return result;
}
};

Leetcode0006--ZigZag Conversion的更多相关文章

  1. 【leetcode❤python】 6. ZigZag Conversion

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

  2. 64. ZigZag Conversion

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

  3. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  4. leetcode第六题 ZigZag Conversion (java)

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

  5. leetcode题解 6.ZigZag Conversion

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

  6. 6.[leetcode] ZigZag Conversion

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

  7. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

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

  8. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  9. leetcode-algorithms-6 ZigZag Conversion

    leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...

  10. LeetCode 6. ZigZag Conversion & 字符串

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

随机推荐

  1. Parallelized coherent read and writeback transaction processing system for use in a packet switched cache coherent multiprocessor system

    A multiprocessor computer system is provided having a multiplicity of sub-systems and a main memory ...

  2. hdu_1205_吃糖果_201404021440

    吃糖果 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submis ...

  3. Ubuntu 16.04安装迅雷(兼容性不高)

    迅雷官方没有提供LInux的版本,但是提供了一个Xware的版本,这个是用来制作离线下载的,但是网上已经有人通过这个集成了桌面应用:但是没怎么测试过,稳定性不高. http://forum.ubunt ...

  4. Microsoft SQL Server Query Processor Internals and Architecture

    https://msdn.microsoft.com/en-us/library/aa226174(v=sql.70).aspx

  5. Android GIS开发系列-- 入门季(5) FeatureLayer加载本地shp文件与要素查询

    FeatureLayer是要素图层,也是Arcgis的主要图层.用这个图层可以加载本地的shp文件.下面我们看怎样加载shp文件到MapView中.查看ArcGis API可知FeatureLayer ...

  6. Android抽屉菜单DrawerLayout的实现案例

    (1)项目布局文件 activity_main.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://sc ...

  7. swift 2.0 语法 分支

    import UIKit // 注意: Swift中可以不写;号, 但是有一定的前提条件, 一行只有一句代码 //      如果一行有多句代码, 那么;还是必须写 // 注意: Swift变态的地方 ...

  8. unity3D游戏开发实战原创视频讲座系列11之相扑游戏开发并公布到Win\WP8

     解说文件夹 第一讲 游戏的演示和资源介绍 第二讲 场景的建设 第三讲 玩家的移动 第四讲 对手的AI(让对手动起来) 第五讲 游戏的管理(上) 第六讲 游戏的管理(下) 第七讲 公布到Win8系 ...

  9. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  10. CDOJ 1330 柱爷与远古法阵(高斯消元)

    CDOJ 1330 柱爷与远古法阵(高斯消元) 柱爷与远古法阵 Time Limit: 125/125MS (Java/Others)     Memory Limit: 240000/240000K ...