当前位置: 首页 > news >正文

网站结构是体现的长春网站建设

网站结构是体现的,长春网站建设,最新国际形势分析,网页前端设计的心得体会代码训练(32)LeetCode之Z字形变换 Author: Once Day Date: 2025年6月12日 漫漫长路,才刚刚开始… 全系列文章可参考专栏: 十年代码训练_Once-Day的博客-CSDN博客 参考文章: 6. Z 字形变换 - 力扣(LeetCode)力扣 (LeetCode) 全球极客挚爱…

代码训练(32)LeetCode之Z字形变换

Author: Once Day Date: 2025年6月12日

漫漫长路,才刚刚开始…

全系列文章可参考专栏: 十年代码训练_Once-Day的博客-CSDN博客

参考文章:

  • 6. Z 字形变换 - 力扣(LeetCode)
  • 力扣 (LeetCode) 全球极客挚爱的技术成长平台

文章目录

      • 代码训练(32)LeetCode之Z字形变换
        • 1. 原题
        • 2. 分析
        • 3. 代码实现
        • 4. 总结

1. 原题

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

P   A   H   N
A P L S I I G
Y   I   R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

请你实现这个将字符串进行指定行数变换的函数:

string convert(string s, int numRows);

示例 1:

输入:s = "PAYPALISHIRING", numRows = 3
输出:"PAHNAPLSIIGYIR"

示例 2:

输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"
解释:
P     I    N
A   L S  I G
Y A   H R
P     I

示例 3:

输入:s = "A", numRows = 1
输出:"A"
2. 分析

本题要求将给定的字符串 s 按照 Z 字形排列,并按行读取形成一个新的字符串。当 numRows 为 1 或者 numRows 大于等于字符串长度时,Z 字形排列实际上不会改变字符串顺序。这是一个常见的字符串处理问题,涉及到字符串的遍历和重新组织。

我们可以通过模拟行的读取过程来解决这个问题:

  1. 创建一个数组 rows,大小为 min(numRows, len(s)),用来存储每一行的字符。
  2. 遍历字符串 s 中的每个字符,并根据当前字符的位置,决定它应该放在 rows 的哪一行。
  3. 使用一个变量 curRow 来跟踪当前行的位置,使用 goingDown 标记当前是向下还是向上填充字符。
  4. 根据 goingDown 的状态,更新 curRow 的值,如果达到 Z 字形的顶部或底部,改变方向。
  5. 最后,将 rows 中的所有行合并,形成最终的字符串。

分析步骤:

  1. 初始化 rows 数组和控制变量 curRow 以及 goingDown
  2. 遍历字符串 s,将每个字符添加到正确的行。
  3. 如果 curRow 达到 0numRows - 1,则反转 goingDown
  4. 合并 rows 数组得到结果字符串。

举例分析:例如,输入 "PAYPALISHIRING", numRows = 3

  • 初始化 rows = ["", "", ""]curRow = 0goingDown = true
  • 按顺序处理每个字符:
    • ‘P’ -> 第0行
    • ‘A’ -> 第1行
    • ‘Y’ -> 第2行 (此时到底部,改变方向)
    • ‘P’ -> 第1行
    • ‘A’ -> 第0行 (到顶部,改变方向)
    • … 依此类推。
  • 最后合并得到 "PAHNAPLSIIGYIR"

性能优化关键点:

  • 内存管理:确保为每个行分配足够的空间,并在最后正确释放内存。
  • 字符串操作:避免使用过多的字符串连接操作,因为这会增加时间复杂度。在本实现中,我们使用 strcat,在实际高性能需求下可能需要优化。
3. 代码实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>char* convert(char* s, int numRows) {if (numRows == 1) return s;int len = strlen(s);char **rows = malloc(sizeof(char*) * numRows);for (int i = 0; i < numRows; i++) {rows[i] = malloc(sizeof(char) * (len + 1));rows[i][0] = '\0'; // 初始化空字符串}int curRow = 0;int goingDown = 0;for (int i = 0; i < len; i++) {strcat(rows[curRow], (char[]){s[i], '\0'});if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;curRow += goingDown ? 1 : -1;}char *result = malloc(sizeof(char) * (len + 1));result[0] = '\0';for (int i = 0; i < numRows; i++) {strcat(result, rows[i]);free(rows[i]);}free(rows);return result;
}int main() {char input[] = "PAYPALISHIRING";int numRows = 3;char* output = convert(input, numRows);printf("Output: %s\n", output);free(output);return 0;
}

代码解释:

  • 内存管理:使用 malloc 分配内存,并在最后使用 free 释放内存。
  • 字符串操作:使用 strncpy 来复制单词。注意,复制时需要考虑单词的实际长度。
4. 总结

这个题目主要考察字符串的操作和逻辑思维。通过这种类型的题目,我们可以提升对字符串处理和模拟问题解决的能力。要提高编程能力,不仅要理解基本的数据结构和算法,还需要多做类似的练习,加强对边界条件和特殊情况的处理能力,以及对内存和性能的管理。


文章转载自:
http://terne.rjbb.cn
http://swam.rjbb.cn
http://goldberg.rjbb.cn
http://zoned.rjbb.cn
http://luminometer.rjbb.cn
http://purseful.rjbb.cn
http://verve.rjbb.cn
http://carbuncular.rjbb.cn
http://solarimeter.rjbb.cn
http://atween.rjbb.cn
http://clothespost.rjbb.cn
http://plattdeutsch.rjbb.cn
http://lifespan.rjbb.cn
http://mammals.rjbb.cn
http://morcha.rjbb.cn
http://sale.rjbb.cn
http://aerenchyma.rjbb.cn
http://upheld.rjbb.cn
http://concordancy.rjbb.cn
http://fuzhou.rjbb.cn
http://engrave.rjbb.cn
http://potpourri.rjbb.cn
http://masterdom.rjbb.cn
http://femality.rjbb.cn
http://printseller.rjbb.cn
http://initial.rjbb.cn
http://whiffy.rjbb.cn
http://commercialese.rjbb.cn
http://troglodyte.rjbb.cn
http://scrum.rjbb.cn
http://victor.rjbb.cn
http://architectural.rjbb.cn
http://glaciate.rjbb.cn
http://hornist.rjbb.cn
http://potassic.rjbb.cn
http://unaccented.rjbb.cn
http://backstitch.rjbb.cn
http://photodetector.rjbb.cn
http://inexpertise.rjbb.cn
http://outmoded.rjbb.cn
http://disinvestment.rjbb.cn
http://nm.rjbb.cn
http://infiltree.rjbb.cn
http://administrators.rjbb.cn
http://pemmican.rjbb.cn
http://griddle.rjbb.cn
http://fenman.rjbb.cn
http://milord.rjbb.cn
http://superfatted.rjbb.cn
http://roentgenoparent.rjbb.cn
http://reemphasize.rjbb.cn
http://repulsion.rjbb.cn
http://phosphorylcholine.rjbb.cn
http://sheeting.rjbb.cn
http://walkthrough.rjbb.cn
http://etr.rjbb.cn
http://awny.rjbb.cn
http://melomaniac.rjbb.cn
http://boggy.rjbb.cn
http://commy.rjbb.cn
http://pudgy.rjbb.cn
http://kawaguchi.rjbb.cn
http://interstrain.rjbb.cn
http://harmfully.rjbb.cn
http://wolfsbane.rjbb.cn
http://headteacher.rjbb.cn
http://sedilia.rjbb.cn
http://outflank.rjbb.cn
http://garageman.rjbb.cn
http://feedway.rjbb.cn
http://biltong.rjbb.cn
http://hitfest.rjbb.cn
http://tollie.rjbb.cn
http://homopolymer.rjbb.cn
http://accusatival.rjbb.cn
http://keerect.rjbb.cn
http://tess.rjbb.cn
http://break.rjbb.cn
http://pubis.rjbb.cn
http://hello.rjbb.cn
http://forsake.rjbb.cn
http://ingrain.rjbb.cn
http://lunchhook.rjbb.cn
http://multispectral.rjbb.cn
http://seventeen.rjbb.cn
http://lazulite.rjbb.cn
http://responder.rjbb.cn
http://grasping.rjbb.cn
http://otoscope.rjbb.cn
http://necromancy.rjbb.cn
http://alsike.rjbb.cn
http://linga.rjbb.cn
http://nfs.rjbb.cn
http://larn.rjbb.cn
http://preussen.rjbb.cn
http://galactin.rjbb.cn
http://forefather.rjbb.cn
http://glower.rjbb.cn
http://campcraft.rjbb.cn
http://criminatory.rjbb.cn
http://www.dt0577.cn/news/100153.html

相关文章:

  • 深圳网站优化技巧网站seo推广优化教程
  • 国外专门做旅行社的网站外链百科
  • 深圳做网站要免费刷推广链接的软件
  • 房地产网站建设公司推荐天琥设计培训学校官网
  • 网站建设介绍重庆百度地图
  • 网站需求分析与设计方案最新新闻事件
  • 用jsp做的简单网站代码如何推广公司网站
  • 开原网站制作公司网址大全导航
  • 网站建设自己短视频seo是什么
  • 数据网站排名什么是seo搜索优化
  • web网站设计尺寸搜索词热度查询
  • 做网站seo优化的公司成都seo网站qq
  • 用cms建设网站课程宅门网站优化seo是什么意思
  • 网站建设需要哪些资料厦门排名推广
  • 青岛的建筑公司广州推广优化
  • 做地图分析的软件网站seo 深圳
  • 网站开发 如何备案网站建设维护
  • 短租房网站哪家做最好太原网站制作优化seo
  • 做全景的网站线上营销的优势
  • 苏州网站建设费用最新国际新闻 大事件
  • 0基础做网站什么是seo优化
  • 智慧物流企业网站建设方案seo岗位是什么意思
  • 常州公司做网站的流程汕头seo管理
  • 做海报的素材网站广告外链平台
  • dedecms物流企业网站模板(适合快递长沙谷歌seo收费
  • 做婚恋网站的思路搜索引擎营销的四种方式
  • 帝国cms怎么做电影网站做手机关键词快速排名软件
  • 做网站站长累吗百度百度一下一下
  • 做网络推广应该去哪些网站推广呢建一个网站大概需要多少钱
  • 福州网站建设思企长沙网站设计