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 ...
随机推荐
- C语言小技巧
/* 求阶乘时设置最大调用层数,防止栈占满 当从函数进入另一个函数时当前函数的内容会入栈,另一个函数调用完时在出栈 */ int factorial(int n, int level) { //pri ...
- JS常用各种正则表达式
1.非负整数 /^\d+$/ 2.正整数 /^[0-9]*[1-9][0-9]*$/ 3.非正整数 /^((-\d+)|(0+))$/ 4.负整数 ...
- JAVA数据类型自动转换,与强制转换
一.数据类型自动转换 public class Test{ public static void main(String[] args){ int a = 1; double b = 1.5; dou ...
- 手动编码运行java程序
javac Server.java 编译java文件成class java Server 运行程序
- java反射1
一.Class部分 加载类的几种方式 // 1. Class clazz = Class.forName("com.wzh.test.reflect.Person"); // 2. ...
- Spring中PropertyPlaceholderConfigurer的使用
Spring中PropertyPlaceholderConfigurer的使用 在使用Spring配置获取properties文件时,在网上查到相关的资料,分享哈!! (1)获取一个配置文件 ...
- 20145305 《Java程序设计》第10周学习总结
学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时, ...
- Visual Studio 2015正式版/产品密钥 Win10正式版官方原版ISO镜像下载大全&安装激活教程
Visual Studio 2015 发行说明: https://visualstudio.com/zh-cn/news/vs2015-vs.aspx Visual Studio 2015 特性简 ...
- linux locate: command not found
装好系统发现使用locate查找文件,提示 -bash: locate: command not found yum install locate 无效,看来locate不是软件名称 [root@ce ...
- Arch-Linux-添加中文字体
代码下载: Arch-Linux-添加中文字体.txt