File tree 10 files changed +299
-4
lines changed
1800.Maximum Ascending Subarray Sum
1822.Sign of the Product of an Array
10 files changed +299
-4
lines changed Original file line number Diff line number Diff line change 54
54
<li><code>1 <= nums[i] <= 100</code></li>
55
55
</ul >
56
56
57
-
58
57
## 解法
59
58
60
59
<!-- 这里可写通用的实现逻辑 -->
66
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
66
68
67
``` python
69
-
68
+ class Solution :
69
+ def maxAscendingSum (self , nums : List[int ]) -> int :
70
+ res, cur = 0 , nums[0 ]
71
+ for i in range (1 , len (nums)):
72
+ if nums[i] > nums[i - 1 ]:
73
+ cur += nums[i]
74
+ else :
75
+ res = max (res, cur)
76
+ cur = nums[i]
77
+ res = max (res, cur)
78
+ return res
70
79
```
71
80
72
81
### ** Java**
73
82
74
83
<!-- 这里可写当前语言的特殊实现逻辑 -->
75
84
76
85
``` java
86
+ class Solution {
87
+ public int maxAscendingSum (int [] nums ) {
88
+ int cur = nums[0 ];
89
+ int res = 0 ;
90
+ for (int i = 1 ; i < nums. length; ++ i) {
91
+ if (nums[i] > nums[i - 1 ]) {
92
+ cur += nums[i];
93
+ } else {
94
+ res = Math . max(res, cur);
95
+ cur = nums[i];
96
+ }
97
+ }
98
+ res = Math . max(res, cur);
99
+ return res;
100
+ }
101
+ }
102
+ ```
103
+
104
+ ### ** C++**
105
+
106
+ ``` cpp
107
+ class Solution {
108
+ public:
109
+ int maxAscendingSum(vector<int >& nums) {
110
+ int res = 0, cur = nums[ 0] ;
111
+ for (int i = 1; i < nums.size(); ++i) {
112
+ if (nums[ i] > nums[ i - 1] ) {
113
+ cur += nums[ i] ;
114
+ } else {
115
+ res = max(res, cur);
116
+ cur = nums[ i] ;
117
+ }
118
+ }
119
+ res = max(res, cur);
120
+ return res;
121
+ }
122
+ };
123
+ ```
77
124
125
+ ### **Go**
126
+
127
+ ```go
128
+ func maxAscendingSum(nums []int) int {
129
+ res, cur := 0, nums[0]
130
+ for i := 1; i < len(nums); i++ {
131
+ if nums[i] > nums[i-1] {
132
+ cur += nums[i]
133
+ } else {
134
+ if res < cur {
135
+ res = cur
136
+ }
137
+ cur = nums[i]
138
+ }
139
+ }
140
+ if res < cur {
141
+ res = cur
142
+ }
143
+ return res
144
+ }
78
145
```
79
146
80
147
### ** ...**
Original file line number Diff line number Diff line change 50
50
<li><code>1 <= nums[i] <= 100</code></li>
51
51
</ul >
52
52
53
-
54
53
## Solutions
55
54
56
55
<!-- tabs:start -->
57
56
58
57
### ** Python3**
59
58
60
59
``` python
61
-
60
+ class Solution :
61
+ def maxAscendingSum (self , nums : List[int ]) -> int :
62
+ res, cur = 0 , nums[0 ]
63
+ for i in range (1 , len (nums)):
64
+ if nums[i] > nums[i - 1 ]:
65
+ cur += nums[i]
66
+ else :
67
+ res = max (res, cur)
68
+ cur = nums[i]
69
+ res = max (res, cur)
70
+ return res
62
71
```
63
72
64
73
### ** Java**
65
74
66
75
``` java
76
+ class Solution {
77
+ public int maxAscendingSum (int [] nums ) {
78
+ int cur = nums[0 ];
79
+ int res = 0 ;
80
+ for (int i = 1 ; i < nums. length; ++ i) {
81
+ if (nums[i] > nums[i - 1 ]) {
82
+ cur += nums[i];
83
+ } else {
84
+ res = Math . max(res, cur);
85
+ cur = nums[i];
86
+ }
87
+ }
88
+ res = Math . max(res, cur);
89
+ return res;
90
+ }
91
+ }
92
+ ```
93
+
94
+ ### ** C++**
95
+
96
+ ``` cpp
97
+ class Solution {
98
+ public:
99
+ int maxAscendingSum(vector<int >& nums) {
100
+ int res = 0, cur = nums[ 0] ;
101
+ for (int i = 1; i < nums.size(); ++i) {
102
+ if (nums[ i] > nums[ i - 1] ) {
103
+ cur += nums[ i] ;
104
+ } else {
105
+ res = max(res, cur);
106
+ cur = nums[ i] ;
107
+ }
108
+ }
109
+ res = max(res, cur);
110
+ return res;
111
+ }
112
+ };
113
+ ```
67
114
115
+ ### **Go**
116
+
117
+ ```go
118
+ func maxAscendingSum(nums []int) int {
119
+ res, cur := 0, nums[0]
120
+ for i := 1; i < len(nums); i++ {
121
+ if nums[i] > nums[i-1] {
122
+ cur += nums[i]
123
+ } else {
124
+ if res < cur {
125
+ res = cur
126
+ }
127
+ cur = nums[i]
128
+ }
129
+ }
130
+ if res < cur {
131
+ res = cur
132
+ }
133
+ return res
134
+ }
68
135
```
69
136
70
137
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int maxAscendingSum (vector<int >& nums) {
4
+ int res = 0 , cur = nums[0 ];
5
+ for (int i = 1 ; i < nums.size (); ++i) {
6
+ if (nums[i] > nums[i - 1 ]) {
7
+ cur += nums[i];
8
+ } else {
9
+ res = max (res, cur);
10
+ cur = nums[i];
11
+ }
12
+ }
13
+ res = max (res, cur);
14
+ return res;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ func maxAscendingSum (nums []int ) int {
2
+ res , cur := 0 , nums [0 ]
3
+ for i := 1 ; i < len (nums ); i ++ {
4
+ if nums [i ] > nums [i - 1 ] {
5
+ cur += nums [i ]
6
+ } else {
7
+ if res < cur {
8
+ res = cur
9
+ }
10
+ cur = nums [i ]
11
+ }
12
+ }
13
+ if res < cur {
14
+ res = cur
15
+ }
16
+ return res
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxAscendingSum (int [] nums ) {
3
+ int cur = nums [0 ];
4
+ int res = 0 ;
5
+ for (int i = 1 ; i < nums .length ; ++i ) {
6
+ if (nums [i ] > nums [i - 1 ]) {
7
+ cur += nums [i ];
8
+ } else {
9
+ res = Math .max (res , cur );
10
+ cur = nums [i ];
11
+ }
12
+ }
13
+ res = Math .max (res , cur );
14
+ return res ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxAscendingSum (self , nums : List [int ]) -> int :
3
+ res , cur = 0 , nums [0 ]
4
+ for i in range (1 , len (nums )):
5
+ if nums [i ] > nums [i - 1 ]:
6
+ cur += nums [i ]
7
+ else :
8
+ res = max (res , cur )
9
+ cur = nums [i ]
10
+ res = max (res , cur )
11
+ return res
Original file line number Diff line number Diff line change @@ -109,6 +109,43 @@ var arraySign = function (nums) {
109
109
};
110
110
```
111
111
112
+ ### ** C++**
113
+
114
+ ``` cpp
115
+ class Solution {
116
+ public:
117
+ int arraySign(vector<int >& nums) {
118
+ int res = 1;
119
+ for (auto &num : nums) {
120
+ if (num == 0) {
121
+ return 0;
122
+ }
123
+ if (num < 0) {
124
+ res * = -1;
125
+ }
126
+ }
127
+ return res;
128
+ }
129
+ };
130
+ ```
131
+
132
+ ### **Go**
133
+
134
+ ```go
135
+ func arraySign(nums []int) int {
136
+ res := 1
137
+ for _, num := range nums {
138
+ if num == 0 {
139
+ return 0
140
+ }
141
+ if num < 0 {
142
+ res *= -1
143
+ }
144
+ }
145
+ return res
146
+ }
147
+ ```
148
+
112
149
### ** ...**
113
150
114
151
```
Original file line number Diff line number Diff line change @@ -99,6 +99,43 @@ var arraySign = function (nums) {
99
99
};
100
100
```
101
101
102
+ ### ** C++**
103
+
104
+ ``` cpp
105
+ class Solution {
106
+ public:
107
+ int arraySign(vector<int >& nums) {
108
+ int res = 1;
109
+ for (auto &num : nums) {
110
+ if (num == 0) {
111
+ return 0;
112
+ }
113
+ if (num < 0) {
114
+ res * = -1;
115
+ }
116
+ }
117
+ return res;
118
+ }
119
+ };
120
+ ```
121
+
122
+ ### **Go**
123
+
124
+ ```go
125
+ func arraySign(nums []int) int {
126
+ res := 1
127
+ for _, num := range nums {
128
+ if num == 0 {
129
+ return 0
130
+ }
131
+ if num < 0 {
132
+ res *= -1
133
+ }
134
+ }
135
+ return res
136
+ }
137
+ ```
138
+
102
139
### ** ...**
103
140
104
141
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int arraySign (vector<int >& nums) {
4
+ int res = 1 ;
5
+ for (auto &num : nums) {
6
+ if (num == 0 ) {
7
+ return 0 ;
8
+ }
9
+ if (num < 0 ) {
10
+ res *= -1 ;
11
+ }
12
+ }
13
+ return res;
14
+ }
15
+ };
Original file line number Diff line number Diff line change
1
+ func arraySign (nums []int ) int {
2
+ res := 1
3
+ for _ , num := range nums {
4
+ if num == 0 {
5
+ return 0
6
+ }
7
+ if num < 0 {
8
+ res *= - 1
9
+ }
10
+ }
11
+ return res
12
+ }
You can’t perform that action at this time.
0 commit comments