File tree Expand file tree Collapse file tree 9 files changed +354
-2
lines changed
0300-0399/0386.Lexicographical Numbers
0500-0599/0540.Single Element in a Sorted Array Expand file tree Collapse file tree 9 files changed +354
-2
lines changed Original file line number Diff line number Diff line change 19
19
20
20
<!-- 这里可写通用的实现逻辑 -->
21
21
22
+ DFS。
23
+
22
24
<!-- tabs:start -->
23
25
24
26
### ** Python3**
25
27
26
28
<!-- 这里可写当前语言的特殊实现逻辑 -->
27
29
28
30
``` python
29
-
31
+ class Solution :
32
+ def lexicalOrder (self , n : int ) -> List[int ]:
33
+ res = []
34
+
35
+ def dfs (i , n ):
36
+ if i > n:
37
+ return
38
+ res.append(i)
39
+ for j in range (10 ):
40
+ dfs(i * 10 + j, n)
41
+
42
+ for i in range (1 , 10 ):
43
+ dfs(i, n)
44
+ return res
30
45
```
31
46
32
47
### ** Java**
33
48
34
49
<!-- 这里可写当前语言的特殊实现逻辑 -->
35
50
36
51
``` java
52
+ class Solution {
53
+ public List<Integer > lexicalOrder (int n ) {
54
+ List<Integer > res = new ArrayList<> ();
55
+ for (int i = 1 ; i < 10 ; ++ i) {
56
+ dfs(res, i, n);
57
+ }
58
+ return res;
59
+ }
60
+
61
+ private void dfs (List<Integer > res , int i , int n ) {
62
+ if (i > n) {
63
+ return ;
64
+ }
65
+ res. add(i);
66
+ for (int j = 0 ; j < 10 ; ++ j) {
67
+ dfs(res, i * 10 + j, n);
68
+ }
69
+ }
70
+ }
71
+ ```
72
+
73
+ ### ** C++**
74
+
75
+ ``` cpp
76
+ class Solution {
77
+ public:
78
+ vector<int > lexicalOrder(int n) {
79
+ vector<int > res;
80
+ for (int i = 1; i < 10; ++i)
81
+ {
82
+ dfs(res, i, n);
83
+ }
84
+ return res;
85
+ }
86
+
87
+ void dfs(vector<int> &res, int i, int n) {
88
+ if (i > n)
89
+ return;
90
+ res.push_back(i);
91
+ for (int j = 0; j < 10; ++j)
92
+ {
93
+ dfs(res, i * 10 + j, n);
94
+ }
95
+ }
96
+ };
97
+ ```
37
98
99
+ ### ** Go**
100
+
101
+ ``` go
102
+ func lexicalOrder (n int ) []int {
103
+ var res []int
104
+ var dfs func (int , int )
105
+ dfs = func (i, n int ) {
106
+ if i > n {
107
+ return
108
+ }
109
+ res = append (res, i)
110
+ for j := 0 ; j < 10 ; j++ {
111
+ dfs (i*10 +j, n)
112
+ }
113
+ }
114
+
115
+ for i := 1 ; i < 10 ; i++ {
116
+ dfs (i, n)
117
+ }
118
+ return res
119
+ }
38
120
```
39
121
40
122
### ** ...**
Original file line number Diff line number Diff line change 27
27
28
28
## Solutions
29
29
30
+ DFS.
31
+
30
32
<!-- tabs:start -->
31
33
32
34
### ** Python3**
33
35
34
36
``` python
35
-
37
+ class Solution :
38
+ def lexicalOrder (self , n : int ) -> List[int ]:
39
+ res = []
40
+
41
+ def dfs (i , n ):
42
+ if i > n:
43
+ return
44
+ res.append(i)
45
+ for j in range (10 ):
46
+ dfs(i * 10 + j, n)
47
+
48
+ for i in range (1 , 10 ):
49
+ dfs(i, n)
50
+ return res
36
51
```
37
52
38
53
### ** Java**
39
54
40
55
``` java
56
+ class Solution {
57
+ public List<Integer > lexicalOrder (int n ) {
58
+ List<Integer > res = new ArrayList<> ();
59
+ for (int i = 1 ; i < 10 ; ++ i) {
60
+ dfs(res, i, n);
61
+ }
62
+ return res;
63
+ }
64
+
65
+ private void dfs (List<Integer > res , int i , int n ) {
66
+ if (i > n) {
67
+ return ;
68
+ }
69
+ res. add(i);
70
+ for (int j = 0 ; j < 10 ; ++ j) {
71
+ dfs(res, i * 10 + j, n);
72
+ }
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### ** C++**
78
+
79
+ ``` cpp
80
+ class Solution {
81
+ public:
82
+ vector<int > lexicalOrder(int n) {
83
+ vector<int > res;
84
+ for (int i = 1; i < 10; ++i)
85
+ {
86
+ dfs(res, i, n);
87
+ }
88
+ return res;
89
+ }
90
+
91
+ void dfs(vector<int> &res, int i, int n) {
92
+ if (i > n)
93
+ return;
94
+ res.push_back(i);
95
+ for (int j = 0; j < 10; ++j)
96
+ {
97
+ dfs(res, i * 10 + j, n);
98
+ }
99
+ }
100
+ };
101
+ ```
41
102
103
+ ### ** Go**
104
+
105
+ ``` go
106
+ func lexicalOrder (n int ) []int {
107
+ var res []int
108
+ var dfs func (int , int )
109
+ dfs = func (i, n int ) {
110
+ if i > n {
111
+ return
112
+ }
113
+ res = append (res, i)
114
+ for j := 0 ; j < 10 ; j++ {
115
+ dfs (i*10 +j, n)
116
+ }
117
+ }
118
+
119
+ for i := 1 ; i < 10 ; i++ {
120
+ dfs (i, n)
121
+ }
122
+ return res
123
+ }
42
124
```
43
125
44
126
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > lexicalOrder (int n) {
4
+ vector<int > res;
5
+ for (int i = 1 ; i < 10 ; ++i)
6
+ {
7
+ dfs (res, i, n);
8
+ }
9
+ return res;
10
+ }
11
+
12
+ void dfs (vector<int > &res, int i, int n) {
13
+ if (i > n)
14
+ return ;
15
+ res.push_back (i);
16
+ for (int j = 0 ; j < 10 ; ++j)
17
+ {
18
+ dfs (res, i * 10 + j, n);
19
+ }
20
+ }
21
+ };
Original file line number Diff line number Diff line change
1
+ func lexicalOrder (n int ) []int {
2
+ var res []int
3
+ var dfs func (int , int )
4
+ dfs = func (i , n int ) {
5
+ if i > n {
6
+ return
7
+ }
8
+ res = append (res , i )
9
+ for j := 0 ; j < 10 ; j ++ {
10
+ dfs (i * 10 + j , n )
11
+ }
12
+ }
13
+
14
+ for i := 1 ; i < 10 ; i ++ {
15
+ dfs (i , n )
16
+ }
17
+ return res
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > lexicalOrder (int n ) {
3
+ List <Integer > res = new ArrayList <>();
4
+ for (int i = 1 ; i < 10 ; ++i ) {
5
+ dfs (res , i , n );
6
+ }
7
+ return res ;
8
+ }
9
+
10
+ private void dfs (List <Integer > res , int i , int n ) {
11
+ if (i > n ) {
12
+ return ;
13
+ }
14
+ res .add (i );
15
+ for (int j = 0 ; j < 10 ; ++j ) {
16
+ dfs (res , i * 10 + j , n );
17
+ }
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def lexicalOrder (self , n : int ) -> List [int ]:
3
+ res = []
4
+
5
+ def dfs (i , n ):
6
+ if i > n :
7
+ return
8
+ res .append (i )
9
+ for j in range (10 ):
10
+ dfs (i * 10 + j , n )
11
+
12
+ for i in range (1 , 10 ):
13
+ dfs (i , n )
14
+ return res
Original file line number Diff line number Diff line change @@ -52,6 +52,19 @@ class Solution:
52
52
return nums[left]
53
53
```
54
54
55
+ ``` python
56
+ class Solution :
57
+ def singleNonDuplicate (self , nums : List[int ]) -> int :
58
+ left, right = 0 , len (nums) - 1
59
+ while left < right:
60
+ mid = (left + right) >> 1
61
+ if (mid % 2 == 0 and nums[mid] != nums[mid + 1 ]) or (mid % 2 != 0 and nums[mid] != nums[mid - 1 ]):
62
+ right = mid
63
+ else :
64
+ left = mid + 1
65
+ return nums[left]
66
+ ```
67
+
55
68
### ** Java**
56
69
57
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -76,6 +89,23 @@ class Solution {
76
89
}
77
90
```
78
91
92
+ ``` java
93
+ class Solution {
94
+ public int singleNonDuplicate (int [] nums ) {
95
+ int left = 0 , right = nums. length - 1 ;
96
+ while (left < right) {
97
+ int mid = (left + right) >> 1 ;
98
+ if ((mid % 2 == 0 && nums[mid] != nums[mid + 1 ]) || (mid % 2 != 0 && nums[mid] != nums[mid - 1 ])) {
99
+ right = mid;
100
+ } else {
101
+ left = mid + 1 ;
102
+ }
103
+ }
104
+ return nums[left];
105
+ }
106
+ }
107
+ ```
108
+
79
109
### ** TypeScript**
80
110
81
111
``` ts
@@ -114,6 +144,26 @@ func singleNonDuplicate(nums []int) int {
114
144
}
115
145
```
116
146
147
+ ### ** C++**
148
+
149
+ ``` cpp
150
+ class Solution {
151
+ public:
152
+ int singleNonDuplicate(vector<int > &nums) {
153
+ int left = 0, right = nums.size() - 1;
154
+ while (left < right)
155
+ {
156
+ int mid = left + right >> 1;
157
+ if ((mid % 2 == 0 && nums[ mid] != nums[ mid + 1] ) || (mid % 2 != 0 && nums[ mid] != nums[ mid - 1] ))
158
+ right = mid;
159
+ else
160
+ left = mid + 1;
161
+ }
162
+ return nums[ left] ;
163
+ }
164
+ };
165
+ ```
166
+
117
167
### **...**
118
168
119
169
```
You can’t perform that action at this time.
0 commit comments