LeetCode刷题-006Z字形变换

将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:
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"

 1 class Solution
 2 {
 3 public:
 4     string convert(string s, int numRows)
 5     {
 6         vector<char*> rect;
 7         int index = 0;
 8         string result;
 9         while(index<s.size())
10         {
11             char* pA = (char*) new char[numRows];
12             char* pB = (char*) new char[numRows];
13             rect.push_back(pA);
14             rect.push_back(pB);
15             for(int i=0;i<numRows;i++)
16             {
17                pA[i]=‘ ‘;
18                pB[i]=‘ ‘;
19             }
20             for(int j=0;j<numRows && index<s.size();j++)
21             {
22                pA[j]=s[index];
23                index++;
24                cout<<pA[j];
25             }
26             for(int j=numRows-2;j>0 && index<s.size();j--)
27             {
28                pB[j]=s[index];
29                index++;
30             }
31
32         }
33
34         for(int j=0;j<numRows;j++)
35         {
36             for(int i=0;i<rect.size();i++)
37             {
38                 if(rect[i][j]!=‘ ‘)
39                 result.push_back(rect[i][j]);
40             }
41         }
42
43         return result;
44     }
45 };

原文地址:https://www.cnblogs.com/nkqlhqc/p/9085343.html

时间: 2024-08-30 17:33:51

LeetCode刷题-006Z字形变换的相关文章

【leetcode刷题笔记】Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

【leetcode刷题笔记】Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半.但是如果有

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t