File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .HashMap ;
3+ import java .util .List ;
4+ import java .util .Map ;
5+
6+ class Solution {
7+
8+ private static List <String > result = new ArrayList <String >();
9+ static Map <String , String > phone = new HashMap <String , String >() {{
10+ put ("2" , "abc" );
11+ put ("3" , "def" );
12+ put ("4" , "ghi" );
13+ put ("5" , "jkl" );
14+ put ("6" , "mno" );
15+ put ("7" , "pqrs" );
16+ put ("8" , "tuv" );
17+ put ("9" , "wxyz" );
18+ }};
19+
20+ public static List <String > letterCombinations (String digits ) {
21+ rec (0 , digits , "" );
22+ return result ;
23+
24+ }
25+
26+ public static void rec (int level , String digits , String now ) {
27+
28+ if (level == digits .length ()) {
29+ result .add (now );
30+ return ;
31+ }
32+
33+ String input = phone .get (digits .charAt (level ));
34+
35+ if (input != null ) {
36+ for (int i = 0 ; i < input .length (); i ++) {
37+ rec (level + 1 , digits , now + input .charAt (i ));
38+ }
39+ } else {
40+ rec (level + 1 , digits , now );
41+ }
42+
43+ }
44+
45+ public static void main (String [] args ) {
46+ letterCombinations ("23" );
47+ System .out .println (result );
48+ }
49+
50+ }
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .List ;
3+
4+ class Solution {
5+
6+ private List <String > list = new ArrayList ();
7+
8+ public List <String > generateParenthesis (int n ) {
9+ rec (0 , 0 , n , "" );
10+ return list ;
11+ }
12+
13+ public void rec (int left , int right , int n , String s ) {
14+ if (left == n && right == n ) {
15+ list .add (s );
16+ }
17+ if (left > right ) {
18+ rec (left , right +1 , n , s + ")" );
19+ }
20+ if (left < n ) {
21+ rec (left +1 , right , n , s + "(" );
22+ }
23+ }
24+
25+
26+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public double myPow (double x , int n ) {
3+ long N =n ;
4+ if (N < 0 ) {
5+ N = -N ;
6+ x = 1.0 / x ;
7+ }
8+ return fastPow (x , N );
9+
10+
11+ }
12+
13+ public double fastPow (double x , long n ) {
14+ if (n == 0 )
15+ return 1.0 ;
16+ double half = fastPow (x , n / 2 );
17+ return n % 2 == 1 ? half * half * x : half * half ;
18+ }
19+
20+
21+ }
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .List ;
3+
4+ class Solution {
5+
6+
7+ private List <List <Integer >> result = new ArrayList <List <Integer >>;
8+
9+ public List <List <Integer >> subsets (int [] nums ) {
10+ dfs (nums , new ArrayList (),nums .length , 0 );
11+ return result ;
12+ }
13+
14+ public void dfs (int [] nums , List <Integer > array , int length , int level ) {
15+ if (array .size () == length ) {
16+ result .add (array );
17+ return ;
18+ }
19+ dfs (nums , array , length , level + 1 );
20+ array .add (nums [level ]);
21+ dfs (nums , array , length , level + 1 );
22+ array .remove (length - 1 );
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments