【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列
(一)题目
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”.
题意:
(二)解题
已知输入条件:初始string s和numRows。可以得出循环间隔gap = 2*numRows-2。
统计规律:
- 第0行 从j=0开始,间距gap,取s[j]
- 第1~numRows-2行 从j=1开始,间距gap=(j%gap)<numRows?(gap-2(j%gap)):(2*gap-2(j%gap)),取s[j]
- 第numRows-1行,间距gap,取s[j]
代码如下:
class Solution {
public:
string convert(string s, int numRows) {
string result;
int gap=2*numRows-2; //初始化gap
if(numRows == 1) return s;//rows为1,直接返回
for(int i = 0 ; i < numRows ; i++)
{
int j = i ;
bool flag = true;
if(i == 0 || i == numRows-1)//如果第0行或最后一行
{
int j = i;
while(j<s.length())
{
result+=s[j];
j += gap;//间距为gap
}
}
else{
int j = i;
while(j<s.length()){
result+=s[j];
j += (j%gap)<numRows?(gap-2*(j%gap)):(2*gap-2*(j%gap));
}
}
}
return result;
}
};
【一天一道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
一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成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 ...
随机推荐
- solr界面
1.1 界面功能介绍 1.1.1 Analysis
- Scala:字符串
http://blog.csdn.net/pipisorry/article/details/52902348 Scala字符串 在 Scala 中,字符串的类型实际上是 Java String,它本 ...
- Spark UI界面原理
当Spark程序在运行时,会提供一个Web页面查看Application运行状态信息.是否开启UI界面由参数spark.ui.enabled(默认为true)来确定.下面列出Spark UI一些相关配 ...
- EBS并发程序监控
SELECT s.* FROM fnd_concurrent_requests r, v$session v, v$sql s WHERE r.oracle_session_id = v.audsid ...
- android 集成微博常见问题
我们在做微博集成登录.分享.聊天的时候,肯定会遇到很多的坑,这里总结下常见的问题. 文件不存在 C8998 的解决方法 如图我们走微博授权登录的时候如果OAuth2.0 授权设置回调页面设置和本地的不 ...
- 对Bitmap的内存优化
在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitmap时,分给虚拟机中的图片的堆栈大小只有8M,如果超出了,就会出现OutOfMemory异常.所以,对于图 ...
- iOS 南京互联网大会分享及个人见解 韩俊强的博客
首先分两大块: 1.如何打造高效/稳定的App (重点): 2.软件自动化测试: 每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 每日更新关注:http://we ...
- 一个简单程序快速入门JDBC
首先创建jdbc的库,再在这个库里面创建一张users表. drop database if exists jdbc; create database if not exists jdbc; use ...
- boost::coroutine 无法显示调用栈
boost::coroutine 无法显示调用栈(金庆的专栏)一例因 boost::format() 格式化参数个数错误造成的 coredump,因为使用了 boost::coroutine, 无法显 ...
- 最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)
===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...