Leetcode 6——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||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<s.length();i=i+gap){
newString.append(s.charAt(i));
}
}
else{
for(int i=numRows-n-1;i<s.length();i=i+gap){
newString.append(s.charAt(i));
if((index=(i/gap*gap+2*numRows-(i%gap)-2))<s.length())
newString.append(s.charAt(index));
}
}
}
return newString.toString();
}
}
Leetcode 6——ZigZag Conversion的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- leetcode 6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
随机推荐
- directX--关于CSource和CSourceStream (谁调用了fillbuffer)
CSourceStream类,是CSource类的OutputPin[source.h/source.cpp]派生自CAMThread和CBaseOutputPinl 成员变量: CS ...
- html标签自带样式总结
一.html标签自带样式 head { display:none; } body { margin:8px; line-height:1.12; } button, textarea, input, ...
- 芝麻HTTP:Scrapy小技巧-MySQL存储
这两天上班接手,别人留下来的爬虫发现一个很好玩的 SQL脚本拼接. 只要你的Scrapy Field字段名字和 数据库字段的名字 一样.那么恭喜你你就可以拷贝这段SQL拼接脚本.进行MySQL入库处理 ...
- hdu5556 Land of Farms
我对于题目的一种理解 改造农场 1.建新农场 在空的点选 2.重建旧农场 选一个点属于这个农场的地方都要选 最后的农场都不能相连 所以枚举旧农场的个数并进行二分图匹配 #include<bits ...
- freemarker定义一个连续的序列(十九)
1.简易说明 定义一个连续的序列,并打印出序列中的元素 2.实现源码 <#--freemarker定义了一个连续的序列--> <#assign nums=1..100/> &l ...
- Keras FAQ: 常见问题解答
Keras官方中文版文档 如何引用 Keras? 如何在 GPU 上运行 Keras? 如何在多 GPU 上运行 Keras 模型? "sample", "batch&q ...
- Python编程核心内容之一——Function(函数)
Python版本:3.6.2 操作系统:Windows 作者:SmallWZQ 截至上篇随笔<Python数据结构之四--set(集合)>,Python基础知识也介绍好了.接下来准备干 ...
- Unity3D 引擎基础 C# (数据结构入门) Unity3D 界面 UI(NGUI)(动画系统,导航系统)(委托与事件,常用设计模式)
Geomagic Sculpt 2016.2 Windows Software 11个月前 (01-17) 0评论 Geomagic Sculpt 触觉式三维设计 触碰您的设计使用三维工具做三维设计. ...
- 好用的sitemap生成器—GY SiteMap
好用的sitemap生成器-GY SiteMap 下载地址:http://www.wyxxw.cn/download-detail-6-8-14.html 网站地图可以更好的帮助搜索引擎抓取.收录网站 ...
- C++学习-11
虚函数表,调试下断点,指针的自动变量 含有虚函数的类,它的内部含有虚指针指向虚函数表,此时的空类占有4个字节,不管有多少个虚函数,只需要一个虚函数指针指向虚函数表就可以了 构造函数不可以是虚函数,如果 ...