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 ...
随机推荐
- hadoop实验:求气象数据的最低温度
1.下载部分数据.由于实验就仅仅下载2003年的部分气象数据 2.通过zcat *gz > sample.txt命令解压重定向 [hadoop@Master test_data]$ zcat * ...
- JSON初入门
JSON:Javascript Object Notation 轻量级的数据交换格式 语法规则:(JSON语法是js对象表示语法的子集) 1.数据在名称/值对中 2.数据由逗号分隔 3.花括号{}保存 ...
- 排序(1)---------选择排序(C语言实现)
选择排序的基本思想: 选择排序(Selection sort)是一种简单直观的排序算法. 它的工作原理例如以下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素 ...
- 如何使用jquery判断一个元素是否含有一个指定的类(class)
如何使用jquery判断一个元素是否含有一个指定的类(class) 一.总结 一句话总结:可以用hasClass方法(专用)和is方法 1.is(expr|obj|ele|fn)的方法几个参数表示什么 ...
- Java: 数据类型
核心:对事物的某种规范 前提: 1.JAVA:JAVA程序的运行是以堆栈的操作来完成的 堆栈以帧为单位保存线程的状态. JVM对堆栈只进行两种操作:以帧为单位的压栈和出栈操作. 理解 ...
- px、em、rem、vw、vh、vm、rpx这些单位的
px是像素 em是参考父元素的font-size的倍数 rem是参考根元素的font-size 常用于响应式,一般会让html的font-size:625%,body的大小为.16rem.这样1rem ...
- celery work logging 问题
celery 的日志里只输出日志 不输入标准打印
- 【2017 Multi-University Training Contest - Team 3】RXD's date
[Link]: [Description] [Solution] [NumberOf WA] 1 [Reviw] [Code] #include <bits/stdc++.h> using ...
- 洛谷 P2392 kkksc03考前临时抱佛脚
P2392 kkksc03考前临时抱佛脚 题目背景 kkksc03的大学生活非常的颓废,平时根本不学习.但是,临近期末考试,他必须要开始抱佛脚,以求不挂科. 题目描述 这次期末考试,kkksc03需要 ...
- POJ 2828 线段树单点更新 离线搞
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...