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".

 

Runtime: 16ms

 1 class Solution {
 2 public:
 3     string convert(string s, int numRows) {
 4         if(numRows <= 1) return s;
 5         if(s.empty()) return s;
 6         
 7         string result;
 8         int size = 2 * numRows - 2;
 9         for(int i = 0; i < numRows; i++){
10             for(int j = i; j < s.length(); j += size){
11                 result += s[j];
12                 if(i != 0 && i != numRows - 1){
13                     int temp = j + size - 2 * i;
14                     if(temp < s.length())
15                         result += s[temp];
16                 }
17             }
18         }
19         return result;
20     }
21 };

 

更多相关文章
  • LeetCode (6): ZigZag Conversion
     [题目]LeetCode(6):  ZigZag Conversion URL:   https://leetcode.com/problems/zigzag-conversion/ [描述] The string "PAYPALISHIRING" is written in ...
  • ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display t ...
  • LeetCode  ZigZag Conversion(將字符串排成z字型)
      1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 string a=""; 5 int len=s.length(); 6 if(len<=nRowsnRows==1) retu ...
  • 题目描述:ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to disp ...
  • 一个难度为Easy的题,看了好多人的题解都没想明白,最后使劲想使劲想,才想的差不多..太弱了,要找不到工作了.. 题目描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number ...
  • https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like t ...
  • [Problem] The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (y
  • 题目: 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 ...
一周排行
  • 


    		    14. C#數據結構與算法  雙向鏈表
    首先,明白什么是双向链表.所谓双向链表是如果希望找直接前驱结点和直接后继结点的时间复杂度都
  • 注:通过看gabriel canepa 老师(golinux老师译)文章的学习笔记 实验平台 centos 系统 应用背景:作为系统管理员,他们需要一种安全机制,比如检测文件篡改的机制 那它究竟检测什么呢? 文件内容
  • CentOS 7.0下面安裝並配置Spark
    安装环境: 虚拟机:VMware® Workstation 8.0.1(网络桥接) OS:
  • 在很多应用中,需要对某些对象进行序列化,让它们离开内存空间,入住物理硬盘,以便长期保存.比如最常见的是Web服务器中的Session对象,当有10万用户并发访问,就有可能出现10万个Session对象,内存可能吃不消
  • 作者: hezhiwu5#163.com    时间:-22 大家好!!今晚在华软G43*宿舍没什么事做,把javascript中replace方法讲解一下,如果讲得不对或不合理是情理之中的事,因为我不是
  • 运行Python: 开始->程序->Python->IDEL(Python GUI) 各个菜单项及基本用法的幫助: Help->IDEL Help 完整的Python文档:Help->P ...
  • http://www.oschina.net/project/tag/58/im
  • 一.安装前的准备 硬件需求:将要安装系统的计算机需配置了带PXE功能的网卡(比较简单的方法就是看计算机BIOS中的启动选项里是否有通过LAN或者PXE启动这一项) #yum install dhcp* #yum in
  • Checked 和CheckState都是检查控件选中状态,都能判断是否选中控件. 只是Checked 通过布尔判断(true & false): CheckState 通过枚举判断. checked是直接通 ...
  • package com.Hanqi2; public class _1 { public _1() { } public String xingming; public String renzhong; public