https://oj.leetcode.com/problems/zigzag-conversion/

将字符串Z形字排列后,再重新一行一行输出。

可以找到每一行字符位置的规律,然后填充进去。

敲代码之前,先演算好了,每个变量是怎样表达的,规律到底是什么样的。

#include <iostream>
#include <vector>
#include <string>
using namespace std; class Solution {
public:
string convert(string s, int nRows) {
if(s.size() == || nRows == || nRows == )
return s; string str_ans;
vector<string> ans;
ans.resize(nRows);
int Num = nRows + nRows - ; int times = ;
int paddle = ;
while(times >= )
{
for(paddle = ; paddle<nRows;paddle++)
{
int pivot = times*Num + paddle;
if(pivot >= s.size())
{
times = -;
break;
}
ans[paddle] += s[pivot];
if( !paddle == && paddle != (nRows -))
{
int temp = times*Num + nRows - + nRows - - paddle;
if( temp < s.size())
ans[paddle] += s[temp];
}
} times++;
}
for(int i = ; i< nRows; i++)
str_ans += ans[i];
return str_ans;
}
}; int main()
{
class Solution myS;
cout<<myS.convert("ABC",)<<endl;
return ;
}

LeetCode OJ--ZigZag Conversion的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  4. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  5. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  6. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  8. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  10. leetcode 6. ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...

随机推荐

  1. Linux 命令、配置文件及操作

    Linux 命令.配置文件及操作 命令 命令 参数 说明 A alias.unalias 命令别名 B C cat 查看文件内容 cd 切换目录 chown 修改拥有着 chgrp 修改所属组 chm ...

  2. Node项目实战-静态资源服务器

    打开github,在github上创建新项目: Repository name: anydoor Descripotion: Tiny NodeJS Static Web server 选择:publ ...

  3. 忘记root密码怎么办-单用户模式修改root密码

    忘记root密码怎么办-单用户模式修改root密码================================= 1,开机3秒内按下向下的方向键,目的是为了不让它进入系统,而是停留在开机界面. 2 ...

  4. nginx日志相关优化安全

    一.编写脚本实现nginx access日志轮询 配置日志切割脚本,如下: [root@nginx shell]# cat cut_nginx_log.sh #!/bin/bash #Author:M ...

  5. 通过代码链接ftp上传下载删除文件

    因为我的项目是Maven项目,首先要导入一个Maven库里的包:pom.xml <dependency>            <groupId>com.jcraft</ ...

  6. python之绝对导入和相对导入

    绝对导入 import sys, os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) sys.path.append(BASE_DIR) ...

  7. HDU 4565 So Easy! 矩阵快速幂

    题意: 求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值. 分析: 设\((a+\sqrt{b})^n=A_n+B_n \sq ...

  8. CodeForces 500E New Year Domino

    题意: 从左到右排列着\(n\)个多米诺骨牌,它们分别站在\(x\)轴上的位置\(p_i\)上且高度为\(l_i\). 当第\(i\)个多米诺骨牌向右倒下时,如果\(p_i < p_j \leq ...

  9. HTML中块级元素和行内元素的总结和区分。

    HTML标签 html标签定义: 是由一对尖括号包裹的单词构成,例如: <html>. 标签不区分大小写<html> 和 <HTML>, 推荐使用小写. 标签分为两 ...

  10. 【LVS】简介与说明

    一.IPVS的三种负载均衡技术 通过NAT实现虚拟服务器(VS/NAT) 客户通过Virtual IP Address(虚拟服务的IP地址)访问网络服务时,请求报文到达调度器,调度器根据连接调度算法从 ...