ZigZag-LeetCode
题目:
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:即循环对角结构.
向下循环:nRows
斜角线循环
重复...
#include <string>
#include <iostream>
using namespace std; string convert(string s,int nRows){
if(nRows==) return s; string res[nRows];
int i==,j,gap=nRows-; while(i<s.size()){
for(j=;j<nRows && i<s.size();j++) res[j]+=s[i++];
for(j=gap;j> && i<s.size();j--) res[j]+=s[i++];
} string ret="";
for(i=;i<nRows;i++){
ret+=res[i];
}
return ret;
}
ZigZag-LeetCode的更多相关文章
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 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/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
- LeetCode Zigzag Iterator
原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
随机推荐
- C# 解析json Newtonsoft.Json
Newtonsoft.Json.dll public class ErrorInfo { public error_response error_response { get; set; } } pu ...
- 利用try-catch判断变量是已声明未赋值还是未声明
目的是如果一个变量是已声明未赋值,就可以直接赋值,并且不能改变变量的作用域: 如果未声明的话,就重新声明, 在网上搜了下,常见的方法是if(typeof(a)=='undefined'){var a= ...
- Zepto源码笔记(三)
ps:本文中"组装成成数组"指的是若元素个数大于1则返回数组,若元素只有1个则返回元素本身 以下函数是$.fn该对象的方法 ready(callback) 通过readyRE正则表 ...
- math.h中的常量
类似于Matlab中经常用到的一些常量,C++里边也是有的.(经查源文件无意中看到) 写入如下代码: #include<iostream> #include<iomanip> ...
- dede调用指定的多个栏目导航
{dede:channelartlist row=' typeid='1,2这里输入多个指定的栏目ID' } <li><a href='{dede:field name='typeu ...
- 40条优化php代码的小实例
1.如果一个方法能被静态,那就声明他为静态的,速度可提高1/4; 2.echo的效率高于print,因为echo没有返回值,print返回一个整型; 3.在循环之前设置循环的最大次数,而非在在循环中; ...
- 内核参数优化之2-1 tcp/ip 标志位报文解析
以下内容纯属虚构,切勿轻易相信! 众所周知,tcp/ip三次握手和四次挥手,均由syn/ack/fin三个标志位报文决定,但是这三个标志位报文,并不是说在构建连接的时候只发送一次的,因为协议不知道网络 ...
- Reshaper 快捷键
配置单个键盘快捷键 1.在主菜单上,选择Tools | Options 2.在导航视图中选择Environment | Keyboard 3.在命令列表中,选择启动ReSharper的命令名称.浏览列 ...
- Kafka笔记--使用ubuntu为bocker(服务器)windows做producer和comsumer(客户端)
原文连接:http://www.cnblogs.com/davidwang456/p/4201875.html 程序仍然使用之前的一篇博文中的例子 :http://www.cnblogs.com/gn ...
- JavaScript对象基础知识
1.对象所包含的元素一组包含数据的属性.如人的名字.书的价格和手机型号等.允许对属性中所包含的数据进行操作的方法. 2.引用对象的途径一个对象真正地被使用,可以采用以下几种方式.引用Javascrip ...