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

思路:Zigzag,一列长,一列短(少两个字符),长的那列由上到下写,短的由下到上写。

char* convert(char* s, int numRows) {
int len = strlen(s);
if( == len) return ""; char g[numRows][len+]; //+1 for '\0'
int p = ; //pointer to s
int i = ; //line number
int j[numRows]; //column number
for(i = ; i < numRows; i++){
j[i] = ;
} while(s[p]!='\0'){
for(i = ; i < numRows; i++){
if(s[p]=='\0') break;
g[i][j[i]++] = s[p++];
}
for(i = numRows-; i >= ; i--){
if(s[p]=='\0') break;
g[i][j[i]++] = s[p++];
}
} g[][j[]] = '\0';
for(i = ; i < numRows; i++){
g[i][j[i]] = '\0';
if(g[i][]!='\0') strcat(g[], g[i]);
} //g[0] is saved in stack, which will be deleted when leave the function so we should copy it before leave
char* ret = malloc(sizeof(char)*(len+)); //saved in heap, which won't be disappeared when leave the function
memcpy(ret, g[], sizeof(char)*(len+));
return ret;
}

6. ZigZag Conversion (字符串的连接)的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. [leetcode]6. ZigZag Conversion字符串Z形排列

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  3. Leetcode 6 ZigZag Conversion 字符串处理

    题意:将字符串排成Z字形. PAHNAPLSIIGYIR 如果是5的话,是这样排的 P     I AP   YR H L G N  SI A    I 于是,少年少女们,自己去找规律吧 提示:每个Z ...

  4. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  5. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  6. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  7. Q6:ZigZag Conversion

    6. ZigZag Conversion 官方的链接:6. ZigZag Conversion Description : The string "PAYPALISHIRING"  ...

  8. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  9. Java [leetcode 6] ZigZag Conversion

    问题描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  10. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

随机推荐

  1. leetcode1024

    class Solution(object): def videoStitching(self, clips: 'List[List[int]]', T: int) -> int: li = s ...

  2. SecureCRT显示乱码的解决办法

    下面来看看SecureCRT的显示出现乱码这种情况.比如: 现在我们重新设置一下 设置下图中的配置 1.选择字符编码为UTF-8. 2.设置字符集为GB2312后保存好后确认退出. 3.再次测试一下.

  3. inno setup 安装前判断进程是否存在,以及停止相应进程<转>

    打包的时候遇到了这样的需求:假似用户都是傻瓜                  式操作,如果更新安装程序的时候,之前的老程序还在运行这个时候如果你去提示让用户吧老程序手动退掉也不现实. 所以当遇到这种 ...

  4. linux 不同服务器之间复制文件

    ----------------------拷贝文件夹---------------------------------------------- 把当前文件夹tempA拷贝到 目标服务器10.127 ...

  5. ssm框架使用jsp提交表单到controller

    jsp代码: controller代码:

  6. windows下面的java项目打成jar放到XShell终端上面进行远程调试

    前言: java项目打成jar放到linux上面运行,但是linux上面没有eclipse不能进行debug,所以要在windows的eclipse中进行远程调试. 需要注意的是!!!-->在e ...

  7. ubuntu卸载福昕阅读器

    在安装目录找到maintenancetool.sh运行之 ~/opt/foxitsoftware/foxitreader

  8. spring Boot 上传文件,10天后,不能上传的bug

    起因 公司研发人员 部署服务在阿里云 ecs 服务器; 上传文件过1周左右文件自动丢失; 排查思路: (1).查询tomcat 启动日志出现如下信息: java.io.IOException: The ...

  9. Python文件夹与文件的操作(转)

    最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...

  10. linux shell实现 URL 编码/解码方法

    (1)编码的两种方法 # echo '手机' | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' # echo '手机' |tr -d '\n' |od ...