博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 6——ZigZag Conversion
阅读量:4326 次
发布时间:2019-06-06

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

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||numRows>=s.length())return s;               StringBuffer newString=new StringBuffer();        int gap=2*numRows-2;//周期数        int n=numRows;//循环数        int index;                while(n-->0){            if(n==numRows-1||n==0){                for(int i=numRows-n-1;i

 

转载于:https://www.cnblogs.com/GoForMyDream/p/8467591.html

你可能感兴趣的文章
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>