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)

  1. P A H N
  2. A P L S I I G
  3. 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:

  1. string convert(string text, int nRows);
  1. convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

Tags: String

分析:

(1)此题要求返回string,可以利用string = string+char 的方式实现可变长string。

(2)此题没有要求打印出表格中的分布结构,故可以不考虑列分布,每行用一个字符串接收逐渐落在自己行内的字符。当行数i未到达最大行nRows-1时,字符串"|"分布,i++;

当行数达到nRows-1时,字符串开始"/"分布,i--。

c++代码:

  1. class Solution {
  2. public:
  3. string convert(string s, int nRows) {
  4. if (nRows <= )
  5. return s;
  6. vector < string > zigzag(nRows, "");
  7. //此处也可以直接用字符串数组string zigzag[nRows];来声明
  8. int len = s.length();
  9. int k = ; //约束字符串长度
  10. int i = ; //约束行
  11. while (k < len) {
  12. if (i == nRows - ) {
  13. //此处是给最后一行字符串更新
  14. zigzag[i] += s[k++];
  15. //此处接着把字符串沿"/"方向分布,直到第一行(不包括)
  16. for (i = i - ; i > && k < len; i--) {
  17. zigzag[i] += s[k++];
  18. }
  19. } else {
  20. //此处把字符串沿着"|"方向分布,直到最后一行(不包括)
  21. zigzag[i] += s[k];
  22. i++;
  23. k++;
  24. }
  25. }
  26. string result = "";
  27. //跟前面一样,字符串加字符赋值自身实现可变长string效果
  28. for (int i = ; i < nRows; i++) {
  29. for (int j = ; j < zigzag[i].length(); j++) {
  30. result += zigzag[i][j];
  31. }
  32. }
  33. return result;
  34. }
  35. };

LeetCode Algorithm 06_ZigZag Conversion的更多相关文章

  1. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

  2. LeetCode Algorithm 05_Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  4. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  5. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  6. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  8. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

随机推荐

  1. CSUOJ 1603 Scheduling the final examination

    1603: Scheduling the final examination Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 49  Solved: 1 ...

  2. spark系统实现yarn资源的自动调度

    参考: http://blog.csdn.net/dandykang/article/details/48160953     对于Spark应用来说,资源是影响Spark应用执行效率的一个重要因素. ...

  3. 为线程绑定CPU

    // learn gcc atomic variable #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> ...

  4. ZOJ 3690 &amp; HDU 3658 (矩阵高速幂+公式递推)

    ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...

  5. servlet学习(1)

    1.Servlet是sun公司提供的一门用于开发动态web资源的技术. 2.Servlet在web应用的位置: 3.创建Servlet的三种方式: (1)实现servlet的接口 (2)继承Gener ...

  6. Java io流的学习

    近期几天细致学了Java的io流.本来是打算看视频通过视频来学习的.但是后来发现事实上视频看不怎么懂也感觉不是非常easy上手,所以就通过百度和api文档学习了Java的io流 io流能够有两个分类, ...

  7. vim 基础学习之global

    global命令可以在指定模式下,匹配行上进行Ex命令 使用格式: :[range]g[lobal]/{pattern}/[cmd] range-是执行范围(如果缺省,是%) global-命令关键字 ...

  8. 阿里云Redis使用规范

    一.键值设计 1.key名设计 (1)[建议]: 可读性和可管理性 以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:id ugc:video:1 (2)[建议]: 简洁性 ...

  9. ElasticSearch vs Lucene多维度分析对比

    ElasticSearch vs Lucene的关系,简单一句话就是,成品与半成品的关系. (1)Lucene专注于搜索底层的建设,而ElasticSearch专注于企业应用.   (2)Luncen ...

  10. Fragment-如何监听fragment中的回退事件与怎样保存fragment状态

    一.如何监听Fragment中的回退事件 1.问题阐述 在Activity中监听回退事件是件非常容易的事,因为直接重写onBackPressed()函数就好了,但当大家想要监听Fragment中的回退 ...