博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 6 ZigZag Conversion 模拟 难度:0
阅读量:4679 次
发布时间:2019-06-09

本文共 1026 字,大约阅读时间需要 3 分钟。

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   NA P L S I I GY   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;    }};

  

转载于:https://www.cnblogs.com/xuesu/p/4776724.html

你可能感兴趣的文章
Swift,下标简化方法的调用
查看>>
pal2nal
查看>>
HihoCoder - 1236 Scores (五维偏序,分块+bitset)
查看>>
Jquery 事件 DOM操作
查看>>
运算符
查看>>
FIR滤波器的verilog实现方法
查看>>
display的值和对应的意义
查看>>
HashSet、LinkHashSet、TreeSet总结
查看>>
手机号码输入格式化,数字三三四的输入;手机正则校验输入是否合理及提示;...
查看>>
抽象类
查看>>
CSS3 背景
查看>>
WPF DataGrid 之数据绑定
查看>>
c语言之gdb调试。
查看>>
位反转的最佳算法
查看>>
常用面试问题
查看>>
第一个爬虫
查看>>
Java面试知识点之Java基础
查看>>
老外的前端面试题
查看>>
架构:新浪架构师谈微博架构
查看>>
SQL 语句速查
查看>>