File tree Expand file tree Collapse file tree 7 files changed +198
-2
lines changed
1413.Minimum Value to Get Positive Step by Step Sum
1415.The k-th Lexicographical String of All Happy Strings of Length n Expand file tree Collapse file tree 7 files changed +198
-2
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,13 @@ class Solution:
73
73
return max (1 , 1 - t)
74
74
```
75
75
76
+ ``` python
77
+ class Solution :
78
+ def minStartValue (self , nums : List[int ]) -> int :
79
+ s = list (accumulate(nums))
80
+ return 1 if min (s) >= 0 else abs (min (s)) + 1
81
+ ```
82
+
76
83
### ** Java**
77
84
78
85
<!-- 这里可写当前语言的特殊实现逻辑 -->
Original file line number Diff line number Diff line change @@ -65,6 +65,13 @@ class Solution:
65
65
return max (1 , 1 - t)
66
66
```
67
67
68
+ ``` python
69
+ class Solution :
70
+ def minStartValue (self , nums : List[int ]) -> int :
71
+ s = list (accumulate(nums))
72
+ return 1 if min (s) >= 0 else abs (min (s)) + 1
73
+ ```
74
+
68
75
### ** Java**
69
76
70
77
``` java
Original file line number Diff line number Diff line change 69
69
70
70
<!-- 这里可写通用的实现逻辑 -->
71
71
72
+ ** 方法一:DFS**
73
+
72
74
<!-- tabs:start -->
73
75
74
76
### ** Python3**
75
77
76
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
79
78
80
``` python
79
-
81
+ class Solution :
82
+ def getHappyString (self , n : int , k : int ) -> str :
83
+ def dfs (t ):
84
+ if len (t) == n:
85
+ ans.append(t)
86
+ return
87
+ for c in ' abc' :
88
+ if t and t[- 1 ] == c:
89
+ continue
90
+ dfs(t + c)
91
+
92
+ ans = []
93
+ dfs(' ' )
94
+ return ' ' if len (ans) < k else ans[k - 1 ]
80
95
```
81
96
82
97
### ** Java**
83
98
84
99
<!-- 这里可写当前语言的特殊实现逻辑 -->
85
100
86
101
``` java
102
+ class Solution {
103
+ private List<String > ans = new ArrayList<> ();
104
+
105
+ public String getHappyString (int n , int k ) {
106
+ dfs(" " , n);
107
+ return ans. size() < k ? " " : ans. get(k - 1 );
108
+ }
109
+
110
+ private void dfs (String t , int n ) {
111
+ if (t. length() == n) {
112
+ ans. add(t);
113
+ return ;
114
+ }
115
+ for (char c : " abc" . toCharArray()) {
116
+ if (t. length() > 0 && t. charAt(t. length() - 1 ) == c) {
117
+ continue ;
118
+ }
119
+ dfs(t + c, n);
120
+ }
121
+ }
122
+ }
123
+ ```
87
124
125
+ ### ** C++**
126
+
127
+ ``` cpp
128
+ class Solution {
129
+ public:
130
+ vector<string > ans;
131
+ string getHappyString(int n, int k) {
132
+ dfs("", n);
133
+ return ans.size() < k ? "" : ans[ k - 1] ;
134
+ }
135
+
136
+ void dfs(string t, int n) {
137
+ if (t.size() == n)
138
+ {
139
+ ans.push_back(t);
140
+ return;
141
+ }
142
+ for (int c = ' a' ; c <= ' c' ; ++c)
143
+ {
144
+ if (t.size() && t.back() == c) continue;
145
+ t.push_back(c);
146
+ dfs (t, n);
147
+ t.pop_back();
148
+ }
149
+ }
150
+ };
88
151
```
89
152
90
153
### ** ...**
Original file line number Diff line number Diff line change 57
57
### ** Python3**
58
58
59
59
``` python
60
-
60
+ class Solution :
61
+ def getHappyString (self , n : int , k : int ) -> str :
62
+ def dfs (t ):
63
+ if len (t) == n:
64
+ ans.append(t)
65
+ return
66
+ for c in ' abc' :
67
+ if t and t[- 1 ] == c:
68
+ continue
69
+ dfs(t + c)
70
+
71
+ ans = []
72
+ dfs(' ' )
73
+ return ' ' if len (ans) < k else ans[k - 1 ]
61
74
```
62
75
63
76
### ** Java**
64
77
65
78
``` java
79
+ class Solution {
80
+ private List<String > ans = new ArrayList<> ();
81
+
82
+ public String getHappyString (int n , int k ) {
83
+ dfs(" " , n);
84
+ return ans. size() < k ? " " : ans. get(k - 1 );
85
+ }
86
+
87
+ private void dfs (String t , int n ) {
88
+ if (t. length() == n) {
89
+ ans. add(t);
90
+ return ;
91
+ }
92
+ for (char c : " abc" . toCharArray()) {
93
+ if (t. length() > 0 && t. charAt(t. length() - 1 ) == c) {
94
+ continue ;
95
+ }
96
+ dfs(t + c, n);
97
+ }
98
+ }
99
+ }
100
+ ```
66
101
102
+ ### ** C++**
103
+
104
+ ``` cpp
105
+ class Solution {
106
+ public:
107
+ vector<string > ans;
108
+ string getHappyString(int n, int k) {
109
+ dfs("", n);
110
+ return ans.size() < k ? "" : ans[ k - 1] ;
111
+ }
112
+
113
+ void dfs(string t, int n) {
114
+ if (t.size() == n)
115
+ {
116
+ ans.push_back(t);
117
+ return;
118
+ }
119
+ for (int c = ' a' ; c <= ' c' ; ++c)
120
+ {
121
+ if (t.size() && t.back() == c) continue;
122
+ t.push_back(c);
123
+ dfs (t, n);
124
+ t.pop_back();
125
+ }
126
+ }
127
+ };
67
128
```
68
129
69
130
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<string> ans;
4
+ string getHappyString (int n, int k) {
5
+ dfs (" " , n);
6
+ return ans.size () < k ? " " : ans[k - 1 ];
7
+ }
8
+
9
+ void dfs (string t, int n) {
10
+ if (t.size () == n)
11
+ {
12
+ ans.push_back (t);
13
+ return ;
14
+ }
15
+ for (int c = ' a' ; c <= ' c' ; ++c)
16
+ {
17
+ if (t.size () && t.back () == c) continue ;
18
+ t.push_back (c);
19
+ dfs (t, n);
20
+ t.pop_back ();
21
+ }
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private List <String > ans = new ArrayList <>();
3
+
4
+ public String getHappyString (int n , int k ) {
5
+ dfs ("" , n );
6
+ return ans .size () < k ? "" : ans .get (k - 1 );
7
+ }
8
+
9
+ private void dfs (String t , int n ) {
10
+ if (t .length () == n ) {
11
+ ans .add (t );
12
+ return ;
13
+ }
14
+ for (char c : "abc" .toCharArray ()) {
15
+ if (t .length () > 0 && t .charAt (t .length () - 1 ) == c ) {
16
+ continue ;
17
+ }
18
+ dfs (t + c , n );
19
+ }
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def getHappyString (self , n : int , k : int ) -> str :
3
+ def dfs (t ):
4
+ if len (t ) == n :
5
+ ans .append (t )
6
+ return
7
+ for c in 'abc' :
8
+ if t and t [- 1 ] == c :
9
+ continue
10
+ dfs (t + c )
11
+
12
+ ans = []
13
+ dfs ('' )
14
+ return '' if len (ans ) < k else ans [k - 1 ]
You can’t perform that action at this time.
0 commit comments