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 ...
随机推荐
- MySQL数据库服务器参数优化mycnf,16G内存8核CPU,
业务场景: 后台支持手机在线更新系统,db服务器内存16G,8核,dell的pc服务器. qps: 200个左右 tps: 1个左右 一分钟50几个 sort_buffer_size = 32M 大了 ...
- 【转】 Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner
目录(?)[-] GridView Spinner GridView GridView是网格状布局,如图所示.在了解ListView后,很容易了解GridView.下面是例子的XML文件. <? ...
- 一. kafka 入门
一.基本概念 Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据.这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键因素. K ...
- ubunt14.04搭建lNMP
一.安装mysql 1. sudo apt-get update 2. sudo apt-get install apt-get nginx 二.安装mysql 1. sudo apt-get ...
- Java 常见注解
@Retention 1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略2.RetentionPolicy.CLASS —— ...
- SpringBoot04 SpringBoot 和 MyBatis 整合
1 所需的jar包 mysql驱动包:mysql-connector-java 数据库链接池:druid mybatis对应jar包:mybatis-spring-boot-starter 分页查询对 ...
- 《精通Spring4.X企业应用开发实战》读后感第七章(AOP概念)
- Webservice 或者HttpRequest请求的时候提示 “指定的注册表项不存在”错误 解决方案
今天又遇到神奇的事情,在使用WebService的时候居然提示“指定的注册表不存在.” The specified registry key does not exist. Google后发现,原来是 ...
- Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute
DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...
- position:fixed;如何居中
div{ position:fixed; margin:auto; left:; right:; top:; bottom:; width:100px; height:100px; } 如果只需要左右 ...