Skip to content

Commit 00a59df

Browse files
committed
feat: add solutions to lc problems: No.1480,1512,1672
1 parent 8bd50c5 commit 00a59df

File tree

18 files changed

+404
-6
lines changed

18 files changed

+404
-6
lines changed

solution/1400-1499/1480.Running Sum of 1d Array/README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,60 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
原地修改数组。
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
5052

5153
<!-- 这里可写当前语言的特殊实现逻辑 -->
5254

5355
```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
5561
```
5662

5763
### **Java**
5864

5965
<!-- 这里可写当前语言的特殊实现逻辑 -->
6066

6167
```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**
6293
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+
}
63101
```
64102

65103
### **...**

solution/1400-1499/1480.Running Sum of 1d Array/README_EN.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,49 @@
7474
### **Python3**
7575

7676
```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
7882
```
7983

8084
### **Java**
8185

8286
```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**
83112
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+
}
84120
```
85121

86122
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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

solution/1500-1599/1512.Number of Good Pairs/README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,65 @@
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```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
5861
```
5962

6063
### **Java**
6164

6265
<!-- 这里可写当前语言的特殊实现逻辑 -->
6366

6467
```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+
```
65101
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+
}
66116
```
67117

68118
### **...**

solution/1500-1599/1512.Number of Good Pairs/README_EN.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,63 @@
8282
### **Python3**
8383

8484
```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
8689
```
8790

8891
### **Java**
8992

9093
```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+
```
91127
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+
}
92142
```
93143

94144
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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

solution/1600-1699/1672.Richest Customer Wealth/README.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,69 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```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
6570
```
6671

6772
### **Java**
6873

6974
<!-- 这里可写当前语言的特殊实现逻辑 -->
7075

7176
```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+
```
72110
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+
}
73127
```
74128

75129
### **...**

0 commit comments

Comments
 (0)