Leetcode0006--ZigZag Conversion
【转载请注明】https://www.cnblogs.com/igoslly/p/9017638.html
来看一下题目:
|
The string P A H N And then read line by line: 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 Example 2: Input: s = "PAYPALISHIRING", numRows = 4 |
题目意思: 给出字符串和行数 设计出一种“竖-斜”式的显示形式 结果给出按横行读取的数值 |
下面的图可能能更好的解释图示法:

总的来说:
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的更多相关文章
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
随机推荐
- hdu 1269 求连通图的模板题
#include<stdio.h> #include<string.h> #include<iostream>//只存在一个连通分量 #include<str ...
- 利用async和await异步操作解决node.js里面fs模块异步读写,同步结果的问题
async await 解决异步问题,这两个关键字是es7提出的,所以测试,node和浏览器版本提高一些 async await 操作基于promise实现的 async await这两个关键字是一起 ...
- codevs1197 Vigenère密码
题目描述 Description 16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère 密码的加密解密算法简单易用,且破译难度比 ...
- JAVA虚拟机运行时内存划分--运行时数据区域
Java虚拟机在执行java程序时会把内存划分为以下几个不同的数据区域: java虚拟机内存划分(运行时)1.线程私有的: 程序计数器(Program Counter Register):可以看作当前 ...
- JAVA NIO 之Channel
缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.Channel 通道就是将数据传输给 ByteBuffer 对象或者从 ByteBuffer 对象获取数据进行传输. Channel 用于在 ...
- 洛谷——P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 题目描述 Farmer John is on a boat seeking fabled treasur ...
- LVS中文站点
http://blog.csdn.net/turkeyzhou/article/details/16980161 http://zh.linuxvirtualserver.org/
- 万能存储工具类SDCard存储 /data/data/存储 assets存储 raw存储
万能存储工具类 SDCard存储 /data/data/存储 assets存储 raw存储 粘贴过去就能够用了 <uses-permission android:name="and ...
- Python实例--C#执行Python脚本,传参
# -*- coding: utf-8 -*- # 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错) # 这个 Python 脚本, 用于被 C# 来调用. # 简单测试 He ...
- 【剑指offer】合并两有序单链表
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25739727 九度OJ上AC,採用归并的思想递归实现. 题目描写叙述: 输入两个单调递增的 ...