C++ leetcode::ZigZag Conversion
mmp,写完没保存,又得重新写。晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢。这操蛋的生活到底想对我这个小猫咪做什么。
今后要做一个早起的好宝宝~晚起就诅咒自己期末考试考的不会、会的不考。
题目:
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"
题目一看并不难,通过下标来决定该位置的字母应该放在哪里,在此就不赘述公示了,纸上推一推就出来了。不过写这个题的时候自己犯了一个大错误,就是嵌套循环中更新了遍历字符串的下标变量,但是循环终止条件没有考虑越界。这也是我经常犯的一个错误,下次不能再犯了,不然一点长进都没有。
class Solution {
public:
string convert(string s, int numRows) {
string result[numRows];
int sum=2*numRows - 2;
int i = numRows;
if (s.size()<=numRows || numRows == 1)
return s;
for(int j = 0; j < numRows&&j<s.size(); j++){
result[j] = s[j];
}
while(i<s.size()){
int k = 0;
while(k<numRows -2 && i<s.size())
{
if(i%sum == numRows+k && i%sum != sum)
result[numRows-2-k] = result[numRows-2-k] + s[i];
i++;
k++; }
for(int j = 0; j < numRows && i < s.size(); j++){
result[j] = result[j] + s[i];
i++;
}
}
string conversion;
for(int j = 0; j < numRows && j < s.size(); j++)
conversion = conversion + result[j];
return conversion;
}
};
提交代码后,42ms,击败15%。我想不出更好的解法了。看看大佬的,mmp,也没看懂。
class Solution {
public:
string convert(string s, int numRows)
{
vector<string> substring(numRows);
int l = s.length();
int counter = ; if(numRows == )
return s; int i = ;
while (i < numRows)
{
if (counter == l)
break; substring[i] += s[counter];
counter++; if (i == (numRows-) && counter != l)
{
for(int j = i - ; j>=;j--)
{
if (counter == l)
break; substring[j] += s[counter];
counter++;
}
i=;
}
++i;
} string result ; for(int k = ; k<numRows; k++)
{
result += substring[k];
}
return result;
}
};
C++ leetcode::ZigZag Conversion的更多相关文章
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode: ZigZag Conversion 解题报告
ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...
- [leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...
- [LeetCode]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 like ...
- [LeetCode] ZigZag Conversion [9]
称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- Leetcode:ZigZag Conversion分析和实现
问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A G M B F H ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
随机推荐
- 【转】Windows Live Writer 代码插件改造
源码和插件都在后面,如果不想看我神神叨叨的可以直接到文章后面下载 一 .找插件 在使用Windows Live Writer 经常要用到插入代码的功能,根据博客园中教程,分别使用了: WindowsL ...
- File操作-将txt里的内容写入到数据库表
package com.Cristin.File;//将txt里的内容写入到数据库表 import com.Cristin.MySQL.AddDataToDB;import org.testng.an ...
- 基因组与Python --PyVCF 好用的vcf文件处理器
vcf文件的全称是variant call file,即突变识别文件,它是基因组工作流程中产生的一种文件,保存的是基因组上的突变信息.通过对vcf文件进行分析,可以得到个体的变异信息.嗯,总之,这是很 ...
- 理解 Redis(3) - 字符串值
正如前面所讲的, redis 的数据结构就是一系列的键值对键 -> printable ASCII (可打印的 ASCII 码, 最大值是 512MB)值 -> Primitives (基 ...
- CentOS7下搭建LAMP+FreeRadius+Daloradius Web管理
注意:本文所有命令均在root命令下执行. freeradius服务官网:http://freeradius.org/ daloradius Web管理页面官网:https://sourceforge ...
- win7下配置Tomcat
1.下载tomcat 2.添加系统环境变量,我的电脑->属性->高级系统设置->环境变量 (1)变量名: CATALINA_BASE 变量值: D:\Program File ...
- Java中有多个异常, 如何确定捕获顺序(多个catch),先从上到下执行,判断异常的大小,如果包含捕到异常,就进入这个catch,后面的就不再执行
Java中异常的捕获顺序(多个catch)( Java代码 import java.io.IOException; public class ExceptionTryCatchTest { publi ...
- Spring 的@@Autowired 和 @Qualifier注释
@Autowired spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造方法进行标注,配合AutowiredAnnotationBeanProc ...
- [JS]计算字符串中出现最多的字符和其出现次数
这是一道面试题 此处是利用Obj来解决的,当然不只此一种方法. //思路:遍历数组,拿到一个字符,并将之以 "字符":出现次数 的key:value形式存到对象中. //如果此字符 ...
- gulp自动化打包工具
/** * Created by hasee on 2016/7/5. */var gulp = require('gulp');var sass = require('gulp-sass');//容 ...