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 ...
随机推荐
- CSUOJ 1603 Scheduling the final examination
1603: Scheduling the final examination Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 49 Solved: 1 ...
- spark系统实现yarn资源的自动调度
参考: http://blog.csdn.net/dandykang/article/details/48160953 对于Spark应用来说,资源是影响Spark应用执行效率的一个重要因素. ...
- 为线程绑定CPU
// learn gcc atomic variable #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> ...
- ZOJ 3690 & HDU 3658 (矩阵高速幂+公式递推)
ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...
- servlet学习(1)
1.Servlet是sun公司提供的一门用于开发动态web资源的技术. 2.Servlet在web应用的位置: 3.创建Servlet的三种方式: (1)实现servlet的接口 (2)继承Gener ...
- Java io流的学习
近期几天细致学了Java的io流.本来是打算看视频通过视频来学习的.但是后来发现事实上视频看不怎么懂也感觉不是非常easy上手,所以就通过百度和api文档学习了Java的io流 io流能够有两个分类, ...
- vim 基础学习之global
global命令可以在指定模式下,匹配行上进行Ex命令 使用格式: :[range]g[lobal]/{pattern}/[cmd] range-是执行范围(如果缺省,是%) global-命令关键字 ...
- 阿里云Redis使用规范
一.键值设计 1.key名设计 (1)[建议]: 可读性和可管理性 以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:id ugc:video:1 (2)[建议]: 简洁性 ...
- ElasticSearch vs Lucene多维度分析对比
ElasticSearch vs Lucene的关系,简单一句话就是,成品与半成品的关系. (1)Lucene专注于搜索底层的建设,而ElasticSearch专注于企业应用. (2)Luncen ...
- Fragment-如何监听fragment中的回退事件与怎样保存fragment状态
一.如何监听Fragment中的回退事件 1.问题阐述 在Activity中监听回退事件是件非常容易的事,因为直接重写onBackPressed()函数就好了,但当大家想要监听Fragment中的回退 ...