File tree Expand file tree Collapse file tree 5 files changed +207
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 5 files changed +207
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int lengthOfLongestSubstring (String s ) {
3+ Map <Character , Integer > characters = new HashMap <>();
4+ int maxLength = 0 ;
5+ int length = 0 ;
6+ int start = 0 ;
7+
8+ for (int i = 0 ; i < s .length (); i ++){
9+ if (!characters .containsKey (s .charAt (i ))){
10+ characters .put (s .charAt (i ), i );
11+ length ++;
12+ }
13+ else {
14+ maxLength = Math .max (length , maxLength );
15+
16+ int place = characters .get (s .charAt (i ));
17+ if (place < start ){
18+ characters .put (s .charAt (i ), i );
19+ length ++;
20+ continue ;
21+ }
22+
23+ length = i - place ;
24+ start = place + 1 ;
25+ characters .put (s .charAt (i ), i );
26+ }
27+ }
28+ maxLength = Math .max (length , maxLength );
29+
30+ return maxLength ;
31+ }
32+ }
33+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int numIslands (char [][] grid ) {
3+ Queue <Pair > island = new LinkedList <>();
4+ int N = grid .length ;
5+ int M = grid [0 ].length ;
6+ boolean [][] visited = new boolean [N ][M ];
7+ int count = 0 ;
8+
9+ for (int i = 0 ; i < N ; i ++){
10+ for (int j = 0 ; j < M ; j ++){
11+ if (!visited [i ][j ] && grid [i ][j ] == '1' ){
12+ island .offer (new Pair (i , j ));
13+ visited [i ][j ] = true ;
14+ bfs (grid , visited , island );
15+ count ++;
16+ }
17+ }
18+ }
19+ return count ;
20+ }
21+ public void bfs (char [][] grid , boolean [][] visited , Queue <Pair > island ){
22+ int [] mx = {-1 , 1 , 0 , 0 };
23+ int [] my = {0 , 0 , -1 , 1 };
24+
25+
26+ while (!island .isEmpty ()){
27+ Pair p = island .poll ();
28+ for (int i = 0 ; i < 4 ; i ++){
29+ int nx = mx [i ] + p .x ;
30+ int ny = my [i ] + p .y ;
31+
32+ if (nx < 0 || ny < 0 || nx >= grid .length || ny >= grid [0 ].length ) continue ;
33+ if (visited [nx ][ny ] || grid [nx ][ny ] == '0' ) continue ;
34+
35+ island .offer (new Pair (nx , ny ));
36+ visited [nx ][ny ] = true ;
37+ }
38+
39+ }
40+ }
41+
42+ class Pair {
43+ int x ;
44+ int y ;
45+ Pair (int x , int y ){
46+ this .x = x ;
47+ this .y = y ;
48+ }
49+ }
50+ }
51+
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * public class ListNode {
4+ * int val;
5+ * ListNode next;
6+ * ListNode() {}
7+ * ListNode(int val) { this.val = val; }
8+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+ * }
10+ */
11+ class Solution {
12+ public ListNode reverseList (ListNode head ) {
13+ if (head == null ) return null ;
14+
15+ ListNode backward = null ;
16+ ListNode forward = head ;
17+
18+ while (forward != null ){
19+ forward = head .next ;
20+ head .next = backward ;
21+ backward = head ;
22+ if (forward != null ) head = forward ;
23+ }
24+
25+ return head ;
26+ }
27+ }
28+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public void setZeroes (int [][] matrix ) {
3+ int M = matrix .length ;
4+ int N = matrix [0 ].length ;
5+
6+ boolean firstRow = false ;
7+ boolean firstCol = false ;
8+
9+ for (int i = 0 ; i < M ; i ++){
10+ if (matrix [i ][0 ] == 0 ){
11+ firstRow = true ;
12+ break ;
13+ }
14+ }
15+
16+ for (int j = 0 ; j < N ; j ++){
17+ if (matrix [0 ][j ] == 0 ){
18+ firstCol = true ;
19+ break ;
20+ }
21+ }
22+
23+ for (int i = 0 ; i < M ; i ++){
24+ for (int j = 0 ; j < N ; j ++){
25+ if (matrix [i ][j ] == 0 ){
26+ matrix [i ][0 ] = 0 ;
27+ matrix [0 ][j ] = 0 ;
28+ }
29+ }
30+ }
31+
32+ for (int i = 1 ; i < M ; i ++){
33+ for (int j = 1 ; j < N ; j ++){
34+ if (matrix [i ][0 ] == 0 || matrix [0 ][j ] == 0 ) matrix [i ][j ] = 0 ;
35+ }
36+ }
37+
38+ if (firstRow ){
39+ for (int i = 0 ; i < M ; i ++){
40+ matrix [i ][0 ] = 0 ;
41+ }
42+ }
43+
44+ if (firstCol ){
45+ for (int j = 0 ; j < N ; j ++){
46+ matrix [0 ][j ] = 0 ;
47+ }
48+ }
49+ }
50+ }
51+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int uniquePaths (int m , int n ) {
3+ int [][] pathN = new int [m ][n ];
4+ boolean [][] visited = new boolean [m ][n ];
5+ pathN [0 ][0 ] = 1 ;
6+
7+ bfs (pathN , visited );
8+ return pathN [m -1 ][n -1 ];
9+ }
10+ public void bfs (int [][] pathN , boolean [][] visited ){
11+ Queue <Pair > paths = new LinkedList <>();
12+ int [] mx = {0 , 1 };
13+ int [] my = {1 , 0 };
14+
15+ paths .offer (new Pair (0 ,0 ));
16+
17+ while (!paths .isEmpty ()){
18+ Pair p = paths .poll ();
19+
20+ for (int i = 0 ; i < 2 ; i ++){
21+ int nx = mx [i ] + p .x ;
22+ int ny = my [i ] + p .y ;
23+
24+ if (nx >= pathN .length || ny >= pathN [0 ].length ) continue ;
25+ pathN [nx ][ny ] += pathN [p .x ][p .y ];
26+ if (!visited [nx ][ny ]){
27+ paths .offer (new Pair (nx , ny ));
28+ visited [nx ][ny ] = true ;
29+ }
30+
31+ }
32+ }
33+ }
34+
35+ class Pair {
36+ int x ;
37+ int y ;
38+ Pair (int x , int y ){
39+ this .x = x ;
40+ this .y = y ;
41+ }
42+ }
43+ }
44+
You can’t perform that action at this time.
0 commit comments