LeetCode题解——ZigZag Conversion
题目:
把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即
P A H N
A P L S I I G
Y I R
那么输出为"PAHNAPLSIIGYIR"
解法:
细节实现题,假如重新排列为5行,那么排列后的下标分布应该为
0 8 16 24
1 7 9 15 17 23
2 6 10 14 18 22
3 5 11 13 19 21
4 12 20
可以看出规律,输出为5行的时候,每两个完整列之间的下标差为8 = (2 × 5 - 2);
然后是从第二行到倒数第二行,每两个完整列之间都插入了一个下标,插入的下标与左边列之间的差依次递减2。
代码:
class Solution {
public:
string convert(string s, int nRows) {
if(s.size() <= || nRows <= )
return s;
string result;
for(int i = ; i < nRows; ++i)
for(int j = , idx = i; idx < s.size(); ++j, idx = (*nRows - ) * j + i) //idx为计算出来的原串下标
{
result.append(, s[idx]);
if(i == || i == nRows - ) //第一行和最后一行的完整列中间没有插入字符
continue;
if(idx + *(nRows - i - ) < s.size()) //计算插入的字符,其下标与左边的差
result.append(, s[idx + *(nRows - i - )]);
}
return result;
}
};
LeetCode题解——ZigZag Conversion的更多相关文章
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- leetcode题解||ZigZag Conversion问题
problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...
- 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 ...
随机推荐
- 解决position:relative情况下,z-index无效的方法
在实际开发中,div+css经常会碰到层级的问题 其中有个很头痛的就是z-index控制层级时,老是发现z-index不起作用 老杨依据自己的经验,总结出以下步骤: 1.判断被覆盖的层(想要置顶的层) ...
- Extjs-4.2.1(二)——使用Ext.define自定义类
鸣谢:http://www.cnblogs.com/youring2/archive/2013/08/22/3274135.html --------------------------------- ...
- js获取节点
demo1: <!-- <div id="test" v="1">你好</div> --> // console.log(t ...
- 用in判断input中的placeholder属性是否在这个对象里
<input id="test"> var ele = document.getElementById("test"); if("plac ...
- The 11th Zhejiang Provincial Collegiate Programming Contest->Problem A:A - Pokemon Master
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3776 题意:比较两组数据的总和大小. #include <iostr ...
- 国外程序员整理的 C++ 资源大全
摘要:C++是在C语言的基础上开发的一种集面向对象编程.泛型编程和过程化编程于一体的编程语言.应用较为广泛,是一种静态数据类型检查的,支持多重编程的通用程序设计语言. 关于 C++ 框架.库和资源的一 ...
- jsp关于include html、jsp等文件出现乱码问题的解决方案
一般来说使用jsp标签<jsp:include>引入一个jsp文件: ①可以在被引入的jsp中加入:<%@ page contentType="text/html;char ...
- Spring下载地址
spring官方网站改版后,不提供直接下载,而是通过maven下载,所以将直接下载的地址给出: http://maven.springframework.org/release/org/springf ...
- linux动态库默认搜索路径设置的三种方法
众所周知, Linux 动态库的默认搜索路径是 /lib 和 /usr/lib .动态库被创建后,一般都复制到这两个目录中.当程序执行时需要某动态库, 并且该动态库还未加载到内存中,则系统会自动到这两 ...
- Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...