- 浏览: 189467 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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".
这道题目就是一个找规律的题目,没有其他的方法,题目已经给出nRows = 3的情况,我们可以再写出nRows = 4时的情况。代码如下:
上面的方法,每一行都要遍历一遍字符串中的字符,我们通过观察可以发现,第一行和最后一行的下一个元素都是加上一个next,中间的行在每一个next前面多了一个元素,每次加入next之前单独处理就可以了,这样只需要遍历一遍。代码如下:
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".
这道题目就是一个找规律的题目,没有其他的方法,题目已经给出nRows = 3的情况,我们可以再写出nRows = 4时的情况。代码如下:
public class Solution { public String convert(String s, int numRows) { if(numRows == 1) return s; StringBuilder sb = new StringBuilder(); int next = 2 * numRows - 2; int mod = 0; while(mod < numRows) { for(int i = 0; i < s.length(); i++) { if(i % next == mod || i % next == next - mod) { sb.append(s.charAt(i)); } } mod ++; } return sb.toString(); } }
上面的方法,每一行都要遍历一遍字符串中的字符,我们通过观察可以发现,第一行和最后一行的下一个元素都是加上一个next,中间的行在每一个next前面多了一个元素,每次加入next之前单独处理就可以了,这样只需要遍历一遍。代码如下:
public class Solution { public String convert(String s, int numRows) { if(s == null || s.length() == 0) return ""; if(numRows == 1) return s; int next = 2 * numRows - 2; char[] c = new char[s.length()]; int index = 0; for(int i = 0; i < numRows; i++) { for(int j = i; j < s.length(); j += next) { c[index++] = s.charAt(j); if(i != 0 && i != numRows - 1 && j + next - i * 2 < s.length()) c[index++] = s.charAt(j + next - i * 2); } } return new String(c); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 293Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 300You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 419Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 400Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 531Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 610Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 506Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 705Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 503The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 458Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 618Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 635Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 458All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 929Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 955Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 630Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 723Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 907Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 814You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 754For a undirected graph with tre ...
相关推荐
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) Java AC版本
在“C语言-leetcode题解之06-zigzag-conversion.c”这一文章中,作者将详细剖析LeetCode平台上编号为6的题目,也就是“ZigZag Conversion(之字形转换)”问题,并提供一个用C语言实现的解决方案。 ZigZag ...
本篇将要讨论的题目是LeetCode的第6题“ZigZag Conversion”。这是一个字符串操作问题,其主要目的是将给定的字符串按照一种特殊的“之”字形排列方式进行转换。具体来说,例如输入字符串“PAYPALISHIRING”和数字3...
Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop ...
c c语言_leetcode 0006_zigzag_conversion.zip
java入门 java_leetcode题解之006_ZigZag_Conversion
* ZigZag转换(ZigZag Conversion):将字符串转换为ZigZag格式。 * 回文数(Palindrome Number):判断数字是否为回文数。 3. 数学运算: * 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序...
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 ...
本题解针对LeetCode中的第6题——"Z字形变换"(也称作“ZigZag Conversion”),这是一个关于字符串处理的题目,对于理解字符串操作和逻辑思维有着很好的锻炼作用。 "Z字形变换"的题目要求是将一个给定的字符串按照...
6. **ZigZag Conversion** (字符串操作) - 题目描述:将英文按照ZigZag顺序排列。 - 解决方案:根据行数和当前位置的奇偶性决定字符是向左还是向右移动。 7. **String to Integer (atoi)** (字符串转整数/数学) ...
本资源是关于LeetCode第6题的C#解决方案,这道题被称为“Z字形变换”(也称为“ZigZag Conversion”)。接下来,我们将深入探讨这个问题以及如何使用C#解决它。 **问题描述:** “Z字形变换”是将一个给定的字符串...
本主题聚焦于LeetCode的第6题,名为“Z字形变换”(也称为“ZigZag Conversion”)。这是一道关于字符串处理的题目,它要求我们将一个给定的字符串按照特定的Z字形模式进行排列。 首先,我们来理解一下“Z字形变换...
- ZigZag Conversion(Z字形变换):将字符串“PAYPALISHIRING”按照Z字形排列。 - String to Integer (atoi)(字符串转换整数):将字符串转换成整数。 题目难度的标注是按照po主的主观判断来完成的,可能会有一些...
- ZigZag Conversion(Z字形转换):涉及对字符串按照特定模式进行重新排列的问题。 - Compress a String(字符串压缩):编写一个方法来压缩字符串。 - Valid Parentheses(有效的括号):检查字符串内的括号是否...
6._ZIGZAG CONVERSION_(Z字形转换) 题目描述:给定一个字符串和行数,将其转换为Z字形。 知识点:字符串、数组 思路:使用数组记录每行的元素,遍历字符串,根据行数和索引将元素填充到数组中。 7._REVERSE ...
Conversion,则可以生成一个空的测试文件以及一个模板代码文件ZigZag_Conversion.cpp,cpp的内容如下: #include "comm_header.h" int main() { int test_cases = input(); for (int i = 1; i <= test_cases; +...
6. **ZigZag Conversion** (Medium): 将直行文本转换为锯齿形排列。可以使用两指针分别控制行和列的变化来实现。 7. **Reverse Integer** (Easy): 反转一个整数。需要注意整数溢出的问题,可以使用数学方法避免。 ...
6. ZigZagConversion: 字符串按Z字形排列后给定行号,要求输出该行的字符,这涉及到字符串操作和索引计算。 7. ReverseInteger: 需要将一个整数反转,同时要考虑到反转后的整数是否超出整型的范围。 8. ...
6. ZigZag Conversion:将字符串按Z字形排列,输出按行读取的字符串。这个问题可以通过模拟Z字形的方式来解决,也可以通过数学的方式确定每个字符应该处于哪一个行。 7. Reverse Integer:这题要求将一个整数反转,...
6. ZigZag Conversion(Z字形变换) 知识点:字符串操作 将一个字符串按照Z字形的方式写入给定行数的矩阵中,然后按行读取。这个问题的难点在于找到正确的索引映射关系,以确定在转换过程中字符应该放在哪里。 7....