https://leetcode.com/problems/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".

class Solution {
public:
string convert(string s, int numRows) {
if(numRows == 1)return s;
string* a = new string[numRows];
string ans;
int n = 2 * numRows - 2;
for(int i = 0;i < s.size();i++)
{
int r = i % n;
int l = r % numRows;
if(l == r)a[l] += s[i];
else a[numRows - l - 2] += s[i];
}
for(int i = 0;i < numRows;i++){
ans += a[i];
}
return ans;
}
};

  

LeetCode 6 ZigZag Conversion 模拟 难度:0的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. 【leetcode】ZigZag Conversion

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

  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 6. ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...

随机推荐

  1. Android 基础概念了解

    Android 的前世今生Android 系统框架Android 主要组成 部分Android 常用的操作 Android 的前世今生 Android 的诞生 2003年10月,有"Andr ...

  2. Xcode8.0 去除控制台多余打印

    选择Product->Scheme->Edit Scheme ...或者直接按 command + shift + < 快捷键,在弹出的窗口中Environment Variable ...

  3. Cheatsheet: 2016 03.01 ~ 03.31

    JAVA Quick Java 8 or Java 7 Dev Environments With Docker Printing arrays by hacking the JVM Mobile H ...

  4. windows平台解决quick3.5final + sublimeText3 模拟器找不到的问题

    下载了quick3.5 准备在sublime上进行开发,结果发现quick3.5中的player模拟器不见了,原来模拟器移到了quick引擎主目录下的 /tools/simulator/runtime ...

  5. C# 只移除最后一个字符

    string str = "|||"; Console.WriteLine(str.Substring(0, str.Length - 1)); Console.WriteLine ...

  6. C++备忘录

    参考资料: 1. <C++编程思想(第一卷)> 知识点: ● register变量:它是一种局部变量类型.它告诉编译器尽快访问该变量.一般会把变量放在寄存器中,但不保证一定会.不能得到或计 ...

  7. 深入理解IOC模式及Unity框架

    研究了下,有几篇博客确实已经说得很清楚了 1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html  这篇博客是通过一个 ...

  8. Jenkins+ANT+SVN快速搭建持续集成环境

    伟大的语言都从helloworld开始,这里我们用Jenkins+ANT+SVN来搭建这一套持续集成环境. 环境准备: 一台已经配置好Jenkins和Ant的机器. 项目准备: 用Ecplise建立一 ...

  9. JQuery筛选器全系列介绍

    jQuery提供了强大的选择器让我们获取对象.在这边,我人为地将jQuery选择器分为两大部分:选择对象和筛选条件.选择对象表示要获取什么对象,筛选条件是对获取的对象进行筛选,最终留下符合某些特征的对 ...

  10. LOL

    当输入数据一样时,计算结果是一样的,但运行时间的差别很大.在算法正确的前提下,应该选择算法效率高的 嵌入式软件,BSP驱动,通信协议,上层应用软件. 多协议标签交换(MPLS)是一种用于快速数据包交换 ...