diff --git a/longest-substring-without-repeating-characters/njngwn.java b/longest-substring-without-repeating-characters/njngwn.java new file mode 100644 index 000000000..d31b689ba --- /dev/null +++ b/longest-substring-without-repeating-characters/njngwn.java @@ -0,0 +1,24 @@ +// Time Complexity: O(n), n: s.length() +// Space Complexity: O(n), n: s.length() +class Solution { + public int lengthOfLongestSubstring(String s) { + int begin = 0; + int end = 0; + int maxLength = 0; + + Set letterSet = new HashSet<>(); + while (end < s.length()) { + // if the letter is not in the hashset, then add it into hashset and move end pointer + if (!letterSet.contains(s.charAt(end))) { + letterSet.add(s.charAt(end)); + ++end; + maxLength = Math.max(maxLength, end - begin); // update maxlength + } else { // if the letter is in the hashset, then remove it and move begin pointer + letterSet.remove(s.charAt(begin)); + ++begin; + } + } + + return maxLength; + } +} diff --git a/number-of-islands/njngwn.java b/number-of-islands/njngwn.java new file mode 100644 index 000000000..b375a61bf --- /dev/null +++ b/number-of-islands/njngwn.java @@ -0,0 +1,30 @@ +// Time Complexity: O(m*n), m: number of row, n: number of column +// Space Complexity: O(m*n), m: number of row, n: number of column, because of call stack +class Solution { + void findLand(char[][] grid, int row, int col) { + if (row < 0 || row > grid.length-1 || col < 0 || col > grid[0].length-1) return; + if (grid[row][col] == '0') return; + + grid[row][col] = '0'; // make element '0'(water) + + findLand(grid, row-1, col); + findLand(grid, row+1, col); + findLand(grid, row, col-1); + findLand(grid, row, col+1); + } + + public int numIslands(char[][] grid) { + int num = 0; + + for (int i = 0; i < grid.length; ++i) { + for (int j = 0; j < grid[0].length; ++j) { + if (grid[i][j] == '1') { + findLand(grid, i, j); + num++; + } + } + } + + return num; + } +} diff --git a/set-matrix-zeroes/njngwn.java b/set-matrix-zeroes/njngwn.java new file mode 100644 index 000000000..b57420763 --- /dev/null +++ b/set-matrix-zeroes/njngwn.java @@ -0,0 +1,28 @@ +// Time Complexity: O(m*n) +// Space Complexity: O(m+n) +class Solution { + public void setZeroes(int[][] matrix) { + ArrayList rowList = new ArrayList<>(); + ArrayList colList = new ArrayList<>(); + + for (int m = 0; m < matrix.length; ++m) { + for (int n = 0; n < matrix[0].length; ++n) { + if (matrix[m][n] != 0) continue; + if (!rowList.contains(m)) rowList.add(m); + if (!colList.contains(n)) colList.add(n); + } + } + + for (Integer row : rowList) { + for (int n = 0; n < matrix[0].length; ++n) { + matrix[row][n] = 0; + } + } + + for (Integer col : colList) { + for (int n = 0; n < matrix.length; ++n) { + matrix[n][col] = 0; + } + } + } +} diff --git a/unique-paths/njngwn.java b/unique-paths/njngwn.java new file mode 100644 index 000000000..096dc97ee --- /dev/null +++ b/unique-paths/njngwn.java @@ -0,0 +1,19 @@ +// Time Complexity: O(m*n), m: number of row, n: number of column +// Space Complexity: O(m*n), m: number of row, n: number of column +class Solution { + public int uniquePaths(int m, int n) { + int[][] pathMap = new int[m][n]; + + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (i == 0 || j == 0) { + pathMap[i][j] = 1; + } else { + pathMap[i][j] = pathMap[i-1][j] + pathMap[i][j-1]; + } + } + } + + return pathMap[m-1][n-1]; + } +}