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 ...
随机推荐
- Centos7更改网卡名称Eth0并配置静态IP
1.首先查看一下centos7的网卡名称 eno33554984 2.更改为centos7之前版本的网卡名称 3.更改网卡文件的名称 4.禁用可预测命名规则. 通过编辑 /etc/default/gr ...
- HDU 5459 Jesus Is Here(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意: S(1) = c,S(2) = ff, S(3) = cff,之后S(i) = S(i-1)+S( ...
- Ural 1297 Palindrome(后缀数组+最长回文子串)
https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...
- python学习 day011打卡 迭代器
本节的主要内容: 1.函数名的使用以及第一类对象 2.闭包 3.迭代器 一.函数名的运用. 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数的变量. 1.函数名的内存地址 def fun ...
- centos7安装tomcat8 新手入门 图文教程
系统环境 操作系统:64位CentOS Linux release 7.2.1511 (Core) JDK版本:1.8.0_121 下载tomcat8压缩包 访问官网:http://tomcat.ap ...
- 【log4net】配置文件解释
ASP.NET MVC 1.web.config: <configSections> <section name="log4net" type="log ...
- Mybatis的SqlSession理解(一)
SqlSession是Mybatis最重要的构建之一,可以认为Mybatis一系列的配置目的是生成类似JDBC生成的Connection对象的statement对象,这样才能与数据库开启“沟通”,通过 ...
- python 安装包
一般python的包都是.tar.gz结尾的压缩包,据说是linux下面的格式.但也是可以在windows上面安装的,安装过程,1,在 https://pypi.python.org/pypi 这个网 ...
- JAVA实操项目:转账接口设计
在一个项目中,一般都会支付相关的业务,而涉及到支付必定会有转账的操作,转账这一步想起来算是比较关键的部分,这个接口的设计能力,也大致体现出一个人的水平. 昨天碰到了一个题目: 尝试用java编写一个转 ...
- 如何备份MySQL数据库
在MySQL中进行数据备份的方法有两种: 1. mysqlhotcopy 这个命令会在拷贝文件之前会把表锁住,并把数据同步到数据文件中,以避免拷贝到不完整的数据文件,是最安全快捷的备份方法. 命令的使 ...