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 ...
随机推荐
- 1.运行Android Studio,一直提示:Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
1.解决问题办法:菜单栏,Tools -> Adnroid -> enable ADB integration勾上 2.暂时性的解决方案:在Android Studio中的:Prefere ...
- c语言知识(1)
用freopen重定向输入 freopen函数以指定模式重新指定到另一个文件,模式用于指定新文件的访问方式. FILE *freopen(const char * restrict filename, ...
- js swipe 图片滑动控件实现 任意尺寸适用任意屏幕
http://www.swiper.com.cn/http://www.idangero.us/swiper/demos/ 解决问题点: 1.先得到图片真实的宽高, 根据真实宽高 等比例 2.调用的控 ...
- Windows XP下安装和配置Apache2.2.22服务器+PHP5+Mysql5
原文:http://www.chinaz.com/web/2012/0516/252021.shtml 随着PHP网站的流行,国内越来越多的站长使用php开发网站或者使用相关的php开源网站(例如:D ...
- 运用BigDecimal精确计算
package com.wzh.test; import java.math.BigDecimal; public class test { /** * @param args */ public s ...
- ios8消息快捷处理——暂无输入框
if (isiOS8) { //ios8的远程推送注册 NSSet *set = nil; #if 1 //1.创建消息上面要添加的动作(按钮的形式显示出来) UIMutableUserNotific ...
- shell 外部传入jmeter脚本线程数,rampUp时间,持续运行时间
jmeter参数化部分参考上一篇 shell参数说明:$1线程数,$2:全部并发数rampup时间,$3:脚本持续运行时间,$4:每次脚本循环持续时间 $5:所以循环持续时间 #!/bin/bash ...
- protoc的protoc-gen-grpc-java插件
编译 protoc-gen-grpc-java插件 的文档在: https://github.com/grpc/grpc-java/tree/master/compiler 编译的步骤: Chang ...
- poj 1753 Flip Game
点击打开链接 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25674 Accepted: 1109 ...
- ormlite性能对比
看了一下现在的android设备,性能都不差,就懒得直接用sqlite,直接上ORM框架把,上网搜了一圈,觉得androrm, ormlite 这两个不错,当然,还有点别的,这里就不多做介绍,竟然说明 ...