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" ...
随机推荐
- Django项目SECRET_KEY等敏感信息保存
在我们做完django项目后,向生产环境部署时,为了避免一些敏感信息被有心人利用,我们应该将其保护起来,比如说在settings配置中的一些密码等内容存在操作系统内,通过调用来使用,比如下面这种做法: ...
- python3爬取豆瓣top250电影
需求:爬取豆瓣电影top250的排名.电影名称.评分.评论人数和一句话影评 环境:python3.6.5 准备工作: 豆瓣电影top250(第1页)网址:https://movie.douban.co ...
- phpMyAdmin关于PHP 5.5+ is required. Currently installed version is: 5.4.16问题
出现这个提示PHP 5.5+ is required. Currently installed version is: 5.4.16原因可能是: phpmyadmin 版本太新,最小需要php5.5. ...
- Do not pour out HDU - 5954 数学积分
题目:题目链接 思路:纯高等数学问题,不过不是很好积分,具体积分思路及过程参考大佬博客——https://blog.csdn.net/danliwoo/article/details/53002695 ...
- JAVA-基础(五) 更多工具集
1.StringTokenizer(字符串标记) StringTokenizer实现枚举(Enumeration)接口.因此,给定一个输 入字符串,可以使用StringTokenizer对包含于其中的 ...
- SpringDataJpa错误
在运行项目的时候出现的错误如下: would dispatch back to the current handler URL [/save] again. Check your ViewResolv ...
- Scala学习-02-方法
算数和操作符重载 所有的操作符都是方法. a + b 是一种缩写形式 : a .+ b “+”是方法名(操作符重载) ++和—— Scala中并没有“++”和“——”.需要使用“+=”和“-=” ...
- linux随笔四
1.ps -ef : -e 显示系统上运行的所有进程,-f 显示一些有用的信息列 UID:负责启动进程的用户 PID:进程的ID PPID:父进程的PID(某个进程由另一个进程启动) C: ...
- PHP 函数 ignore_user_abort()
ignore_user_abort 设置与客户机断开是否会终止脚本的执行. 本函数返回 user-abort 设置的之前的值(一个布尔值). int ignore_user_abort ([ st ...
- Java中接口的作用
转载于:https://www.zhihu.com/question/20111251 困惑:例如我定义了一个接口,但是我在继承这个接口的类中还要写接口的实现方法,那我不如直接就在这个类中写实现方法岂 ...