Skip to content

Commit 494a14a

Browse files
author
beck chen
committed
use a different approach for reverse words in a string, and correct some previous implementations
1 parent 1475e67 commit 494a14a

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

leetcode practice/array and strings/reverseWordsInAString2.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
* @param {character[]} str
1111
* @return {void} Do not return anything, modify str in-place instead.
1212
*/
13-
var reverseWords = function (str) {
14-
reverseChars(str, 0, str.length - 1);
13+
var reverseWords = function (s) {
14+
let startIndex = 0;
15+
while (s[startIndex] === ' ') {
16+
startIndex++;
17+
}
18+
19+
let endIndex = s.length - 1;
20+
while (s[endIndex] === ' ') {
21+
endIndex--;
22+
}
1523

16-
// loop from beginning of arr to second to last character,
17-
// look for start of string, and look for space, and
18-
// reverse the characters in between
19-
let wordStartIndex = 0;
20-
for (let ii = 0; ii < str.length - 1; ii++) {
21-
if (str[ii + 1] === ' ') {
22-
reverseChars(str, wordStartIndex, ii);
23-
wordStartIndex = ii + 2;
24+
let word = '';
25+
const wordsArr = [];
26+
for (let charIndex = startIndex; charIndex <= endIndex; charIndex++) {
27+
if (s[charIndex] !== ' ') {
28+
word += s[charIndex];
29+
} else if (word.length !== 0 && s[charIndex] === ' ') {
30+
wordsArr.unshift(word);
31+
word = '';
2432
}
2533
}
2634

27-
// reverse the last word
28-
reverseChars(str, wordStartIndex, str.length - 1);
29-
return str;
30-
};
35+
// dont forget about the last word
36+
wordsArr.unshift(word);
3137

32-
function reverseChars(wordsToReverseArr, firstIndex, lastIndex) {
33-
while (firstIndex < lastIndex) {
34-
[wordsToReverseArr[firstIndex], wordsToReverseArr[lastIndex]]
35-
= [wordsToReverseArr[lastIndex], wordsToReverseArr[firstIndex]];
36-
firstIndex++;
37-
lastIndex--;
38-
}
39-
}
38+
return wordsArr.join(' ');
39+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
var spiralOrder = function (matrix) {
6+
const printArr = [];
7+
if (matrix.length === 0) { return printArr; }
8+
9+
let firstRow = 0, lastRow = matrix.length - 1,
10+
firstCol = 0, lastCol = matrix[0].length - 1;
11+
12+
while (lastRow >= firstRow && lastCol >= firstCol) {
13+
// add top
14+
for (let colIndex = firstCol; colIndex <= lastCol; colIndex++) {
15+
printArr.push(matrix[firstRow][colIndex]);
16+
}
17+
18+
// add right
19+
for (let rowIndex = firstRow + 1; rowIndex <= lastRow; rowIndex++) {
20+
printArr.push(matrix[rowIndex][lastCol]);
21+
}
22+
23+
if (lastRow > firstRow && lastCol > firstCol) {
24+
// add bottom
25+
for (let colIndex = lastCol - 1; colIndex > firstCol; colIndex--) {
26+
printArr.push(matrix[lastRow][colIndex]);
27+
}
28+
29+
for (let rowIndex = lastRow; rowIndex > firstRow; rowIndex--) {
30+
printArr.push(matrix[rowIndex][firstCol]);
31+
}
32+
}
33+
34+
firstRow++;
35+
lastRow--;
36+
firstCol++;
37+
lastCol--;
38+
}
39+
40+
return printArr;
41+
}

sessions/array and strings/printElementOfMatrixInSpiralOrder.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ function printElementsInLayer(matrix, layerIndex) {
2121
firstRow = layerIndex,
2222
lastRow = matrix.length - 1 - layerIndex;
2323

24+
// in the case where we're at the center-most layer, and that layer only
25+
// has one element, we should just print it
26+
if (lastRow === layerIndex && lastColumm === layerIndex) {
27+
console.log(matrix[layerIndex][layerIndex]);
28+
return;
29+
}
30+
2431
// print top
2532
// note that this does not include the rightmost element of the first row.
2633
// that element belongs to the right column
@@ -51,4 +58,5 @@ function printElementsInLayer(matrix, layerIndex) {
5158
}
5259

5360
// this should print 2, 3, 1, 6, 7, 9, 11, 4, 8, 12, 13, 5
54-
console.log(printElementsSpiralOrder([[2, 3, 1, 6], [12, 13, 5, 7], [8, 4, 11, 9]]));
61+
// console.log(printElementsSpiralOrder([[2, 3, 1, 6], [12, 13, 5, 7], [8, 4, 11, 9]]));
62+
console.log(printElementsSpiralOrder([[6, 9, 7]]));

sessions/array and strings/rotate2DArray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function rotateMatrix(matrix) {
2020
function rotateLayerOfMatrix(matrix, layerIndex) {
2121
const startIndex = layerIndex;
2222
const endIndex = matrix.length - 1 - layerIndex; // ex. for a 5 x 5 matrix, layer 1 ends at 5 - 1 - 1 = 3
23-
for (let ii = startIndex; ii < endIndex; ii++) {
23+
for (let ii = 0; ii < endIndex - startIndex; ii++) {
2424
// store top element
2525
const storedTopElem = matrix[startIndex][startIndex + ii];
2626
// rotate left to top

0 commit comments

Comments
 (0)