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的更多相关文章

  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. 2018上半年GitHub上最热门的开源项目

    关注GitHub的人都知道,这个平台上面有太多优秀的值得学习的开源项目了,这里总结了2018上半年GitHub上最热门的开源项目. 1: tensorflow https://github.com/t ...

  2. 安装Apache PHP MySQL PHPMyAdmin

    视频教程:https://www.youtube.com/watch?v=FJC2iGt_2bc,Youtube看不了的FQ吧-3- 本人参考这篇文章:http://blog.csdn.net/kno ...

  3. C++的new_handler

    这个new_handler其实对应于signal_handler 当operator new申请一个内存失败时,它会进行如下的处理步骤:1.如果存在客户指定的处理函数,则调用处理函数(new_hand ...

  4. C# 之 继承

    继承     继承是OOP最重要的特性之中的一个.不论什么类都能够从还有一个类中继承,这就是说,这个类拥有它继承的类的全部成员. 在OOP中,被继承的类称为父类. 在C#中的对象仅能直接派生于一个基类 ...

  5. 使用Linux遇到的一些问题和解决方案

    1.如何在重装系统或换机器以后继续使用以前用过的thunderbird邮件 执行命令thunderbird -ProfileManager以后会打开一个配置用户界面. 在里面添加一个新的配置,然后选择 ...

  6. android ActionBar的使用

    Action Bar主要功能包括:   1. 显示选项菜单   2. 提供标签页的切换方式的导航功能,能够切换多个fragment.    3.  提供下拉的导航条目.   4. 提供交互式活动视图取 ...

  7. Linq聚合函数使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. jquery的ajax总结

    jquery的ajax总结 一.总结 一句话总结:ajax函数中层级关系如下: 最底层的封装方式: $.ajax(); 第二层: .load(),$.get(), $.post() 最高层: $.ge ...

  9. Material Design控件使用学习 toolbar+drawerlayout+ Snackbar

    效果 1.,导包design包和appcompat-v7 ,设置Theme主题Style为NoActionbar 2.custom_toolbar.xml <?xml version=" ...

  10. 1.Maven之(一)Maven是什么

    转自:https://blog.csdn.net/xhxmister/article/details/79409208 首先,Maven的正确发音是[ˈmevən],而不是“马瘟”以及其他什么瘟.Ma ...