006 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".
详见:https://leetcode.com/problems/zigzag-conversion/description/
解题思路:
1. 每一组 V 字形的长度为 size = 2 * line - 2 (斜向上的部分没有第一行和最后一样的元素),line 是行数
2. 对于垂直向下的一列元素来说,每一组向下的列之间间隔 size 大小,即一组元素的个数。
3. 对于斜向上的元素来说,它们的位置位于当前组 size - i(i 为该元素所在的行数,一组有 size 个字符,倒数第 i 个,位置为 size -i)。当前组的第一个字符所在位置为 j - i (j 为与斜向上的元素同在一行的,垂直向下的列的元素在字符串中的序号,i 为它们共同的行号)。
即:j-i就是zigzag的起始字符,然后我们是要倒数第i个,因为一个zigzag有size个字符,所以倒数第i个就是size-i,我们要赋值的字符就是(j-i)+(size-i)=j+size-2*i
class Solution {
public:
string convert(string s, int numRows) {
if(s.empty()||numRows==1)
{
return s;
}
string res="";
int len=s.size();
int size=numRows*2-2;
for(int i=0;i<numRows;++i)
{
for(int j=i;j<len;j+=size)
{
res+=s[j];
if(i!=0&&i!=numRows-1&&j+size-2*i<len)
{
res+=s[j+size-2*i];
}
}
}
return res;
}
};
006 ZigZag Conversion的更多相关文章
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [Leetcode]006. ZigZag Conversion
public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
随机推荐
- Select\Poll\Epoll异步IO与事件驱动
事件驱动与异步IO 事件驱动编程是一种编程规范,这里程序的执行流由外部事件来规定.它的特点是包含一个事件循环,但外部事件发生时使用回调机制来触发响应的处理.另外两种常见的编程规范是(单线程)同步以及多 ...
- 百度之星 hdu5701 中位数计数
http://acm.hdu.edu.cn/showproblem.php?pid=5701 给出一个序列,取其中的任何一个连续的序列,该序列的数从小到大排列,待更新,,, #include<i ...
- js实现翻牌效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 转载:IntelliJ IDEA 2016.2 配置Tomcat 运行Web项目
以前都用MyEclipse写程序的 突然用了IDEA各种不习惯的说 借鉴了很多网上好的配置办法,感谢各位大神~ 前期准备 IDEA.JDK.Tomcat请先在自己电脑上装好 好么~ 博客图片为主 请多 ...
- MS SQL 取分组后的几条数据
SELECT uploaddate ,ptnumber ,instcount FROM ( SELECT ROW_NUMBER() OVER( PARTITION BY uploaddate ORDE ...
- TCP/IP的3次握手和4次握手
在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接,如图1所示. (1) 第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_SEND状态,等 ...
- 单片机CY与OV的区别
CY(Carry): 用于表示加法进算中的进位和减法运算中的借位,加法运算中有进位或减法运算中有借位则CY位置1,否则为0 OV: 表示运算过程中是否发生了溢出,若运算结果超过了8位二进制数所能表示数 ...
- Hive与Hbase结合使用
hive的启动需要使用到zookeeper, 所以, 要么自己搭建zookeeper, 要么跟其它东西一起使用, 我这里做的是跟hbase一起使用的zookeeper, 因为hbase自带zookee ...
- p2345 奶牛集会
传送门 题目 约翰的N 头奶牛每年都会参加“哞哞大会”.哞哞大会是奶牛界的盛事.集会上的活动很 多,比如堆干草,跨栅栏,摸牛仔的屁股等等.它们参加活动时会聚在一起,第i 头奶牛的坐标为Xi,没有两头奶 ...
- Spark 各个组件关系
Term Meaning Application User program built on Spark. Consists of a driver program and executors on ...