Skip to content

Commit ad90370

Browse files
committed
feat: add solutions to lc problems: No.2239~2242
* No.2239.Find Closest Number to Zero * No.2240.Number of Ways to Buy Pens and Pencils * No.2241.Design an ATM Machine * No.2242.Maximum Score of a Node Sequence
1 parent 49f435e commit ad90370

File tree

19 files changed

+529
-10
lines changed

19 files changed

+529
-10
lines changed

solution/2200-2299/2239.Find Closest Number to Zero/README.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,76 @@
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53-
53+
class Solution:
54+
def findClosestNumber(self, nums: List[int]) -> int:
55+
ans, d = 0, 1000000
56+
for v in nums:
57+
if (t := abs(v)) < d or (t == d and v > ans):
58+
ans, d = v, t
59+
return ans
5460
```
5561

5662
### **Java**
5763

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

6066
```java
67+
class Solution {
68+
public int findClosestNumber(int[] nums) {
69+
int ans = 0, d = 1000000;
70+
for (int v : nums) {
71+
int t = Math.abs(v);
72+
if (t < d || (t == d && v > ans)) {
73+
ans = v;
74+
d = t;
75+
}
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int findClosestNumber(vector<int>& nums) {
88+
int ans = 0, d = 1e6;
89+
for (int& v : nums)
90+
{
91+
int t = abs(v);
92+
if (t < d || (t == d && v > ans))
93+
{
94+
ans = v;
95+
d = t;
96+
}
97+
}
98+
return ans;
99+
}
100+
};
101+
```
61102
103+
### **Go**
104+
105+
```go
106+
func findClosestNumber(nums []int) int {
107+
ans, d := 0, 1000000
108+
for _, v := range nums {
109+
t := abs(v)
110+
if t < d || (t == d && v > ans) {
111+
ans, d = v, t
112+
}
113+
}
114+
return ans
115+
}
116+
117+
func abs(x int) int {
118+
if x < 0 {
119+
return -x
120+
}
121+
return x
122+
}
62123
```
63124

64125
### **TypeScript**

solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,74 @@ Thus, the closest number to 0 in the array is 1.
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def findClosestNumber(self, nums: List[int]) -> int:
48+
ans, d = 0, 1000000
49+
for v in nums:
50+
if (t := abs(v)) < d or (t == d and v > ans):
51+
ans, d = v, t
52+
return ans
4753
```
4854

4955
### **Java**
5056

5157
```java
58+
class Solution {
59+
public int findClosestNumber(int[] nums) {
60+
int ans = 0, d = 1000000;
61+
for (int v : nums) {
62+
int t = Math.abs(v);
63+
if (t < d || (t == d && v > ans)) {
64+
ans = v;
65+
d = t;
66+
}
67+
}
68+
return ans;
69+
}
70+
}
71+
```
72+
73+
### **C++**
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
int findClosestNumber(vector<int>& nums) {
79+
int ans = 0, d = 1e6;
80+
for (int& v : nums)
81+
{
82+
int t = abs(v);
83+
if (t < d || (t == d && v > ans))
84+
{
85+
ans = v;
86+
d = t;
87+
}
88+
}
89+
return ans;
90+
}
91+
};
92+
```
5293
94+
### **Go**
95+
96+
```go
97+
func findClosestNumber(nums []int) int {
98+
ans, d := 0, 1000000
99+
for _, v := range nums {
100+
t := abs(v)
101+
if t < d || (t == d && v > ans) {
102+
ans, d = v, t
103+
}
104+
}
105+
return ans
106+
}
107+
108+
func abs(x int) int {
109+
if x < 0 {
110+
return -x
111+
}
112+
return x
113+
}
53114
```
54115

55116
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int findClosestNumber(vector<int>& nums) {
4+
int ans = 0, d = 1e6;
5+
for (int& v : nums)
6+
{
7+
int t = abs(v);
8+
if (t < d || (t == d && v > ans))
9+
{
10+
ans = v;
11+
d = t;
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func findClosestNumber(nums []int) int {
2+
ans, d := 0, 1000000
3+
for _, v := range nums {
4+
t := abs(v)
5+
if t < d || (t == d && v > ans) {
6+
ans, d = v, t
7+
}
8+
}
9+
return ans
10+
}
11+
12+
func abs(x int) int {
13+
if x < 0 {
14+
return -x
15+
}
16+
return x
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int findClosestNumber(int[] nums) {
3+
int ans = 0, d = 1000000;
4+
for (int v : nums) {
5+
int t = Math.abs(v);
6+
if (t < d || (t == d && v > ans)) {
7+
ans = v;
8+
d = t;
9+
}
10+
}
11+
return ans;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def findClosestNumber(self, nums: List[int]) -> int:
3+
ans, d = 0, 1000000
4+
for v in nums:
5+
if (t := abs(v)) < d or (t == d and v > ans):
6+
ans, d = v, t
7+
return ans

solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,60 @@
4949
<!-- 这里可写当前语言的特殊实现逻辑 -->
5050

5151
```python
52-
52+
class Solution:
53+
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
54+
ans = 0
55+
for x in range(total // cost1 + 1):
56+
v = total - x * cost1
57+
ans += v // cost2 + 1
58+
return ans
5359
```
5460

5561
### **Java**
5662

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

5965
```java
66+
class Solution {
67+
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
68+
long ans = 0;
69+
for (int x = 0; x <= total / cost1; ++x) {
70+
int v = total - x * cost1;
71+
ans += v / cost2 + 1;
72+
}
73+
return ans;
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
84+
long long ans = 0;
85+
for (int x = 0; x <= total / cost1; ++x)
86+
{
87+
int v = total - x * cost1;
88+
ans += v / cost2 + 1;
89+
}
90+
return ans;
91+
}
92+
};
93+
```
6094
95+
### **Go**
96+
97+
```go
98+
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
99+
var ans int64
100+
for x := 0; x <= total/cost1; x++ {
101+
v := total - x*cost1
102+
ans += int64(v/cost2 + 1)
103+
}
104+
return ans
105+
}
61106
```
62107

63108
### **TypeScript**

solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,58 @@ The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
48+
ans = 0
49+
for x in range(total // cost1 + 1):
50+
v = total - x * cost1
51+
ans += v // cost2 + 1
52+
return ans
4753
```
4854

4955
### **Java**
5056

5157
```java
58+
class Solution {
59+
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
60+
long ans = 0;
61+
for (int x = 0; x <= total / cost1; ++x) {
62+
int v = total - x * cost1;
63+
ans += v / cost2 + 1;
64+
}
65+
return ans;
66+
}
67+
}
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
76+
long long ans = 0;
77+
for (int x = 0; x <= total / cost1; ++x)
78+
{
79+
int v = total - x * cost1;
80+
ans += v / cost2 + 1;
81+
}
82+
return ans;
83+
}
84+
};
85+
```
5286
87+
### **Go**
88+
89+
```go
90+
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
91+
var ans int64
92+
for x := 0; x <= total/cost1; x++ {
93+
v := total - x*cost1
94+
ans += int64(v/cost2 + 1)
95+
}
96+
return ans
97+
}
5398
```
5499

55100
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
4+
long long ans = 0;
5+
for (int x = 0; x <= total / cost1; ++x)
6+
{
7+
int v = total - x * cost1;
8+
ans += v / cost2 + 1;
9+
}
10+
return ans;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
2+
var ans int64
3+
for x := 0; x <= total/cost1; x++ {
4+
v := total - x*cost1
5+
ans += int64(v/cost2 + 1)
6+
}
7+
return ans
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
3+
long ans = 0;
4+
for (int x = 0; x <= total / cost1; ++x) {
5+
int v = total - x * cost1;
6+
ans += v / cost2 + 1;
7+
}
8+
return ans;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
3+
ans = 0
4+
for x in range(total // cost1 + 1):
5+
v = total - x * cost1
6+
ans += v // cost2 + 1
7+
return ans

0 commit comments

Comments
 (0)