File tree Expand file tree Collapse file tree 5 files changed +101
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
encode-and-decode-strings Expand file tree Collapse file tree 5 files changed +101
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ private int i , p ;
3+ public TreeNode buildTree (int [] preorder , int [] inorder ) {
4+ // Time complexity: O(n)
5+ // Space complexity: O(n)
6+ return builder (preorder , inorder , Integer .MIN_VALUE );
7+ }
8+
9+ private TreeNode builder (int [] preorder , int [] inorder , int stop ) {
10+ if (p >= preorder .length ) return null ;
11+ if (inorder [i ] == stop ) {
12+ i += 1 ;
13+ return null ;
14+ }
15+
16+ TreeNode node = new TreeNode (preorder [p ]);
17+ p += 1 ;
18+
19+ node .left = builder (preorder , inorder , node .val );
20+ node .right = builder (preorder , inorder , stop );
21+ return node ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] countBits (int n ) {
3+ // time complexity: O(n)
4+ // space complexity: O(n)
5+ int [] output = new int [n +1 ];
6+ int num = 0 ;
7+ while (num <= n ) output [num ] = Integer .bitCount (num ++);
8+ return output ;
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int numDecodings (String s ) {
3+ // time complexity: O(n)
4+ // space complexity: O(n)
5+ if (s .charAt (0 ) == '0' ) return 0 ;
6+
7+ int [] dp = new int [s .length () + 1 ];
8+ dp [0 ] = 1 ;
9+ dp [1 ] = 1 ;
10+
11+ for (int i = 2 ; i <= s .length (); i ++) {
12+ int oneDigit = Integer .parseInt (s .substring (i -1 , i ));
13+ int twoDigits = Integer .parseInt (s .substring (i -2 , i ));
14+
15+ if (oneDigit > 0 && oneDigit < 10 ) dp [i ] += dp [i -1 ];
16+ if (twoDigits >= 10 && twoDigits <= 26 ) dp [i ] += dp [i -2 ];
17+ }
18+
19+ return dp [s .length ()];
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ /*
3+ * @param strs: a list of strings
4+ * @return: encodes a list of strings to a single string.
5+ */
6+ public String encode (List <String > strs ) {
7+ // write your code here
8+ StringBuilder sb = new StringBuilder ();
9+ for (String str : strs ) {
10+ sb .append (str .length ()).append ("#" ).append (str );
11+ }
12+ return sb .toString ();
13+ }
14+
15+ /*
16+ * @param str: A string
17+ * @return: decodes a single string to a list of strings
18+ */
19+ public List <String > decode (String str ) {
20+ // write your code here
21+ List <String > output = new ArrayList <>();
22+ int i = 0 ;
23+ while (i < str .length ()) {
24+ int idx = str .indexOf ('#' , i );
25+ int length = Integer .parseInt (str .substring (i , idx ));
26+ String s = str .substring (idx + 1 , idx + 1 + length );
27+ output .add (s );
28+ i = idx + 1 + length ;
29+ }
30+ return output ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isAnagram (String s , String t ) {
3+ // time complexity: O(n log n)
4+ // space complexity: O(n)
5+ if (s .length () != t .length ()) return false ;
6+
7+ char [] sArr = s .toCharArray ();
8+ char [] tArr = t .toCharArray ();
9+
10+ Arrays .sort (sArr );
11+ Arrays .sort (tArr );
12+
13+ return Arrays .equals (sArr , tArr );
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments