Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.2239~2242
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yanglbme committed Jun 4, 2022
1 parent 49f435e commit ad90370
Show file tree
Hide file tree
Showing 19 changed files with 529 additions and 10 deletions.
63 changes: 62 additions & 1 deletion solution/2200-2299/2239.Find Closest Number to Zero/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,76 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def findClosestNumber(self, nums: List[int]) -> int:
ans, d = 0, 1000000
for v in nums:
if (t := abs(v)) < d or (t == d and v > ans):
ans, d = v, t
return ans
```

### **Java**

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

```java
class Solution {
public int findClosestNumber(int[] nums) {
int ans = 0, d = 1000000;
for (int v : nums) {
int t = Math.abs(v);
if (t < d || (t == d && v > ans)) {
ans = v;
d = t;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int findClosestNumber(vector<int>& nums) {
int ans = 0, d = 1e6;
for (int& v : nums)
{
int t = abs(v);
if (t < d || (t == d && v > ans))
{
ans = v;
d = t;
}
}
return ans;
}
};
```
### **Go**
```go
func findClosestNumber(nums []int) int {
ans, d := 0, 1000000
for _, v := range nums {
t := abs(v)
if t < d || (t == d && v > ans) {
ans, d = v, t
}
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **TypeScript**
Expand Down
63 changes: 62 additions & 1 deletion solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,74 @@ Thus, the closest number to 0 in the array is 1.
### **Python3**

```python

class Solution:
def findClosestNumber(self, nums: List[int]) -> int:
ans, d = 0, 1000000
for v in nums:
if (t := abs(v)) < d or (t == d and v > ans):
ans, d = v, t
return ans
```

### **Java**

```java
class Solution {
public int findClosestNumber(int[] nums) {
int ans = 0, d = 1000000;
for (int v : nums) {
int t = Math.abs(v);
if (t < d || (t == d && v > ans)) {
ans = v;
d = t;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int findClosestNumber(vector<int>& nums) {
int ans = 0, d = 1e6;
for (int& v : nums)
{
int t = abs(v);
if (t < d || (t == d && v > ans))
{
ans = v;
d = t;
}
}
return ans;
}
};
```
### **Go**
```go
func findClosestNumber(nums []int) int {
ans, d := 0, 1000000
for _, v := range nums {
t := abs(v)
if t < d || (t == d && v > ans) {
ans, d = v, t
}
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **TypeScript**
Expand Down
16 changes: 16 additions & 0 deletions solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int findClosestNumber(vector<int>& nums) {
int ans = 0, d = 1e6;
for (int& v : nums)
{
int t = abs(v);
if (t < d || (t == d && v > ans))
{
ans = v;
d = t;
}
}
return ans;
}
};
17 changes: 17 additions & 0 deletions solution/2200-2299/2239.Find Closest Number to Zero/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func findClosestNumber(nums []int) int {
ans, d := 0, 1000000
for _, v := range nums {
t := abs(v)
if t < d || (t == d && v > ans) {
ans, d = v, t
}
}
return ans
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
13 changes: 13 additions & 0 deletions solution/2200-2299/2239.Find Closest Number to Zero/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int findClosestNumber(int[] nums) {
int ans = 0, d = 1000000;
for (int v : nums) {
int t = Math.abs(v);
if (t < d || (t == d && v > ans)) {
ans = v;
d = t;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def findClosestNumber(self, nums: List[int]) -> int:
ans, d = 0, 1000000
for v in nums:
if (t := abs(v)) < d or (t == d and v > ans):
ans, d = v, t
return ans
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,60 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
ans = 0
for x in range(total // cost1 + 1):
v = total - x * cost1
ans += v // cost2 + 1
return ans
```

### **Java**

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

```java
class Solution {
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
long ans = 0;
for (int x = 0; x <= total / cost1; ++x) {
int v = total - x * cost1;
ans += v / cost2 + 1;
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
long long ans = 0;
for (int x = 0; x <= total / cost1; ++x)
{
int v = total - x * cost1;
ans += v / cost2 + 1;
}
return ans;
}
};
```
### **Go**
```go
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
var ans int64
for x := 0; x <= total/cost1; x++ {
v := total - x*cost1
ans += int64(v/cost2 + 1)
}
return ans
}
```

### **TypeScript**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,58 @@ The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
### **Python3**

```python

class Solution:
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
ans = 0
for x in range(total // cost1 + 1):
v = total - x * cost1
ans += v // cost2 + 1
return ans
```

### **Java**

```java
class Solution {
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
long ans = 0;
for (int x = 0; x <= total / cost1; ++x) {
int v = total - x * cost1;
ans += v / cost2 + 1;
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
long long ans = 0;
for (int x = 0; x <= total / cost1; ++x)
{
int v = total - x * cost1;
ans += v / cost2 + 1;
}
return ans;
}
};
```
### **Go**
```go
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
var ans int64
for x := 0; x <= total/cost1; x++ {
v := total - x*cost1
ans += int64(v/cost2 + 1)
}
return ans
}
```

### **TypeScript**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
long long ans = 0;
for (int x = 0; x <= total / cost1; ++x)
{
int v = total - x * cost1;
ans += v / cost2 + 1;
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func waysToBuyPensPencils(total int, cost1 int, cost2 int) int64 {
var ans int64
for x := 0; x <= total/cost1; x++ {
v := total - x*cost1
ans += int64(v/cost2 + 1)
}
return ans
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
long ans = 0;
for (int x = 0; x <= total / cost1; ++x) {
int v = total - x * cost1;
ans += v / cost2 + 1;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
ans = 0
for x in range(total // cost1 + 1):
v = total - x * cost1
ans += v // cost2 + 1
return ans
Loading

0 comments on commit ad90370

Please sign in to comment.