File tree 3 files changed +57
-0
lines changed
3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ vector<int > productExceptSelf (vector<int >& nums) {
7
+ int n = nums.size ();
8
+ vector<int > res (n, 1 );
9
+ int pre = 1 , post = 1 ;
10
+ for (int i = 1 , j = n-1 ; i < n; ++i) {
11
+ res[i] *= pre;
12
+ res[j] *= post;
13
+ pre *= res[i];
14
+ post *= res[j];
15
+ } // 正着求
16
+ return res;
17
+ }
18
+ };
Original file line number Diff line number Diff line change
1
+ #include < algorithm>
2
+ using namespace std ;
3
+ class Solution {
4
+ public:
5
+ double new21Game (int N, int K, int W) {
6
+ // vector<double> dp(K+W+1, 0.0);
7
+ double dp[20000 +1 ];
8
+ fill (dp, dp+20000 +1 , 0.0 );
9
+ double _sum = N-K+1 ;
10
+ for (int i = K; i <= N ; ++i) {
11
+ dp[i] = 1.0 ;
12
+ // _sum += 1.0;
13
+ }
14
+
15
+
16
+ for (int i = K-1 ; i >= 0 ; --i) {
17
+ // 问题出在这
18
+ // for (int j = i+1; j <= i+W; ++j) {
19
+ // _sum += dp[j];
20
+ // }
21
+ dp[i] = _sum * 1 /W;
22
+ // printf("%d-%f--dp[i]=%f\n", i+W, dp[i+W-1], _sum);
23
+ _sum = _sum - dp[i+W] + dp[i];
24
+ }
25
+ return dp[0 ];
26
+ }
27
+ };
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
3
+ n = len (nums )
4
+ res = [1 ] * n
5
+ l , r = 1 , 1
6
+ for i in range (0 , n ):
7
+ j = n - i - 1
8
+ res [i ] *= l
9
+ res [j ] *= r
10
+ l *= nums [i ]
11
+ r *= nums [j ]
12
+ return res
You can’t perform that action at this time.
0 commit comments