博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]ZigZag Conversion
阅读量:4151 次
发布时间:2019-05-25

本文共 1615 字,大约阅读时间需要 5 分钟。

class Solution {//be careful with the special case//when done with coding, figure out some cases (including illegal, normal, edge cases)//to run with this solution, this will help to get a bug-free solutionpublic:	string convert(string s, int nRows) {		// Start typing your C/C++ solution below		// DO NOT write int main() function    		vector
> zig(nRows, vector
()); int zigRow = 0; int sign = 1; for (int i = 0; i < s.size(); ++i) { zig[zigRow].push_back(s[i]); if (nRows == 1)//special case zigRow = 0; else { if(zigRow == nRows-1) sign = -1; if(zigRow == 0) sign = 1; zigRow += sign; } } string ans; for (int i = 0; i < zig.size(); ++i) { for (int j = 0; j < zig[i].size(); ++j) { ans.push_back(zig[i][j]); } } return ans; }};

second time

class Solution {public:    string convert(string s, int nRows) {        // Start typing your C/C++ solution below        // DO NOT write int main() function            if(nRows == 1) return s;                string result;        for(int i = 0; i < nRows; ++i)        {            int curOriginalIdx = i;            int k = 0;            while(curOriginalIdx < s.size())            {                int nextOriginalIdx;                if(k%2 == 0) nextOriginalIdx = curOriginalIdx+2*(nRows-1-i);                else nextOriginalIdx = curOriginalIdx+2*(i);                if(nextOriginalIdx != curOriginalIdx)                    result.push_back(s[curOriginalIdx]);                curOriginalIdx = nextOriginalIdx;                k++;            }        }                return result;    }};

转载地址:http://qhxti.baihongyu.com/

你可能感兴趣的文章
【Python】学习笔记——-7.2、访问限制
查看>>
【Python】学习笔记——-7.3、继承和多态
查看>>
【Python】学习笔记——-7.5、实例属性和类属性
查看>>
git中文安装教程
查看>>
虚拟机 CentOS7/RedHat7/OracleLinux7 配置静态IP地址 Ping 物理机和互联网
查看>>
Jackson Tree Model Example
查看>>
常用js收集
查看>>
如何防止sql注入
查看>>
springmvc传值
查看>>
在Eclipse中查看Android源码
查看>>
Android使用webservice客户端实例
查看>>
[转]C语言printf
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>