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. localStorage本地存储的用法

    localStorage用法 if(window.localStorage){ alert('这个浏览器支持本地存储'); }else{ alert('这个浏览器支持不本地存储'); } localS ...

  2. Spring Cloud (5)hystrix 服务熔断

    1.pom文件 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...

  3. VC中链接错误,提示string重定义

    VC链接错误,说是string已经有了实现了,只要 rebuild 一下好了. Linking...LINK : warning LNK4075: ignoring '/EDITANDCONTINUE ...

  4. keal

    I remember the wonderful moment you appeared before me, like a fleeting vision, like a genius of pur ...

  5. python异常处理方法

    异常是指程序中的例外.违例情况,比如序列的下标越界.打开不存在的文件.空引用异常等.通过捕获异常并进行正确处理,可以提高程序的健壮性.如果没有代码处理异常,Python解释器将输出相关异常信息并终止程 ...

  6. LeetCode OJ 80. Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  7. [PHP]PHP的session机制,配置与高级应用

    ---------------------------------------------------------------------------------------------------- ...

  8. Git设置/取消代理

    设置代理 git config --global http.proxy http://proxy.com:1234 git config --global https.proxy http://pro ...

  9. Structs复习 Action传递参数

    Structs传递参数通常有三种方式 下面我来一个个介绍 1.属性 Jar包 web.xml <?xml version="1.0" encoding="UTF-8 ...

  10. 吴裕雄 python深度学习与实践(1)

    #coding = utf8 import threading,time count = 0 class MyThread(threading.Thread): def __init__(self,t ...