Skip to content

[sora0319] Week 07 solutions #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions longest-substring-without-repeating-characters/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> characters = new HashMap<>();
int maxLength = 0;
int length = 0;
int start = 0;

for(int i = 0; i < s.length(); i++){
if(!characters.containsKey(s.charAt(i))){
characters.put(s.charAt(i), i);
length++;
}
else{
maxLength = Math.max(length, maxLength);

int place = characters.get(s.charAt(i));
if(place < start){
characters.put(s.charAt(i), i);
length++;
continue;
}

length = i - place;
start = place + 1;
characters.put(s.charAt(i), i);
}
}
maxLength = Math.max(length, maxLength);

return maxLength;
}
}

51 changes: 51 additions & 0 deletions number-of-islands/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution {
public int numIslands(char[][] grid) {
Queue<Pair> island = new LinkedList<>();
int N = grid.length;
int M = grid[0].length;
boolean[][] visited = new boolean[N][M];
int count = 0;

for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(!visited[i][j] && grid[i][j] == '1'){
island.offer(new Pair(i, j));
visited[i][j] = true;
bfs(grid, visited, island);
count++;
}
}
}
return count;
}
public void bfs(char[][] grid, boolean[][] visited, Queue<Pair> island){
int[] mx = {-1, 1, 0, 0};
int[] my = {0, 0, -1, 1};


while(!island.isEmpty()){
Pair p = island.poll();
for(int i = 0; i < 4; i++){
int nx = mx[i] + p.x;
int ny = my[i] + p.y;

if(nx < 0 || ny < 0|| nx >= grid.length || ny >= grid[0].length) continue;
if(visited[nx][ny] || grid[nx][ny] == '0') continue;

island.offer(new Pair(nx, ny));
visited[nx][ny] = true;
}

}
}

class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
}

28 changes: 28 additions & 0 deletions reverse-linked-list/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return null;

ListNode backward = null;
ListNode forward = head;

while(forward != null){
forward = head.next;
head.next = backward;
backward = head;
if(forward != null) head = forward;
}

return head;
}
}

51 changes: 51 additions & 0 deletions set-matrix-zeroes/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution {
public void setZeroes(int[][] matrix) {
int M = matrix.length;
int N = matrix[0].length;

boolean firstRow = false;
boolean firstCol = false;

for(int i = 0; i < M; i++){
if(matrix[i][0] == 0){
firstRow = true;
break;
}
}

for(int j = 0; j < N; j++){
if(matrix[0][j] == 0){
firstCol = true;
break;
}
}

for(int i = 0; i < M; i++){
for(int j = 0; j < N; j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}

for(int i = 1; i < M; i++){
for(int j = 1; j < N; j++){
if(matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0;
}
}

if(firstRow){
for(int i = 0; i < M; i++){
matrix[i][0] = 0;
}
}

if(firstCol){
for(int j = 0; j < N; j++){
matrix[0][j] = 0;
}
}
}
}

44 changes: 44 additions & 0 deletions unique-paths/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
public int uniquePaths(int m, int n) {
int[][] pathN = new int[m][n];
boolean[][] visited = new boolean[m][n];
pathN[0][0] = 1;

bfs(pathN, visited);
return pathN[m-1][n-1];
}
public void bfs(int[][] pathN, boolean[][] visited){
Queue<Pair> paths = new LinkedList<>();
int[] mx = {0, 1};
int[] my = {1, 0};

paths.offer(new Pair(0,0));

while(!paths.isEmpty()){
Pair p = paths.poll();

for(int i = 0; i < 2; i++){
int nx = mx[i] + p.x;
int ny = my[i] + p.y;

if(nx >= pathN.length || ny >= pathN[0].length) continue;
pathN[nx][ny] += pathN[p.x][p.y];
if(!visited[nx][ny]){
paths.offer(new Pair(nx, ny));
visited[nx][ny] = true;
}

}
}
}

class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
}