LeetCode Algorithm 06_ZigZag Conversion
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 text, int nRows);
convert("PAYPALISHIRING", 3)
should return "PAHNAPLSIIGYIR"
.
Tags: String
分析:
(1)此题要求返回string,可以利用string = string+char 的方式实现可变长string。
(2)此题没有要求打印出表格中的分布结构,故可以不考虑列分布,每行用一个字符串接收逐渐落在自己行内的字符。当行数i未到达最大行nRows-1时,字符串"|"分布,i++;
当行数达到nRows-1时,字符串开始"/"分布,i--。
c++代码:
class Solution {
public:
string convert(string s, int nRows) {
if (nRows <= )
return s;
vector < string > zigzag(nRows, "");
//此处也可以直接用字符串数组string zigzag[nRows];来声明
int len = s.length();
int k = ; //约束字符串长度
int i = ; //约束行
while (k < len) {
if (i == nRows - ) {
//此处是给最后一行字符串更新
zigzag[i] += s[k++];
//此处接着把字符串沿"/"方向分布,直到第一行(不包括)
for (i = i - ; i > && k < len; i--) {
zigzag[i] += s[k++];
}
} else {
//此处把字符串沿着"|"方向分布,直到最后一行(不包括)
zigzag[i] += s[k];
i++;
k++;
}
}
string result = "";
//跟前面一样,字符串加字符赋值自身实现可变长string效果
for (int i = ; i < nRows; i++) {
for (int j = ; j < zigzag[i].length(); j++) {
result += zigzag[i][j];
}
}
return result;
}
};
LeetCode Algorithm 06_ZigZag Conversion的更多相关文章
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- LeetCode Algorithm 05_Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
随机推荐
- 【CS Round #37 (Div. 2 only) B】Group Split
[Link]:https://csacademy.com/contest/round-37/task/group-split/ [Description] 让你把一个数分成两个数a.b的和; (a,b ...
- 洛谷 P1985 翻转棋
P1985 翻转棋 题目描述 农夫约翰知道,聪明的奶牛可以产更多的牛奶.他为奶牛设计了一种智力游戏,名叫翻转棋. 翻转棋可以分成 M × N 个格子,每个格子有两种颜色,一面是黑的,一面是白的. 一旦 ...
- div设置了居中和宽度,但是显示时宽度占100%
<div id="bigDiv" align="center"> <div id="bottom" style=" ...
- 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类
在上一篇文章中<使用DbUtils实现增删改查>,发现运行runner.query()这行代码时.须要自己去处理查询到的结果集,比較麻烦.这行代码的原型是: public Object q ...
- js正則表達式--验证表单
检測手机号码:/0? (13|14|15|18)[0-9]{9}/ 检測username:(数字,英文,汉字.下划线.中横线):/^[A-Za-z0-9_\-\u4e00-\u9fa5]+$/ pas ...
- codeforce 571 B Minimization
题意:给出一个序列,经过合适的排序后.使得最小. 做法:将a升序排序后,dp[i][j]:选择i个数量为n/k的集合,选择j个数量为n/k+1的集合的最小值. 举个样例, a={1,2,3,4,5,6 ...
- Linux 内建命令和系统命令
shell内建命令是指bash(或其它版本)工具集中的命令.一般都会有一个与之同名的系统命令,比如bash中的echo命令与/bin/echo是两个不同的命令,尽管他们行为大体相仿.当在bash中键入 ...
- dp水题
hdu 2084: #include <stdio.h> #include <iostream> #include <string.h> using namespa ...
- Elasticsearch中JAVA API的使用
1.Elasticsearch中Java API的简介 Elasticsearch 的Java API 提供了非常便捷的方法来索引和查询数据等. 通过添加jar包,不需要编写HTTP层的代码就可以开始 ...
- userAgent判断客户端,以及各个浏览器的ua
userAgent判断客户端,以及各个浏览器的ua http://blog.csdn.net/yoyoosyy/article/details/70142884 navigator.userAgent ...