File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+
3
+ // bubble sort algorithm
4
+ public void sortColors (int [] nums ) {
5
+
6
+ for (int i = 0 ; i < nums .length -1 ; i ++) {
7
+
8
+ boolean swapped = false ;
9
+ for (int j = 0 ; j < nums .length -1 ; j ++) {
10
+ if (nums [j ] > nums [j +1 ]) {
11
+ int temp = nums [j ];
12
+ nums [j ] = nums [j +1 ];
13
+ nums [j +1 ] = temp ;
14
+ swapped = true ;
15
+ }
16
+ }
17
+
18
+ if (!swapped ) {
19
+ break ;
20
+ }
21
+ }
22
+ }
23
+
24
+ public void sortColors (int [] nums ) {
25
+ int count0 = 0 ;
26
+ int count1 = 0 ;
27
+ int count2 = 0 ;
28
+
29
+ for (int i = 0 ; i < nums .length ; i ++) {
30
+ if (nums [i ] == 0 ) {
31
+ count0 ++;
32
+ } else if (nums [i ] == 1 ) {
33
+ count1 ++;
34
+ } else {
35
+ count2 ++;
36
+ }
37
+ }
38
+
39
+ for (int j = 0 ; j < nums .length ; j ++) {
40
+ if (j < count0 ) {
41
+ nums [j ] = 0 ;
42
+ } else if (j < count1 + count0 ) {
43
+ nums [j ] = 1 ;
44
+ } else {
45
+ nums [j ] = 2 ;
46
+ }
47
+ }
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isAnagram (String s , String t ) {
3
+
4
+ if (s .length () != t .length ()) {
5
+ return false ;
6
+ }
7
+
8
+ HashMap <Character , Integer > smap = new HashMap <>();
9
+ HashMap <Character , Integer > tmap = new HashMap <>();
10
+ for (int i = 0 ; i < s .length (); i ++) {
11
+ smap .put (s .charAt (i ), smap .getOrDefault (s .charAt (i ), 0 ) + 1 );
12
+ tmap .put (t .charAt (i ), tmap .getOrDefault (t .charAt (i ), 0 ) + 1 );
13
+ }
14
+
15
+ return smap .equals (tmap );
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments