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 ...
随机推荐
- CodeForces 1000F One Occurrence
You are given an array $a$ consisting of $n$ integers, and $q$ queries to it. $i$-th query is denote ...
- [luoguP1130] 红牌(DP)
传送门 幼儿园DP. ——代码 #include <cstdio> #include <iostream> ; << ); int a[MAXN][MAXN], f ...
- 页面加载即执行JQuery的三种方法
[1]$(function( ){ }): $(function(){ $("#name").click(function(){ //adding your code here } ...
- kendo grid 报错:length
其实是:events中的{}Onsave的问题,把events整个注释掉就好了
- UnicodeEncodeError: 'gbk' codec can't encode character '\u2022' in position
在GBK解码时忽略掉不能解码的数据 self.file.write(content.encode("gbk", 'ignore').decode("gbk", ...
- srvctl error
Hit an strange error just now. [oracle@racnode1 ~]$ srvctl # # An unexpected error has been detected ...
- mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ 今天在linux中安装了mys ...
- SVM学习(续)核函数 & 松弛变量和惩罚因子
SVM的文章可以看:http://www.cnblogs.com/charlesblc/p/6193867.html 有写的最好的文章来自:http://www.blogjava.net/zhenan ...
- <感悟帖>互联网与电子传统行业之经历
依据鄙人浅显的实习.毕业工作经验以及个人思考得此文. 鄙人有幸在BAT中的B实习,以及某电子行业科研机构工作,略有心得.总结例如以下.也算是对自己的一个交待和反省. 鄙人小硕毕业211学校非985,C ...
- ES 2016+
ES2016(ES7)新增: Array.prototype.includes Exponentiation Operator 求冥运算 ES2017 (ES8)新增: ECMAScript® 201 ...