File tree 18 files changed +404
-6
lines changed
1400-1499/1480.Running Sum of 1d Array
1500-1599/1512.Number of Good Pairs
1600-1699/1672.Richest Customer Wealth
18 files changed +404
-6
lines changed Original file line number Diff line number Diff line change 44
44
45
45
<!-- 这里可写通用的实现逻辑 -->
46
46
47
+ 原地修改数组。
48
+
47
49
<!-- tabs:start -->
48
50
49
51
### ** Python3**
50
52
51
53
<!-- 这里可写当前语言的特殊实现逻辑 -->
52
54
53
55
``` python
54
-
56
+ class Solution :
57
+ def runningSum (self , nums : List[int ]) -> List[int ]:
58
+ for i in range (1 , len (nums)):
59
+ nums[i] += nums[i - 1 ]
60
+ return nums
55
61
```
56
62
57
63
### ** Java**
58
64
59
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
66
61
67
``` java
68
+ class Solution {
69
+ public int [] runningSum (int [] nums ) {
70
+ for (int i = 1 ; i < nums. length; ++ i) {
71
+ nums[i] += nums[i - 1 ];
72
+ }
73
+ return nums;
74
+ }
75
+ }
76
+ ```
77
+
78
+ ### ** C++**
79
+
80
+ ``` cpp
81
+ class Solution {
82
+ public:
83
+ vector<int > runningSum(vector<int >& nums) {
84
+ for (int i = 1; i < nums.size(); ++i) {
85
+ nums[ i] += nums[ i - 1] ;
86
+ }
87
+ return nums;
88
+ }
89
+ };
90
+ ```
91
+
92
+ ### **Go**
62
93
94
+ ```go
95
+ func runningSum(nums []int) []int {
96
+ for i := 1; i < len(nums); i++ {
97
+ nums[i] += nums[i-1]
98
+ }
99
+ return nums
100
+ }
63
101
```
64
102
65
103
### ** ...**
Original file line number Diff line number Diff line change 74
74
### ** Python3**
75
75
76
76
``` python
77
-
77
+ class Solution :
78
+ def runningSum (self , nums : List[int ]) -> List[int ]:
79
+ for i in range (1 , len (nums)):
80
+ nums[i] += nums[i - 1 ]
81
+ return nums
78
82
```
79
83
80
84
### ** Java**
81
85
82
86
``` java
87
+ class Solution {
88
+ public int [] runningSum (int [] nums ) {
89
+ for (int i = 1 ; i < nums. length; ++ i) {
90
+ nums[i] += nums[i - 1 ];
91
+ }
92
+ return nums;
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### ** C++**
98
+
99
+ ``` cpp
100
+ class Solution {
101
+ public:
102
+ vector<int > runningSum(vector<int >& nums) {
103
+ for (int i = 1; i < nums.size(); ++i) {
104
+ nums[ i] += nums[ i - 1] ;
105
+ }
106
+ return nums;
107
+ }
108
+ };
109
+ ```
110
+
111
+ ### **Go**
83
112
113
+ ```go
114
+ func runningSum(nums []int) []int {
115
+ for i := 1; i < len(nums); i++ {
116
+ nums[i] += nums[i-1]
117
+ }
118
+ return nums
119
+ }
84
120
```
85
121
86
122
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > runningSum (vector<int >& nums) {
4
+ for (int i = 1 ; i < nums.size (); ++i) {
5
+ nums[i] += nums[i - 1 ];
6
+ }
7
+ return nums;
8
+ }
9
+ };
Original file line number Diff line number Diff line change
1
+ func runningSum (nums []int ) []int {
2
+ for i := 1 ; i < len (nums ); i ++ {
3
+ nums [i ] += nums [i - 1 ]
4
+ }
5
+ return nums
6
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] runningSum (int [] nums ) {
3
+ for (int i = 1 ; i < nums .length ; ++i ) {
4
+ nums [i ] += nums [i - 1 ];
5
+ }
6
+ return nums ;
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def runningSum (self , nums : List [int ]) -> List [int ]:
3
+ for i in range (1 , len (nums )):
4
+ nums [i ] += nums [i - 1 ]
5
+ return nums
Original file line number Diff line number Diff line change 54
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
55
56
56
``` python
57
-
57
+ class Solution :
58
+ def numIdenticalPairs (self , nums : List[int ]) -> int :
59
+ counter = collections.Counter(nums)
60
+ return sum ([x * (x - 1 ) for x in counter.values()]) >> 1
58
61
```
59
62
60
63
### ** Java**
61
64
62
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
66
64
67
``` java
68
+ class Solution {
69
+ public int numIdenticalPairs (int [] nums ) {
70
+ Map<Integer , Integer > counter = new HashMap<> ();
71
+ for (int num : nums) {
72
+ counter. put(num, counter. getOrDefault(num, 0 ) + 1 );
73
+ }
74
+ int res = 0 ;
75
+ for (int n : counter. values()) {
76
+ res += n * (n - 1 );
77
+ }
78
+ return res >> 1 ;
79
+ }
80
+ }
81
+ ```
82
+
83
+ ### ** C++**
84
+
85
+ ``` cpp
86
+ class Solution {
87
+ public:
88
+ int numIdenticalPairs(vector<int >& nums) {
89
+ unordered_map <int, int> counter;
90
+ for (int num : nums) {
91
+ ++counter[ num] ;
92
+ }
93
+ int res = 0;
94
+ for (auto &[ num, n] : counter) {
95
+ res += n * (n - 1);
96
+ }
97
+ return res >> 1;
98
+ }
99
+ };
100
+ ```
65
101
102
+ ### **Go**
103
+
104
+ ```go
105
+ func numIdenticalPairs(nums []int) int {
106
+ counter := make(map[int]int)
107
+ for _, num := range nums {
108
+ counter[num]++
109
+ }
110
+ res := 0
111
+ for _, n := range counter {
112
+ res += n * (n - 1)
113
+ }
114
+ return res >> 1
115
+ }
66
116
```
67
117
68
118
### ** ...**
Original file line number Diff line number Diff line change 82
82
### ** Python3**
83
83
84
84
``` python
85
-
85
+ class Solution :
86
+ def numIdenticalPairs (self , nums : List[int ]) -> int :
87
+ counter = collections.Counter(nums)
88
+ return sum ([x * (x - 1 ) for x in counter.values()]) >> 1
86
89
```
87
90
88
91
### ** Java**
89
92
90
93
``` java
94
+ class Solution {
95
+ public int numIdenticalPairs (int [] nums ) {
96
+ Map<Integer , Integer > counter = new HashMap<> ();
97
+ for (int num : nums) {
98
+ counter. put(num, counter. getOrDefault(num, 0 ) + 1 );
99
+ }
100
+ int res = 0 ;
101
+ for (int n : counter. values()) {
102
+ res += n * (n - 1 );
103
+ }
104
+ return res >> 1 ;
105
+ }
106
+ }
107
+ ```
108
+
109
+ ### ** C++**
110
+
111
+ ``` cpp
112
+ class Solution {
113
+ public:
114
+ int numIdenticalPairs(vector<int >& nums) {
115
+ unordered_map <int, int> counter;
116
+ for (int num : nums) {
117
+ ++counter[ num] ;
118
+ }
119
+ int res = 0;
120
+ for (auto &[ num, n] : counter) {
121
+ res += n * (n - 1);
122
+ }
123
+ return res >> 1;
124
+ }
125
+ };
126
+ ```
91
127
128
+ ### **Go**
129
+
130
+ ```go
131
+ func numIdenticalPairs(nums []int) int {
132
+ counter := make(map[int]int)
133
+ for _, num := range nums {
134
+ counter[num]++
135
+ }
136
+ res := 0
137
+ for _, n := range counter {
138
+ res += n * (n - 1)
139
+ }
140
+ return res >> 1
141
+ }
92
142
```
93
143
94
144
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int numIdenticalPairs (vector<int >& nums) {
4
+ unordered_map <int , int > counter;
5
+ for (int num : nums) {
6
+ ++counter[num];
7
+ }
8
+ int res = 0 ;
9
+ for (auto &[num, n] : counter) {
10
+ res += n * (n - 1 );
11
+ }
12
+ return res >> 1 ;
13
+ }
14
+ };
Original file line number Diff line number Diff line change
1
+ func numIdenticalPairs (nums []int ) int {
2
+ counter := make (map [int ]int )
3
+ for _ , num := range nums {
4
+ counter [num ]++
5
+ }
6
+ res := 0
7
+ for _ , n := range counter {
8
+ res += n * (n - 1 )
9
+ }
10
+ return res >> 1
11
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numIdenticalPairs (int [] nums ) {
3
+ Map <Integer , Integer > counter = new HashMap <>();
4
+ for (int num : nums ) {
5
+ counter .put (num , counter .getOrDefault (num , 0 ) + 1 );
6
+ }
7
+ int res = 0 ;
8
+ for (int n : counter .values ()) {
9
+ res += n * (n - 1 );
10
+ }
11
+ return res >> 1 ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numIdenticalPairs (self , nums : List [int ]) -> int :
3
+ counter = collections .Counter (nums )
4
+ return sum ([x * (x - 1 ) for x in counter .values ()]) >> 1
Original file line number Diff line number Diff line change 61
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
62
63
63
``` python
64
-
64
+ class Solution :
65
+ def maximumWealth (self , accounts : List[List[int ]]) -> int :
66
+ res = 0
67
+ for account in accounts:
68
+ res = max (res, sum (account))
69
+ return res
65
70
```
66
71
67
72
### ** Java**
68
73
69
74
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
75
71
76
``` java
77
+ class Solution {
78
+ public int maximumWealth (int [][] accounts ) {
79
+ int res = 0 ;
80
+ for (int [] account : accounts) {
81
+ int t = 0 ;
82
+ for (int money : account) {
83
+ t += money;
84
+ }
85
+ res = Math . max(res, t);
86
+ }
87
+ return res;
88
+ }
89
+ }
90
+ ```
91
+
92
+ ### ** C++**
93
+
94
+ ``` cpp
95
+ class Solution {
96
+ public:
97
+ int maximumWealth(vector<vector<int >>& accounts) {
98
+ int res = 0;
99
+ for (auto& account : accounts) {
100
+ int t = 0;
101
+ for (auto& money : account) {
102
+ t += money;
103
+ }
104
+ res = max(res, t);
105
+ }
106
+ return res;
107
+ }
108
+ };
109
+ ```
72
110
111
+ ### **Go**
112
+
113
+ ```go
114
+ func maximumWealth(accounts [][]int) int {
115
+ res := 0
116
+ for _, account := range accounts {
117
+ t := 0
118
+ for _, money := range account {
119
+ t += money
120
+ }
121
+ if t > res {
122
+ res = t
123
+ }
124
+ }
125
+ return res
126
+ }
73
127
```
74
128
75
129
### ** ...**
You can’t perform that action at this time.
0 commit comments