leetcode6. ZigZag Conversion

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

easy程度题

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"

给一个字符串,把它按Z形排列,然后按行输出。

class Solution 
{
public:
    string convert(string s, int numRows) 
    {
        string add[numRows];
        
        int j,i=0,m;
        while(i<s.size())
        {
            for(j=0;i<s.size()&&j<numRows;j++)
                add[j]+=s[i++];
            for(j=numRows-2;i<s.size()&&j>0;j--)
                add[j]+=s[i++];
        }
        
        string  result="";
        for(int i=0;i<numRows;i++)
            result+=add[i];
            
        return result;
    }
};


6. 锯齿形变换 [leetcode 6: ZigZag Conversion] 6. 锯齿形变换 [leetcode 6: ZigZag Conversion] 原题链接 https://leetcode.com/problems/zigzag-conversion 老王的解法链接 https://github.com/simplemain/leetcode/blob/master/6/analysis.md 难度 ★★☆☆☆ 标签 字符串 题目描述 给定一个字符串s和整数n... 阅读详情

相关推荐

LeetCode 6. ZigZag Conversion(Z字形变换)

题目 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 &amp;amp;amp;quot;LEETCODEISHIRING&amp;amp;amp;quot; 行数为 3 时,排列如下: L C I R E T O E S I I G E D H N 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:&amp;amp;amp

马一凡的博客 501

Leetcode 6 ZigZag Conversion

Leetcode 6 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...

博学 笃志 切问 近思 157

LeetCode 6. ZigZag Conversion Python3

LeetCode 6. ZigZag Conversion Python3 Description 点击查看题目 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 fixe...

与你的博客 316

LeetCode 6. ZigZag Conversion

LeetCode 6. ZigZag Conversion Solution1:我的答案 总是因为各种各样的傻逼错误导致不能AC,脚踏实地很重要! 这个笨方法实在是有点慢。 class Solution { public: string convert(string s, int numRows) { string res = ""; if (!s....

Allenlzcoder的博客 500

leetcode解题报告6. ZigZag Conversion

leetcode解题报告6. ZigZag Conversion题目地址 难度是medium题目描述给定一个字符串,按照zigzag的方式排,然后输出按这种方式排之后的顺序的字符串。 (比较复杂,但看例子就很明白了,具体看题目地址)我的思路刚看的时候,觉得是模拟类的题目,直接按zigzag的方式排就好了。但是,对字符串按某个方式排是比较抽象的,排成某个图案是让人来看的,但对机器或程序来说,还是以某

sysu_xiaoming的博客 465

LeetCode 6Zigzag Conversion

LeetCode 6Zigzag Conversion题目大意给定一个字符串,将其按照Z字形排列成给定行,逐行输出排列后的字符串。The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern

Cecilia_whf的博客 506

LeetCode题目:6.ZigZag Conversion

LeetCode题目:6.ZigZag Conversion 原题链接:https://leetcode.com/problems/zigzag-conversion/description/ 解题思路:对指定行row,找出逻辑,得到公式,然后进行计算即可。核心思想:对第一行和第row行,一个之字有两个字符,相邻字符的距离为2(row-1)。对第i行,一个之字有三个字符,某字符到下两个字符的距离

MyCodecOdecoDecodE的博客 373

LeetCode Notes_#6 Zigzag Conversion

LeetCode Notes_#6 Zigzag ConversionLeetCode Contents 题目思路和解答思路解答要注意的一些点参考 题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want...

baji9701的博客 190

LeetCode 6. ZigZag Conversion , Z 字形变换 C#

前言 本文介绍了 LeetCode6 题 , “ZigZag Conversion”, 也就是 “Z 字形变换” 的问题. 本文使用 C# 语言完成题目,介绍了多种方法供大家参考。 题目 English LeetCode 6. ZigZag Conversion The string “PAYPALISHIRING” is written in a zigzag pattern on a ...

wf824284257的博客 911

leetCode 6:ZigZag Conversion

leetCode 6: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 legib

tuobadon的专栏 421

Leetcode6. ZigZag Conversion

题目地址: https://leetcode.com/problems/zigzag-conversion/ 比较直观的做法是对每行分别统计,然后最后汇总。代码如下: import java.util.ArrayList; import java.util.List; public class Solution { public String convert(String s, int ...

数学、算法爱好者的博客 242

Leetcode 6ZigZag Conversion

原题链接:ZigZag Conversion   一开始的思路是根据所给字符串及 Z 行数计算出末元素所在的行列数,进而用二维数组来计算blabla…但算到后期发现不用这么复杂。。。 原理如下图 对于给定的 z 型串,我们取 2*numRows-2为一个周期(除数),以字符串 s 的长度为被除数;z 型串的第一行元素所在位置除以除数,余数皆为 1,第二行元素所在位置除以除数余数,余数为 0 或 ...

wayne17的博客 297

leetcode 6 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

大三狗现在才努力还不算晚吧?! 3344

基于Docker Compose构建个人开发者中转站:文件共享、内网穿透与轻量服务部署

在软件开发与运维实践中,容器化技术通过封装应用及其依赖,实现了环境的一致性与部署的可移植性,极大地简化了跨平台服务的交付流程。其核心原理在于利用操作系统层面的虚拟化,为每个应用提供独立的运行环境,从而解决了依赖冲突和环境差异的难题。这一技术为个人开发者和小型团队带来了显著的工程价值,使得在有限资源下快速搭建、测试和管理多种服务成为可能。典型的应用场景包括临时的文件同步与共享、内网服务的公网暴露、以及轻量级应用的原型部署。本文聚焦于如何整合Docker、Nginx Proxy Manager和File Bro

weixin_34008784的博客 388

Leetcode——6. ZigZag Conversion

题目原址 https://leetcode.com/problems/zigzag-conversion/description/ 题目描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display thi...

想当厨子的程序媛 301

Leetcode:6. ZigZag Conversion

Leetcode:6. ZigZag Conversion 参考:http://blog.csdn.net/linhuanmars/article/details/21145039#reply,找规律 public String convert(String s, int nRows) { if(s == null || s.length...

哎呀妈呀脑壳疼脑壳疼 147

leetcode6. 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 ...

zxc120389574的博客 260
上一篇: leetcode414. Third Maximum Number
下一篇: leetcode438. Find All Anagrams in a String
Coding_Reading
博客等级 码龄10年 17粉丝 66原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值