Leetcode 6 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
class Solution {
public String convert(String s, int numRows) {
if(s == null || s.length() == 0 || numRows <= 1){
return s;
}
int len = s.length();
int circle = 2 * numRows - 2;
StringBuilder[] sc = new StringBuilder[numRows];
for (int i = 0; i < sc.length; i++)
sc[i] = new StringBuilder();
for(int i = 0 ; i < len; i++){
int num = i % circle;
if(num < numRows){
char c = s.charAt(i);
sc[num].append(c);
}else{
sc[circle - num].append(s.charAt(i));
}
}
StringBuilder ss = new StringBuilder();
for(int i = 0 ; i < numRows ; i++){
ss.append(sc[i]);
}
return ss.toString();
}
}
時間複雜度為O(n),思路是借鑑的牛客上面的,很奇妙,自己手動擼了一遍基本掌握-
相關文章
- LeetCode 6. ZigZag ConversionLeetCode
- LeetCode OJ : 6 ZigZag ConversionLeetCode
- LeetCode T6 ZigZag ConversionLeetCode
- leetcode ZigZag ConversionLeetCode
- ZigZag Conversion leetcode javaLeetCodeJava
- LeetCode6: ZigZag Conversion(Z字形變換)LeetCode
- (字串)ZigZag Conversion字串
- LeetCode ZigZag Conversion(006)解法總結LeetCode
- 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
- Leetcode——6. Z 字形變換LeetCode
- LeetCode 6.Z字形變換LeetCode
- 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
- LeetCode 2024/6 每日一題 合集LeetCode每日一題
- 如何使用 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演算法
- LeetCode-6. Z字形變換(找規律)LeetCode
- 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 探究以及例項操作和異常處理