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 ... 
随机推荐
- js验证码倒计时
			var wait=59; function time(){ if(wait >= 0){ $("#buttons").val("" + wait + &q ... 
- http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html
			http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html 
- 李洪强iOS开发本人集成环信的经验总结_09_处理好友请求
			李洪强iOS开发本人集成环信的经验总结_09_处理好友请求 实现这种效果: 01 - 遵守处理好友请求的代理协议 02 - 设置代理 03 - 实现代理方法 04 - 实现代理中用到的方法 
- SRM 585 DIV1 L2
			记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include & ... 
- nginx + tomcat
			http://blog.csdn.net/sun305355024sun/article/details/8620996 
- netty 实现socket服务端编写
			import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap; import io.netty.channe ... 
- WinCE设置多国语言支持
			最近项目中需要支持中(简繁)日韩英多种语言,在网上找了很多解决办法,最后发现还是MSDN最好. [c-sharp] view plaincopy [HKEY_LOCAL_MACHINE/SYSTEM/ ... 
- 中南大学oj 1317 Find the max Link 边权可以为负的树上最长路 树形dp 不能两遍dfs
			http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1317经典问题:树上最长路,边权可以为负值的,树形dp,不能用两边dfs.反例:5 41 2 22 ... 
- poj2823
			这是一道题意简单,数据较大的题(喜闻乐见): 一开始可能会想到RMQ问题,ST,线段树都是O(nlogn),应该勉强能过(没试过): 由于这道题区间是滚动连续的,所以,可以使用单调队列! 以最小值为例 ... 
- poj2478
			比较简单的树形dp; 定义s[i]为节点i的子树节点数和(包括自身):叶子节点s[j]=1; s[i]=signma(s[k])+1 (k是i的孩子) 则i满足的条件是 1.s[k]<=n di ... 
