【leetcode】ZigZag——easy

zigzag型输出字符串。

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

思路:其实就是生成一个row*s.length()大小的二维数组,然后以w型方式去存储每一个字符,横向输出就行了。

#include <iostream>
#include<string>
using namespace std;

class Solution {
public:
    string convert(string s, int numRows) {
        string s_convert;
        int numCols=s.size();
         //二维char型数组的内存分配和初始化
        char** c_array;
        c_array=(char**)malloc(numRows*sizeof(char*));
        for(int k=0;k<numRows;k++)
        {
            c_array[k]=(char*)malloc(numCols*sizeof(char));
        }
        for(int m=0;m<numRows;m++)
            for(int n=0;n<numCols;n++)
                c_array[m][n]=‘0‘;
        //往二维数组中存入数据
        int i=0,j=0;
        int i_add=-1;
        while(j<numCols)
        {
            c_array[i][j]=s[j];
            if(numRows==1)
                j++;
            else
            {
                if(i==numRows-1||i==0)         //每次到达顶端或者底端时,i的增量取反
                    i_add=-i_add;
                i+=i_add;
                j+=1;
            }
        }
        //取出数据
        for(i=0;i<numRows;i++)
        {
            for(j=0;j<numCols;j++)
            {
                if(c_array[i][j]!=‘0‘)
                    s_convert+=c_array[i][j];
            }
            free(c_array[i]);
        }
        free(c_array);
        return s_convert;
    }
};

void main()
{
    string s="ab";
    Solution S;
    cout<<S.convert(s,1);
}
时间: 2024-10-13 01:54:38

【leetcode】ZigZag——easy的相关文章

【LeetCode】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: "PAHNA

【LeetCode】ZigZag Conversion

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

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n