[算法练习]ZigZag Conversion
题目说明:
The string "PAYPALISHIRING"
is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)
should return "PAHNAPLSIIGYIR"
.
程序代码:
#include <gtest/gtest.h>
using namespace std; string convert(string s, int numRows)
{
if (s.empty() || (numRows < 2))
{
return s;
} string result;
int length = s.length();
char* data = new char[length*numRows];
memset(data,0,length * numRows); int delta = 0;
int offset = 0;
int flag = 1; for (int i= 0; i<length; ++i)
{
data[offset + delta * length] = s[i];
if ( (delta+flag) >= numRows)
{
flag = -1;
}
else if ((delta+flag) < 0)
{
flag = 1;
}
delta += flag; if (flag == -1)
{
offset ++;
}
} for (int i=0; i<numRows; ++i)
{
for (int j=0; j < length; ++j)
{
if (data[j+i*length])
result += data[j+i*length];
}
} delete data; return result;
} TEST(Pratices, tZigZagConversion)
{
// ''=> ''
// ABCDEFG 3 => A E => AEBDFCG
// B D F
// C G ASSERT_EQ(convert("",1),"");
ASSERT_EQ(convert("ABCDEFG",1),"ABCDEFG");
}
[算法练习]ZigZag Conversion的更多相关文章
- LeetCode: ZigZag Conversion 解题报告
ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
随机推荐
- 浅谈postMessage跨域通信与localStorage实现跨域共享
https://www.cnblogs.com/tyrion1990/p/8134384.html
- JMeter知识点整理
- ui4-5
2016PS第4-5周 图像的高级编辑方法: 4-1.用变换将照片放入相框 1.打开素材文件01-1.jpg 2.执行:文件/置入,选素材01-2.jpg 3.执行:编辑/变换/缩放,缩小照片,暂不退 ...
- jedis 整合 Spring
一, 单机版整合spring 1. 使用默认配置文件进行配置 <bean id="redisClient" class="redis.clients.jedis ...
- lambda modern C++
Lambda expressions (since C++11) Syntax [ captures ] <tparams>(optional)(c++20) ( params ) s ...
- eclipse中提示js或者JQuery代码
当你在eclipse中的JSP中写JavaScript或者JQuery代码的时候,eclipse是不会自动提示的,所以你需要在eclipse中安装一下插件,该插件的名字叫:Spket IDE,它可以作 ...
- JavaScript设计模式-15.适配器模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 关于js语法(运算中出现无限大的问题)本身的错误的解决方案
错误原因: 一是 JavaScript 浮点数计算的 Bug, 另一个是和计算机最终转换成二进制计算有关系 解决方案: 第一种就是利用JavaScript 的toFixed(n) 方法,直接获取N 位 ...
- How can I set ccshared=-fPIC while executing ./configure?
解决方式如下: make clean ./configure CFLAGS=-fPIC CXXFLAGS=-fPIC
- FocusBI:地产分析&雪花模型
微信公众号:FocusBI关注可了解更多的商业智能.数据仓库.数据库开发.爬虫知识及沪深股市数据推送.问题或建议,请关注公众号发送消息留言;如果你觉得FocusBI对你有帮助,欢迎转发朋友圈或在文章末 ...