File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 3
3
## Algorithms <!-- omit from toc -->
4
4
5
5
- [ Array] ( #array )
6
+ - [ Backtracking] ( #backtracking )
6
7
- [ Binary Search] ( #binary-search )
7
8
- [ Dynamic Programming] ( #dynamic-programming )
8
9
- [ Greedy] ( #greedy )
16
17
| -----| ---------------- | --------------- | --------------- | --------------- | ------------- | --------------| -----|
17
18
| 1672| [ Richest Customer Wealth] ( https://leetcode.com/problems/richest-customer-wealth/ ) | [ C#] ( ./C%23/1672-Richest_Customer_Wealth.cs ) | $O(n \cdot m)$| $O(1)$| Easy|||
18
19
20
+ ## Backtracking
21
+
22
+ | # | Title | Solution | Time | Space | Difficulty | Tag | Note|
23
+ | -----| ---------------- | --------------- | --------------- | --------------- | ------------- | --------------| -----|
24
+ | 0017| [ Letter Combinations of a Phone Number] ( https://leetcode.com/problems/letter-combinations-of-a-phone-number/ ) | [ C#] ( ./C%23/0017-Letter_Combinations_of_a_Phone_Number.cs ) | $O(4^n)$| $O(4^n)$| Medium|||
25
+
19
26
## Binary Search
20
27
21
28
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
Original file line number Diff line number Diff line change
1
+ // URL: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
2
+ // Time Complexity: O(4^n)
3
+ // Space Complexity: O(4^n)
4
+
5
+ public class Solution
6
+ {
7
+ public IList < string > LetterCombinations ( string digits )
8
+ {
9
+ if ( string . IsNullOrEmpty ( digits ) ) return [ ] ;
10
+
11
+ Dictionary < string , string > map = new Dictionary < string , string > ( ) {
12
+ { "2" , "abc" } ,
13
+ { "3" , "def" } ,
14
+ { "4" , "ghi" } ,
15
+ { "5" , "jkl" } ,
16
+ { "6" , "mno" } ,
17
+ { "7" , "qprs" } ,
18
+ { "8" , "tuv" } ,
19
+ { "9" , "wxyz" }
20
+ } ;
21
+
22
+ IList < string > combinations = new List < string > ( ) ;
23
+
24
+ void Backtracking ( int index , string current )
25
+ {
26
+ if ( current . Length == digits . Length )
27
+ {
28
+ combinations . Add ( current ) ;
29
+ return ;
30
+ }
31
+
32
+ string chars = map [ digits [ index ] . ToString ( ) ] ;
33
+
34
+ for ( int i = 0 ; i < chars . Length ; i ++ )
35
+ {
36
+ Backtracking ( index + 1 , current + chars [ i ] ) ;
37
+ }
38
+ }
39
+
40
+ Backtracking ( 0 , "" ) ;
41
+
42
+ return combinations ;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments