File tree Expand file tree Collapse file tree 5 files changed +64
-27
lines changed Expand file tree Collapse file tree 5 files changed +64
-27
lines changed Original file line number Diff line number Diff line change @@ -2,32 +2,27 @@ public class Solution {
2
2
3
3
public String encode (List <String > strs ) {
4
4
StringBuilder encodedString = new StringBuilder ();
5
- for (String str : strs ){
6
- int length = str .length ();
7
- encodedString .append (length + "#" );
8
- encodedString .append (str );
5
+ for (String str : strs ) {
6
+ encodedString . append ( str .length ())
7
+ .append ("#" )
8
+ .append (str );
9
9
}
10
10
return encodedString .toString ();
11
11
}
12
12
13
13
public List <String > decode (String str ) {
14
- List <String > decodedStrings = new ArrayList ();
15
- for (int i =0 ;i <str .length ();i ++){
16
- String length = "" ;
17
- while (str .charAt (i ) != '#' ){
18
- length += str .charAt (i );
19
- i ++;
20
- }
21
- int wordLength = Integer .parseInt (length );
22
- i ++;
23
14
24
- String word = "" ;
25
- for (int j =i ;j <wordLength +i ;j ++){
26
- word += str .charAt (j );
27
- }
28
- decodedStrings .add (word );
29
- i =i +wordLength -1 ;
15
+ List <String > list = new ArrayList <>();
16
+ int i = 0 ;
17
+ while (i < str .length ()) {
18
+ int j = i ;
19
+ while (str .charAt (j ) != '#' )
20
+ j ++;
21
+
22
+ int length = Integer .valueOf (str .substring (i , j ));
23
+ i = j + 1 + length ;
24
+ list .add (str .substring (j + 1 , i ));
30
25
}
31
- return decodedStrings ;
26
+ return list ;
32
27
}
33
28
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } temperatures
3
+ * @return {number[] }
4
+ */
5
+ const dailyTemperatures = function ( temperatures ) {
6
+ const output = Array ( temperatures . length ) . fill ( 0 )
7
+
8
+ const stack = [ ]
9
+
10
+ for ( let i = 0 ; i < temperatures . length ; i ++ ) {
11
+ while ( stack . length !== 0 && stack [ stack . length - 1 ] [ 0 ] < temperatures [ i ] ) {
12
+ const [ temp , idx ] = stack . pop ( )
13
+ output [ idx ] = i - idx
14
+ }
15
+ stack . push ( [ temperatures [ i ] , i ] )
16
+ }
17
+
18
+ return output
19
+ } ;
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
3
- ans = collections .defaultdict (list )
4
-
3
+ hashmap = defaultdict (list )
5
4
for s in strs :
6
- count = [0 ] * 26
7
- for c in s :
8
- count [ord (c ) - ord ('a' )] += 1
9
- ans [tuple (count )].append (s )
10
- return ans .values ()
5
+ # keys can be strings, bcz they are immutable.
6
+ hashmap [str (sorted (s ))].append (s )
7
+ return hashmap .values ()
Original file line number Diff line number Diff line change
1
+ function containsDuplicate ( nums : number [ ] ) : boolean {
2
+ const set = new Set ( ) ;
3
+
4
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
5
+ if ( set . has ( nums [ i ] ) ) return true ;
6
+ else set . add ( nums [ i ] ) ;
7
+ }
8
+
9
+ return false ;
10
+ }
Original file line number Diff line number Diff line change
1
+ function isAnagram ( s : string , t : string ) : boolean {
2
+ if ( s . length !== t . length ) return false ;
3
+
4
+ const store = new Array ( 26 ) . fill ( 0 ) ;
5
+
6
+ for ( let i = 0 ; i < s . length ; i ++ ) {
7
+ store [ s . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) ] ++ ;
8
+ store [ t . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) ] -- ;
9
+ }
10
+
11
+ for ( let i = 0 ; i < store . length ; i ++ ) {
12
+ if ( store [ i ] !== 0 ) return false ;
13
+ }
14
+
15
+ return true ;
16
+ }
You can’t perform that action at this time.
0 commit comments