LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/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".
解题思路:
Zigzag:即循环对角线结构(
| 0 | 8 | 16 | |||||||||
| 1 | 7 | 9 | 15 | 17 | |||||||
| 2 | 6 | 10 | 14 | 18 | |||||||
| 3 | 5 | 11 | 13 | 19 | |||||||
| 4 | 12 | 20 |
)
给出代码:
class Solution {
public:
string convert(string s, int nRows){
if(nRows == ) return s;
string res[nRows];
int i = , j, gap = nRows-;
while(i < s.size()){
for(j = ; i < s.size() && j < nRows; ++j) res[j] += s[i++];
for(j = gap; i < s.size() && j > ; --j) res[j] += s[i++];
}
string str = "";
for(i = ; i < nRows; ++i)
str += res[i];
return str;
}
};
LeetCode 6 ZigZag Conversion(规律)的更多相关文章
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- [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 ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- Leetcode 6——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode(60)-ZigZag Conversion
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
随机推荐
- MSSQL自动备份数据库
最近项目中,需要用到MSSQL自动定时备份功能,本来想利用C#自己写一个的,但是听说在MSSQL2008中已经集成了功能强大的自动备份功能,于是便提刀上阵,狠狠地琢磨了一番: 首先,打开MSSQL20 ...
- 二叉查找树(一)之 图文解析 和 C语言的实现
概要 本章先对二叉树的相关理论知识进行介绍,然后给出C语言的详细实现.关于二叉树的学习,需要说明的是:它并不难,不仅不难,而且它非常简单.初次接触树的时候,我也觉得它似乎很难:而之所产生这种感觉主要是 ...
- WebService基于SoapHeader实现安全认证
本文仅提供通过设置SoapHeader来控制非法用户对WebService的调用,如果是WebService建议使用WSE3.0来保护Web服务,如果使用的是Viaual Studio 2008可以使 ...
- IIS相关问题整理
1.报错:请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理 解决方案地址:http://blog.csdn.net/canielau/article/details/7609613 2.报错: ...
- Event事件跨浏览器封装
var Event = { //注册事件 addEvent: function(element,type,handler){ if(element.addEventListener){ //DOM2级 ...
- Linux - Ubuntu下JDK配置
系统版本: ubuntu 14.04 x64JDK版本: jdk-8u60-linux-x64 1.查看系统位数,输入以下命令即可 getconf LONG_BIT 2.下载对应的JDK文件,我这里下 ...
- asp.net MVC ajax上传文件
普通上传 view: <body> <form id="form1" method="post" action="@Url.Acti ...
- MEF核心笔记(6)让 MEF 拥抱 AOP
场景: 最近推荐同事在项目中使用起了 MEF,用其构建一个插件式的多人开发框架,因为该框架不是让我去设计了,所以对于 MEF 和 IOC 等概念不是很了解的同事,便会出现各种问题.接入 AOP 便是其 ...
- C#为工作Sql而产生的字符串分割小工具(很实用,你值得拥有)
写在前面 为什么要写这个工具? 工作需要,拼接字符串头晕眼花拼接的,特别是in 查询,后面的参数太多,想在数据执行一些这个sql语句老费劲了. 看正文 工作所有的(后台)攻城狮们都会接触到sql语句, ...
- learning sql (second edition) script
create database bank; use bank; /* begin table creation */ create table department (dept_id smallint ...