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 ZigZag ConversionLeetCode
- Leetcode 6 ZigZag ConversionLeetCode
- ZigZag Conversion leetcode javaLeetCodeJava
- LeetCode 6. ZigZag ConversionLeetCode
- LeetCode OJ : 6 ZigZag ConversionLeetCode
- (字串)ZigZag Conversion字串
- LeetCode ZigZag Conversion(006)解法總結LeetCode
- LeetCode6: ZigZag Conversion(Z字形變換)LeetCode
- t6
- Leetcode Binary Tree Zigzag Level Order TraversalLeetCode
- Leetcode-binary Tree Zigzag Level Order TraversalLeetCode
- [LeetCode] ZigZag Converesion 之字型轉換字串LeetCode字串
- Binary Tree ZigZag Level Order Traversal leetcode javaLeetCodeJava
- BITMAP CONVERSION TO ROWIDS
- zigzag走線原理及應用
- bitmap conversion from rowids
- what is conversion exit defined in ABAP domainAI
- Again, a chinese char conversion problemAI
- WoW - Today last chance for conversion AccountAST
- Conversion to Dalvik format failed: Unable to execute dexORMAI
- ADC and DAC Analog Filters for Data ConversionFilter
- PostgreSQL DBA(98) - PG 12 Faster float conversion to textSQLAST
- BITMAP CONVERSION FROM ROWIDS 適用場景
- oracle sql hint提示_BITMAP CONVERSION FROM ROWIDSOracleSQL
- 如何使用 SAP CDS view 中的 currency conversion 功能View
- invalid conversion from ‘LRUCache*‘ to ‘int‘ [-fpermissive] /new的使用
- 單位轉換函式: MATERIAL_UNIT_CONVERSION函式
- View and Data API Tips : Conversion between DbId and nodeViewAPI
- 研究 Protobuf 時發現一個挺好的演算法 — ZigZag演算法
- PSQLException: ERROR: failed to find conversion function from unknown to charSQLExceptionErrorAIFunction
- laravel模型查詢的時候報 Array to string conversionLaravel模型
- NUMBER BASE CONVERSION(進位制轉換) 經典模擬
- 一文搞懂 ZigZag 演算法及 Go 語言的實現演算法Go
- [Android] Eclipse錯誤:Conversion to Dalvik format failed with error 1AndroidEclipseORMAIError
- BW Conversion Routine 探究以及例項操作和異常處理
- 執行計劃出現BITMAP CONVERSION TO ROWIDS的解釋!
- log4j2 ERROR StatusLogger Unrecognized conversion specifier解決ErrorZed
- CONVERSION_EXIT_ALPHA_INPUT 加前導0 和去掉 前導0