leetcode 6
题目描述:

该开始就输在了理解题意上。。 没搞懂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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 跨域请求 & jsonp
0 什么是跨域请求 在一个域名下请求另外一个域名下的资源,就是跨域请求.example 1:比如:我当前的域名是http://che.pingan.com.我现在要去请求http://www.cnbl ...
- Android Studio 系列教程(转载)
史上最详细的Android Studio系列教程一--下载和安装:http://segmentfault.com/a/1190000002401964史上最详细的Android Studio系列教程二 ...
- delphi Pointer 转成string
var s: string; p: pointer; s := PChar(p);前提p指向的字符串要以#0结尾.
- Mingyang.net:hibernate.hbm2ddl.auto配置详解【转】
原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hibernate.cfg.xml 中hibern ...
- nyoj 88 汉诺塔(一)
点击打开链接 汉诺塔(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝 ...
- 页面设计--Grid列表
Grid列表控件 功能:主要实现Html Table功能,支持多表头.固定表头.固定列.输出.组合查询.编辑功能 优点:可以快速的通过数据集合生成多表头.多样式的列表.通过简单的拖拉实现多层表头. G ...
- ubuntu实用技巧
添加alias ~/.bash_alias文件: alias go="python /Users/xhat/Downloads/goagent/local/proxy.py" ~/ ...
- getdata
public partial class GetData : System.Web.UI.Page { protected void Page_Load(object sender, EventArg ...
- dhtmlxGrid分页查询,条件查询实例
使用jquery的ajax get将页面条件请求到后台,取得数据库数据,分页查询,返回前台grid中. 引入所需文件: <script>window.dhx_globalImgPath = ...
- 《Code Complete》ch.20 软件质量概述
WHAT & WHY ? 软件质量的特性 外在特性 正确性(Correctness) 可用性(Usability) 效率(Efficiency) 可靠性(Reliability) 完整性(In ...