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" ...
随机推荐
- Mysql数据库插入中文出现乱码相关
查看数据库编码的命令:show variables like "character%"; mysql> show variables like "character ...
- 03等待多个线程返回WaitForMultipleObject
二. WaitForMultipleObject 等待单个线程返回 1. 函数原型 DWORD WINAPI WaitForMultipleObjects( _In_ DWORD nCount, _I ...
- vue + axios---封装一个http请求
在使用vue开发时,官方推荐使用axios来请求接口 // axios官方地址 https://github.com/axios/axios 但是axios并不像 vue-resource 一样拥有i ...
- thinkphp5开发restful-api接口学习 教程视频 接口文档
目录 1. 获取验证码 2. 用户注册 3. 用户登录 4. 用户上传头像 5. 用户修改密码 6. 用户找回密码 7. 用户绑定手机号 8. 用户绑定邮箱 9. 用户绑定用户名(手机/邮箱) 10. ...
- VS自学日记整理
vs渣渣自学之旅 一.vs实用插件 二.制作简历之旅 1.一堆错误示范示范 2.标签的使用 3.文件的文本的样式的保存 二.美化博客园之旅 1.第一天 学python有点多这个慢慢消化
- Applied Nonparametric Statistics-lec4
Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/5 Two sample test 直接使用R的t- ...
- Gym - 100781G Goblin Garden Guards (扫描线)
题意: n 只哥布林,每只哥布林都有一个位置坐标. m 个炮台,每个炮台都有一个位置坐标和一个攻击半径. 如果一个哥布林在任何一个炮台的攻击范围内,都会被杀死. 求最后没有被杀死的哥布林的数量. 这题 ...
- German Collegiate Programming Contest 2018
// Coolest Ski Route #include <iostream> #include <cstdio> #include <cstring> #inc ...
- vmware esxi 6.0 开启嵌套虚拟化
环境描述: 已通过vSphere Client创建一个名字为centos7的虚拟机,现在需要打开该虚拟机的嵌套虚拟化功能. 第一步: 开启ESXi Shell 开启vSphere的ssh远程登录服务或 ...
- SpringDataJpa错误
在运行项目的时候出现的错误如下: would dispatch back to the current handler URL [/save] again. Check your ViewResolv ...