LeetCode T6 ZigZag Conversion
題目地址:
中文:https://leetcode-cn.com/problems/zigzag-conversion/
英文:https://leetcode.com/problems/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: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Example 3:
Input: s = "A", numRows = 1
Output: "A"
Constraints:
1 <= s.length <= 1000
s consists of English letters (lower-case and upper-case), ‘,’ and ‘.’.
1 <= numRows <= 1000
思路:
PS:發現在Markdown中用程式碼塊表示題目描述中的樣例比引用好,因為程式碼塊不會吞格式
是道找規律題
這道題要把一個字串按照Z字形擺放,是倒著的Z,準確說應該是映象的N字形,按照這個擺放順序輸出,然後其實我們把輸出順序寫出來,發現會有多個映象N形,也就是說,每個映象N形是一個週期,其實每個週期並不是一個完整的N形,下面舉例說明:
以numRows為4為例
0 6
1 5 7
2 4 8
3 9
一個週期應該是0到5,可以說是一個V字形一個週期,可以發現0和下一週期對應的6間隔是2*(numRows-1),第一個週期0到5的每個數字和下一個週期都間隔5。然後看第一個週期,因為我們要按照行輸出,所以應該一行一行地看,我們發現,第一行0和6間隔2*(numRows-1),第二行的1和5間隔2*(numRows-1-i)這裡的i是指行數(從0開始),然後發現其實第三行,第四行也是如此,這樣利用這個規律就解出這道題了。
題解:
class Solution {
public static String convert(String s, int numRows) {
if(numRows==1) return s;
String res="";
res += s.charAt(0);
int j=0;
int t = numRows-1;
for(int i=0;i<numRows;i++){//對每一行
while(j<s.length()){
//如果不是第一行和最後一行,算兩個
if(i % t != 0 && (t-i)!=0 &&(i+j)<s.length()) res += s.charAt(i+j);
if((i+j+2*(t-i))<s.length()) res += s.charAt(i+j+2*(t-i));
j += 2*t;
}
j=0;
}
return res;
}
}
相關文章
- Leetcode 6 ZigZag ConversionLeetCode
- LeetCode 6. ZigZag ConversionLeetCode
- LeetCode ZigZag Conversion(006)解法總結LeetCode
- LeetCode6: ZigZag Conversion(Z字形變換)LeetCode
- t6
- zigzag走線原理及應用
- what is conversion exit defined in ABAP domainAI
- 103. Binary Tree Zigzag Level Order Traversal
- Conversion to Dalvik format failed: Unable to execute dexORMAI
- PostgreSQL DBA(98) - PG 12 Faster float conversion to textSQLAST
- invalid conversion from ‘LRUCache*‘ to ‘int‘ [-fpermissive] /new的使用
- PSQLException: ERROR: failed to find conversion function from unknown to charSQLExceptionErrorAIFunction
- 2018-10-13 21:30:51 conversion of number systems
- 如何使用 SAP CDS view 中的 currency conversion 功能View
- laravel模型查詢的時候報 Array to string conversionLaravel模型
- 研究 Protobuf 時發現一個挺好的演算法 — ZigZag演算法
- NUMBER BASE CONVERSION(進位制轉換) 經典模擬
- log4j2 ERROR StatusLogger Unrecognized conversion specifier解決ErrorZed
- JPEG格式研究——(4)反量化、逆ZigZag變化和IDCT變換
- DcatAdmin 多對多關聯是,multipleSelect 報錯 Array to string conversion
- 一文搞懂 ZigZag 演算法及 Go 語言的實現演算法Go
- Spring 異常關鍵字 no matching editors or conversion strategy found 解決方法Spring
- 【LeetCode】如何學習LeetCode?LeetCode
- leetcodeLeetCode
- LeetCode in actionLeetCode
- leetcode 238LeetCode
- LeetCode 164 最大間距 HERODING的LeetCode之路LeetCode
- LeetCode 143 重排連結串列 HERODING的LeetCode之路LeetCode
- LeetCode問題LeetCode
- 【LeetCode】Jewels and StonesLeetCode
- Leetcode 513 javascriptLeetCodeJavaScript
- LeetCode 162 JavascriptLeetCodeJavaScript
- Leetcode 921 JavascriptLeetCodeJavaScript
- [Leetcode] Edit DistanceLeetCode
- LeetCode 克隆圖LeetCode
- LeetCode #378 JavaScriptLeetCodeJavaScript
- LeetCode 747 JavaScriptLeetCodeJavaScript
- Leetcode725LeetCode