【LeetCode】6 - ZigZag Conversion
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"
.
Solution:原来是说:
class Solution {
public:
string convert(string s, int nRows) {
if(nRows==)return s;
vector<vector<char>> vec(nRows, vector<char>()); int index=-;
int tag=;
for(int i=;i<s.size();i++){
index += tag;
vec[index].push_back(s[i]);
if(index==nRows-)tag=-;
else{
if(index==)tag=;
}
}
string ret;
for(int i=;i<nRows;i++){
for(int j=;j<vec[i].size();j++){
ret+=vec[i][j];
}
}
return ret;
}
};
【LeetCode】6 - ZigZag Conversion的更多相关文章
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- 【LeetCode】281. Zigzag Iterator 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- warning:deprecated conversion from string constant to 'char *' 解决方案
#include <iostream> using namespace std; int fuc(char *a) { cout << a << endl; } i ...
- Diving Into Lync Client Logins
Now that we have a fully functional UC lab it's time to start using the lab to explore various aspec ...
- git merge简介【转】
转自:http://blog.csdn.net/hudashi/article/details/7664382 git merge的基本用法为把一个分支或或某个commit的修改合并现在的分支上.我们 ...
- CF-358D-Dima and Hares【T^T+*^*】
[文章标题打着转载,是因为不是自己想出来的解题,但下面的文字是自己敲的[~捂脸*>_<*~]] 题目就不贴了~~~DP+greedy的题.弱爆了看别人的代码思路过的.T^T但还是自己复述一 ...
- Vim 新用法
daw , delete a word cw , delete from cursor to the end then insert mode a word 移动: f ; Aa Oo Cc Ii S ...
- linux硬件驱动层
1.make menuconfig scripts/kconfig/lxdialog/menubox.o: In function `print_buttons':menubox.c:(.text+0 ...
- php-redis中文文档(转)
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...
- 《OD学hadoop》第一周0625
一.实用网站 1. linux内核版本 www.kernel.org 2. 查看网站服务器使用的系统 www.netcraft.com 二.推荐书籍 1. <Hadoop权威指南> 1- ...
- zookeeper监控告警
一.ZooKeeper简介 ZooKeeper作为分布式系统中重要的组件,目前在业界使用越来越广泛,ZooKeeper的使用场景非常多,以下是几种典型的应用场景: l 数据发布与订阅(配置中心) l ...
- HDU 1317 XYZZY【Bellman_Ford判断正环】
题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正 这题自己写的时候wa--wa-- 后来看了题解,还 ...