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. 中文 iOS/Mac 开发博客列表(转)

    转自https://github.com/tangqiaoboy/iOSBlogCN 中文 iOS/Mac 开发博客列表 本博客列表会不断更新维护,如果有推荐的博客,请到此处提交博客信息. 本博客列表 ...

  2. Excel的数据导入到PB的DW中

    Excel的数据导入到PB的DW中//==================================================================== // Event:cb_ ...

  3. JSP Filter用法

    1.filter的作用 在HttpServletRequest请求到达Servlet之前,拦截客户的HttpServletRequest 根据需要检查HttpServletRequest的相关信息,修 ...

  4. word 文档 一次性设置多张图片大小

    1.打开WORD文档,插入多张图片. 2.在word中按alt+f11组合键,进入VBA模式. 3.在左边的工程资源管理器中找到你的word文档,在其上右键/添加/模块 4.复制以下代码 Sub Ma ...

  5. 浏览器中的Javascript的简单对话框

    简单对话框是指对话框不去做设计,而直接使用默认的,如alert.confirm.prompt: <html> <head> <meta http-equiv=" ...

  6. .NET WebBroswer内存释放

    最近写的小说爬取工具遇到了性能瓶颈,使用多个Webbroswer控件预加载多个网页,内存会不断增加,达到400M左右,不能忍. 失败的例子 首先尝试把Webbroswer对象置为null,wb=nul ...

  7. Linux进程管理

    一.进程管理简介 进程是正在执行的程序或命令,每一个进程都是一个运行实体,都有自己的地址空间,并占用一定的系统资源. 进程管理的作用: 1.判断服务器的健康状态 2.查看系统中的所有进程 3.杀死进程 ...

  8. select 和 input 的不可编辑,input隐藏

    select 没有readOnly属性 在jsp中 <select  id="a" name="a" disabled="disabled&qu ...

  9. apache与nginx的虚拟域名配置

    由于开发需求,项目有时候需要设置虚拟域名进行测试.下面是分别是apache和nginx的配置 一.apache 环境:wampserver2.5 1.修改host文件 C:\Windows\Syste ...

  10. PageRank理论与实践及用户评分应用PeopleRank算法

    PageRank,网页排名,又称网页级别.Google左侧排名或佩奇排名,是一种由根据网页之间相互的超链接计算的技术,而作为网页排名的要素之一. Google用它来体现网页的相关性和重要性,在搜索引擎 ...