LeetCode #6: Zigzag Conversion

preview_player
Показать описание
A step-by-step visualization to #LeetCode question 6: Zigzag Conversion
0:00 Problem explanation
2:22 Code walkthrough
6:23 A common mistake

#coding #programming #algorithms
Рекомендации по теме
Комментарии
Автор

i like the way you explain the solution. clear and easy to follow. thank you.

Jaczhang
Автор

Terrific explanation! I usually do not comment, but this video cleared all my doubts. Keep up the great work!

sunnysideup
Автор

I created string array and used string concatenation instead of list, no difference in time.

nagmatnazarov
Автор

Great!
Another Solution In Java:

if (numRows == 1) return s;
StringBuilder sb = new StringBuilder();
int diff = 2 * (numRows - 1);
int diagDiff = diff;
int secondIndex;
for (int i = 0; i < numRows; i++) {
int index = i;
while (index < s.length()) {
sb.append(s.charAt(index));
if (i != 0 && i != numRows - 1) {
diagDiff = diff - 2 * i;
secondIndex = index + diagDiff;
if (secondIndex < s.length()) {

}
}
index += diff;
}
}
return sb.toString();

manhnamnutrition
Автор

Another one, too verbose!

if (numRows == 1) return s;
var map = new HashMap<Integer, List<Character>>();
for (int i = 0; i < numRows; i++) map.put(i, new ArrayList<>());
int index = 0;
int step = 1;
for (int i = 0; i < s.length(); i++) {

if (index == 0) step = 1;
if (index == numRows - 1) step = -1;
index += step;
}
var sb = new StringBuilder();
for (int key: map.keySet())

return sb.toString();

manhnamnutrition