LeetCode Algorithm 06

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

Tags: String

分析:

(1)此题要求返回string,可以利用string = string+char 的方式实现可变长string。

(2)此题没有要求打印出表格中的分布结构,故可以不考虑列分布,每行用一个字符串接收逐渐落在自己行内的字符。当行数i未到达最大行nRows-1时,字符串"|"分布,i++;

当行数达到nRows-1时,字符串开始"/"分布,i--。

c++代码:

 1 class Solution {
 2 public:
 3     string convert(string s, int nRows) {
 4         if (nRows <= 1)
 5             return s;
 6         vector < string > zigzag(nRows, "");
 7         //此处也可以直接用字符串数组string zigzag[nRows];来声明
 8         int len = s.length();
 9         int k = 0; //约束字符串长度
10         int i = 0; //约束行
11         while (k < len) {
12             if (i == nRows - 1) {
13                 //此处是给最后一行字符串更新
14                 zigzag[i] += s[k++];
15                 //此处接着把字符串沿"/"方向分布,直到第一行(不包括)
16                 for (i = i - 1; i > 0 && k < len; i--) {
17                     zigzag[i] += s[k++];
18                 }
19             } else {
20                 //此处把字符串沿着"|"方向分布,直到最后一行(不包括)
21                 zigzag[i] += s[k];
22                 i++;
23                 k++;
24             }
25         }
26         string result = "";
27         //跟前面一样,字符串加字符赋值自身实现可变长string效果
28         for (int i = 0; i < nRows; i++) {
29             for (int j = 0; j < zigzag[i].length(); j++) {
30                 result += zigzag[i][j];
31             }
32         }
33         return result;
34     }
35 };
时间: 2024-12-21 18:59:46

LeetCode Algorithm 06的相关文章

LeetCode Algorithm 05

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Tags: String 容易想到的思路(可以解但会超时): class Solution { public: string longestPalin

LeetCode Algorithm

原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 3.Longest Substring Without Repeating Characters 3.Longest Substring Without Repeating Characters /********************************************************** ** 3.Longest Substring Without Repeating Characters ** G

Leetcode Algorithm No.241Different Ways to Add Parentheses

题目来自Leetcode Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. Example 1 Input: "2-1-1". ((2-1)-1) = 0 (2-(1-1)

LeetCode Algorithm 04

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Tags: Divide and Conquer, Array, Binary Search 分析: 对于数字个数k,如果k为奇数,则k个数的中位数为第(k/2+1)个:对

LeetCode Algorithm 07_Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throug

LeetCode Algorithm 03

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode Algorithm 02

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 Algorithm 01

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

布线问题

组合数 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 找出从自然数1.2.... .n(0<n<10)中任取r(0<r<=n)个数的所有组合. 输入 输入n.r. 输出 按特定顺序输出所有组合. 特定顺序:每一个组合中的值从大到小排列,组合之间按逆字典序排列. 样例输入 5 3 样例输出 543 542 541 532 531 521 432 431 421 321 #include <cmath> #include <cstdio