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 T6 ZigZag ConversionLeetCode
- LeetCode6: ZigZag Conversion(Z字形變換)LeetCode
- LeetCode ZigZag Conversion(006)解法總結LeetCode
- 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
- Leetcode——6. Z 字形變換LeetCode
- LeetCode 6.Z字形變換LeetCode
- LeetCode 2024/6 每日一題 合集LeetCode每日一題
- 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
- LeetCode-6. Z字形變換(找規律)LeetCode
- laravel模型查詢的時候報 Array to string conversionLaravel模型
- 研究 Protobuf 時發現一個挺好的演算法 — ZigZag演算法
- NUMBER BASE CONVERSION(進位制轉換) 經典模擬
- log4j2 ERROR StatusLogger Unrecognized conversion specifier解決ErrorZed
- JPEG格式研究——(4)反量化、逆ZigZag變化和IDCT變換
- 2024/12/6 【雜湊表】LeetCode1.兩數之和 【√】LeetCode
- DcatAdmin 多對多關聯是,multipleSelect 報錯 Array to string conversion
- 一文搞懂 ZigZag 演算法及 Go 語言的實現演算法Go
- Spring 異常關鍵字 no matching editors or conversion strategy found 解決方法Spring
- 從0打卡leetcode之day 6--最長迴文串LeetCode
- 【LeetCode】如何學習LeetCode?LeetCode
- [Leetcode]315.計算右側小於當前元素的個數 (6種方法)LeetCode
- leetcodeLeetCode
- LeetCode in actionLeetCode
- leetcode 238LeetCode
- 微軟面試題: LeetCode 151. 翻轉字串裡的單詞 出現次數:6微軟面試題LeetCode字串
- LeetCode 164 最大間距 HERODING的LeetCode之路LeetCode
- LeetCode 143 重排連結串列 HERODING的LeetCode之路LeetCode
- LeetCode問題LeetCode
- 【LeetCode】Jewels and StonesLeetCode
- Leetcode 513 javascriptLeetCodeJavaScript