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. jquery-ajax完整写法

    $(function(){ $('#btn').click(function(){ var obj = $(this); //has_click 防止重复多次点击 var has_click = ob ...

  2. DateEdit控件的使用

    按照年月日时分秒显示时间 1. 设置Mask.EditMask和DisplayFormat,EditFormat属性,设置为一致:'yyyy-MM-dd HH:mm';  //按照想要的显示格式设置此 ...

  3. php的socket通信

    socket通常叫做'套接字',用于描述IP地址和端口,是一个通信链的句柄.应用程序通过套接字向网络发出请求或者应答忘了请求.socket既不是程序,也不是协议,其只是操作系统提供的通信层的一组抽象A ...

  4. centos7 docker activemq

    / cd /home/activemq// wget http://apache.fayea.com/activemq/5.13.3/apache-activemq-5.13.3-bin.tar.gz ...

  5. codevs1409 拦截导弹2

    [问题描述]一场战争正在 A 国与 B 国之间如火如荼的展开.B 国凭借其强大的经济实力开发出了无数的远程攻击导弹,B 国的领导人希望,通过这些导弹直接毁灭 A 国的指挥部,从而取得战斗的胜利!当然, ...

  6. dispatch a action with a timeout

    程序入口文件添加依赖: import { createStore, applyMiddleware } from 'redux' import thunk from 'redux-thunk' // ...

  7. Ubuntu 修改 ssh 登录后的欢迎信息

    /etc/update-motd.d/90-updates-available cd /etc/update-motd.d/ vi 10-help-text 修改或者注释掉里面的内容 vi 90-up ...

  8. npm相关

    npm list -g --depth 0 : -g 列出所有全局模块,--depth 使列表只列出简略信息,如包名.版本号

  9. Nodejs学习总结 -Express入门(一)

    Express是基于Node.js平台开发的Web应用开发框架,下面我们入手学习. 官网 : http://www.expressjs.com.cn/ github:https://github.co ...

  10. Java开发基础

    天数 课程 01 Java基础回顾 集合 泛型 IO流 多线程 Junit Properties   HTML   JavaScript   JavaScript   BOM编程   XML基础   ...