forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
19 changed files
with
529 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
solution/2200-2299/2239.Find Closest Number to Zero/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
solution/2200-2299/2239.Find Closest Number to Zero/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
solution/2200-2299/2239.Find Closest Number to Zero/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
8
solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
10 changes: 10 additions & 0 deletions
10
solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.