题目描述:

该开始就输在了理解题意上。。 没搞懂zigzag是什么意思。

查了一些解释终于明白要干什么了。     将一个字符串按照Z字形排列(侧着看);再把结果按行输出。

刚开始的想法是讲字符串按照规则排列在一个二维数组中,然后按序扫描数组输出。时间复杂度为O(n2).

进一步改进,按行数生成n个字符串,按照规则将目标字符串中每个字符存入相应的字符串中,最后将n个字符串连接。省去了扫描二维数组的时间开销。

时间复杂度为O(n)。

代码如下:

class Solution {
public:
string convert(string s, int numRows) {
int l = s.length();
if(numRows <= || l < )
return s;
string* s_str = new string[numRows];
int period = * (numRows - ); for(int i = ; i< l; ++ i)
{
s_str[numRows - - abs(numRows - - (i % period))].push_back(s[i]);
//此处为借鉴的公式;
//自己写的s_str[i%period].push_back(s[i])会出现越界错误。
} string str_result;
for(int i = ; i < numRows; ++ i)
{
str_result.append(s_str[i]);
}
return str_result;
}
};

leetcode 6的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. JAVA 静态代码块

    特点:随着类的加载而执行,并且只会执行一次,并且还优先于主函数.作用,用于给类进行初始化 /* 静态代码块 格式: static{ 静态代码块中的执行语句 } 特点:随着类的加载而执行,并且只会执行一 ...

  2. Yii 2.0 单文件上传

    先创建一个(UploadForm.php)模型层 <?phpnamespace app\models; use yii\base\Model;use yii\web\UploadedFile; ...

  3. iOS获取电量方法

    ios简单的方法: [UIDevice currentDevice].batteryMonitoringEnabled = YES; double deviceLevel = [UIDevice cu ...

  4. Struts中<s:checkboxlist>的用法

    一.JSP中 ①集合为list <s:checkboxlist name="list" list="{'Java','.Net','RoR','PHP'}" ...

  5. ubuntu 修改静态IP和DNS

    1.修改配置文件/etc/network/interfacesroot@ubuntu:~# sudo vi /etc/network/interfaces 添加以下内容:auto eth0       ...

  6. Servlet中读取参数的几种方式

    为每一Servlet设置初始化参数 可以为每一个Servlet在对应的web.xml中的Servlet节点下编写初始化参数,格式如下: <init-param> <param-nam ...

  7. python matplotlib plot 数据中的中文无法正常显示的解决办法

    转发自:http://blog.csdn.net/laoyaotask/article/details/22117745?utm_source=tuicool python matplotlib pl ...

  8. 小知识:如何解压cpio.gz文件

    第一种方法: zcat xxxx.cpio.gz | cpio -idmv 第二种方法 :第一步: gunzip xxxx.cpio.gz第二步:cpio -idmv < xxxx.cpio # ...

  9. Android——getSystemService

    android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardServi ...

  10. cocos2d-x的环境的搭建

    1.首先提出一个我从开始接触cocosstudio和cocos2d-x的认识的误区,就是cocosstudio和cocos2d-x的区别是什么呢? cocosstudio是辅助工具,只不过它可以帮助我 ...