日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[LeetCode]ZigZag Conversion

發(fā)布時(shí)間:2025/7/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [LeetCode]ZigZag Conversion 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:ZigZag Conversion

一串字符按照Z(yǔ)字形的數(shù)組給了我們,要求轉(zhuǎn)成原本的順序。

思路:

統(tǒng)計(jì)“|/”的個(gè)數(shù);

豎著的和斜著的下標(biāo)有對(duì)應(yīng)關(guān)系;

豎著的:k = j*(2*numRows - 2) + i;

斜著的(不含兩個(gè)端點(diǎn)):k = (j + 1)*(2*numRows - 2) - i;

注意:可能會(huì)有殘缺的部分。

/****************************************************************** 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 <stdio.h> #include <memory.h>char* convert(char* s, int numRows) {int length = strlen(s);printf("%d\n",length);if(length <= numRows || numRows <= 1)return s;int zn = length/(2*numRows - 2);//Z字形豎著放時(shí),最開始的一豎和一撇的組合整體的個(gè)數(shù)int sn = length%(2*numRows - 2);//Z字形豎著放時(shí),剩下殘缺的組合的字母?jìng)€(gè)數(shù)char *cs = (char *)malloc((length + 1)*sizeof(char));memset(cs,0,(length + 1)*sizeof(char));int index = 0,k = 0;for (int i = 0;i < numRows;i++){//完整組合的對(duì)應(yīng)轉(zhuǎn)換for(int j = 0;j < zn;j++){//豎線上的點(diǎn)坐標(biāo)對(duì)應(yīng)公式k = j*(2*numRows - 2) + i;cs[index++] = s[k];if(i > 0 && i < numRows - 1){//斜線上的不含兩端點(diǎn)的點(diǎn)坐標(biāo)對(duì)應(yīng)公式k = (j + 1)*(2*numRows - 2) - i;cs[index++] = s[k];}}if(sn > i){//殘缺組合的對(duì)應(yīng)轉(zhuǎn)換k = zn*(2*numRows - 2) + i;cs[index++] = s[k];if(i > 0 && i < numRows - 1 && sn > 2*numRows - 2 - i){k = (zn + 1)*(2*numRows - 2) - i;cs[index++] = s[k];}}}return cs; }void main(){char s[] = "Apalindromeisaword,phrase,number,orothersequenceofunitsthatcanbereadthesamewayineitherdirection,withgeneralallowancesforadjustmentstopunctuationandworddividers.";char *cs = convert(s,2);printf("%s\n",cs);free(cs); }

?

轉(zhuǎn)載于:https://www.cnblogs.com/yeqluofwupheng/p/6679118.html

總結(jié)

以上是生活随笔為你收集整理的[LeetCode]ZigZag Conversion的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。