LeetCode OJ--ZigZag Conversion
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的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- 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/ The string "PAYPALISHIRING" is writt ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- [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
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
随机推荐
- jenkins+svn+pipeline+kubernetes部署java应用(一)
一.linux安装svn服务端 yum -y install subversion 二.创建svn版本库(项目仓库) mkdir -p /home/svn svnadmin create /home/ ...
- php通过geohash算法实现查找附近的商铺
geohash有以下几个特点: 首先,geohash用一个字符串表示经度和纬度两个坐标.利用geohash,只需在一列上应用索引即可. 其次,geohash表示的并不是一个点,而是一个矩形区域.比如编 ...
- 【linux】 服务器文件说明
文件名 说明 /etc/resolv.conf 域名解析服务器地址文件 /etc/services 服务程序对应端口号文件 /etc/passwd 登录账号文件 /etc/hosts 本地IP域名解 ...
- Applied Nonparametric Statistics-lec8
Ref:https://onlinecourses.science.psu.edu/stat464/print/book/export/html/11 additive model value = t ...
- python模块之shutil和zipfile
shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 import shutil s ...
- 【HIHOCODER 1176】 欧拉路·一
描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的角色来到了一个很大的湖边.湖上有N个小岛(编号1..N),以及连接小岛的 ...
- 最长公共子序列(LCS)问题
最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:子串是串的一个连续的部分,子序列则是从不改变序 ...
- LightOj:1265-Island of Survival
Island of Survival Time Limit: 2 second(s) Memory Limit: 32 MB Program Description You are in a real ...
- jQuery ui 百叶窗blind方向设置
百叶窗特效(Blind Effect)参数direction设置 $( document ).click(function() { $( "#toggle" ).toggle( & ...
- dedecms 建站相关问题
1.栏目新建文章提示:模板文件不存在,无法解析文档! 解决方法:把模板文件使用".html"的格式 /include/arc.archives.class.php 556行 if ...