【转载请注明】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. nyoj 19 擅长排列的小明(深搜,next_permutation)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  2. 常州模拟赛d7t2 数组

    题目背景 HJZ 有很多玩具.他最喜欢玩的玩具是一个可以变化的数组. 题目描述 HJZ 的数组初始时有 n 个元素.他可以把一个位置上的数加上或减去一个固定的 数 x. 一天 LJZ 和 HZY 来 ...

  3. 消息传递(cogs 1001)

    问题描述WZland开办了一个俱乐部(这里面可以干任何的事情),这引来了许多的人来加入.俱乐部的人数越来越多,关系也越来越复杂……俱乐部的人来自各个地方,为了增加友谊,俱乐部举行了一次晚会.晚会上又进 ...

  4. Ubuntu 16.04下FireFox安装Flash插件

    下载: https://get.adobe.com/flashplayer/ 选择tar.gz包 解压 sudo tar zxvf flash_player_npapi_linux.x86_64.ta ...

  5. RMAN RECOVERY

    Data Recovery Advisor The health monitor and the ADR The capabilities and limitations of DRA using t ...

  6. ViewPager中View的复用

    代码例如以下: public class MyViewPagerAdapter extends PagerAdapter { //显示的数据 private List<DataBean> ...

  7. UVA 10385 - Duathlon(三分法)

    UVA 10385 - Duathlon 题目链接 题意:一些运动员,參加铁人两项,跑步r千米,骑车k千米,如今知道每一个人的跑步和骑车速度,问是否能设置一个r和k,保持r + k = t,使得第n个 ...

  8. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  9. 记录 mysql sql limit 0,100问题

    某个场景分页查询出第一页的数据,, limit 0,100  第一页 limit 100,100 第二页 limit 200,100 第三页 select * from user limit 0,10 ...

  10. 第14章4节《MonkeyRunner源代码剖析》 HierarchyViewer实现原理-装备ViewServer-port转发

    在初始化HierarchyViewer的实例过程中,HierarchyViewer会调用自己的成员方法setupViewServer来把ViewServer装备好,那么我们这里先看下这种方法: 39 ...