64. ZigZag Conversion
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".
思路:
方法1: 刚开始正着方三个,然后反着放两个,正着放两个。直到结束。(优)
class Solution {
public:
string convert(string s, int nRows) {
if(nRows <= 1 || s == "") return s;
int len = s.length();
string s2[nRows];
int k = 0, k2 = 0;
while(k2 < len){
while(k < nRows && k2 < len) s2[k++].push_back(s[k2++]);
k--;
while(k > 0 && k2 < len) s2[--k].push_back(s[k2++]);
k++;
}
string s3;
for(int i = 0; i < nRows; ++i) {
s3 += s2[i];
}
return s3;
}
};
方法二: 类似方法一,不过存下每个位置的字符应该放的地方。然后依次读入。
class Solution {
public:
string convert(string s, int nRows) {
if(nRows <= 1 || s == "") return s;
int len = s.length();
vector<int> index(2*nRows-2);
int t = 0;
for(int i = 0; i < nRows; ++i) index[t++] = i;
for(int j = nRows-2; j > 0; --j) index[t++] = j;
string s2[nRows];
int k = 0, m = 2*(nRows-1);
while(k < len){
s2[index[k % m]].push_back(s[k]);
k++;
}
string s3;
for(int i = 0; i < nRows; ++i) {
s3 += s2[i];
}
return s3;
}
};
64. ZigZag Conversion的更多相关文章
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
随机推荐
- 当powershell遇上mysql引发的血案
------------------[故事来由]------------------ 起因:群友问:把cmd.exe /c a:\mysql5.6\bin\mysql.exe -uroot < ...
- GSM模块fibocom G510使用记录
一、背景:最近在做一个单定位的产品,对低功耗要求较高,选用了G510系列的模块。现在仅做了三块样板,先熟悉下。 二、优点:1.功耗低,和西门子的BGS2差不多;2.注册网络指令简单;3.其他有待发现 ...
- 修改 UISearchBar cancelButton 样式
今天收到个问题,老大让我修改UISearchBar cancelButton的样式本来以为很简单的一个活,没想到让我长知识了. 开始在网上搜到的方法和我想象的一样,通过遍历Subviews获得butt ...
- Android常见控件— — —AlertDialog
package com.example.uiwidgettest2; import android.app.Activity;import android.app.AlertDialog;import ...
- 《C++primer》v5 第1章 开始 读书笔记 习题答案
从今天开始在博客里写C++primer的文字.主要以后面的习题作业为主,会有必要的知识点补充. 本人也是菜鸟,可能有不对之处,还望指出. 前期内容可能会比较水. 1.1略 1.2略 1.3 cin和c ...
- Python使用CGIHTTPServer调用shell作为cgi脚本
#!/bin/bash echo "Content-Type:text/html" echo "" echo "hello world!" ...
- iOS应用数据存储2-SQLite3数据库
SQLite3 SQLite3是一款开源的嵌入式关系型数据库,可移植性好,易使用,内存开销小. SQLite3是无类型的,意味着你可以保存任何类型的数据到任意表的任意字段中. SQLite ...
- CSS行内元素和块级元素的居中
一.水平居中 行内元素和块级元素不同,对于行内元素,只需在父元素中设置text-align=center即可; 对于块级元素有以下几种居中方式: 1.将元素放置在table中,再将table的marg ...
- Lua协程
协作例程 1.同一时刻仅一个例程在运行 2.执行权让渡和恢复[栈]
- 深入浅出数据分析 Head First Data Analysis Code 数据与代码
<深入浅出数据分析>英文名为Head First Data Analysis Code, 这本书中提供了学习使用的数据和程序,原书链接由于某些原因不 能打开,这里在提供一个下载的链接.去下 ...