From b35d2102c57e5d46fd39de1b8b2b0493ca4f20e0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 23 Sep 2023 17:10:57 +0800 Subject: [PATCH 1/2] feat: update solutions to lc problem: No.1518 No.1518.Water Bottles --- .prettierignore | 1 - .prettierrc | 1 + .../1500-1599/1518.Water Bottles/README.md | 36 +++++++++++-------- .../1500-1599/1518.Water Bottles/README_EN.md | 36 +++++++++++-------- .../1500-1599/1518.Water Bottles/Solution.cpp | 19 +++++----- .../1500-1599/1518.Water Bottles/Solution.go | 3 +- .../1518.Water Bottles/Solution.java | 17 +++++---- .../1500-1599/1518.Water Bottles/Solution.js | 9 +++-- .../1500-1599/1518.Water Bottles/Solution.php | 6 ++-- .../1500-1599/1518.Water Bottles/Solution.ts | 7 ++++ .../1603.Design Parking System/README.md | 4 +-- .../1603.Design Parking System/README_EN.md | 5 ++- 12 files changed, 81 insertions(+), 63 deletions(-) create mode 100644 solution/1500-1599/1518.Water Bottles/Solution.ts diff --git a/.prettierignore b/.prettierignore index 2fc575e1538bf..1eb3d9c657ff6 100644 --- a/.prettierignore +++ b/.prettierignore @@ -14,7 +14,6 @@ node_modules/ /solution/bash_problem_readme_template.md /solution/bash_problem_readme_template_en.md /solution/0100-0199/0177.Nth Highest Salary/Solution.sql -/solution/0600-0699/0627.Swap Salary/Solution.sql /solution/1400-1499/1454.Active Users/Solution.sql /solution/1400-1499/1484.Group Sold Products By The Date/Solution.sql /solution/1500-1599/1511.Customer Order Frequency/Solution.sql diff --git a/.prettierrc b/.prettierrc index 544e9e1a2aef0..7cfd981cf6802 100644 --- a/.prettierrc +++ b/.prettierrc @@ -19,6 +19,7 @@ "solution/0600-0699/0610.Triangle Judgement/Solution.sql", "solution/0600-0699/0618.Students Report By Geography/Solution.sql", "solution/0600-0699/0626.Exchange Seats/Solution.sql", + "solution/0600-0699/0627.Swap Salary/Solution.sql", "solution/1000-1099/1097.Game Play Analysis V/Solution.sql", "solution/1000-1099/1098.Unpopular Books/Solution.sql", "solution/1100-1199/1113.Reported Posts/Solution.sql", diff --git a/solution/1500-1599/1518.Water Bottles/README.md b/solution/1500-1599/1518.Water Bottles/README.md index b34069ce0e8ec..70ffb7413f044 100644 --- a/solution/1500-1599/1518.Water Bottles/README.md +++ b/solution/1500-1599/1518.Water Bottles/README.md @@ -87,9 +87,8 @@ class Solution: class Solution { public int numWaterBottles(int numBottles, int numExchange) { int ans = numBottles; - while (numBottles >= numExchange) { + for (; numBottles >= numExchange; ++ans) { numBottles -= (numExchange - 1); - ++ans; } return ans; } @@ -103,9 +102,8 @@ class Solution { public: int numWaterBottles(int numBottles, int numExchange) { int ans = numBottles; - while (numBottles >= numExchange) { + for (; numBottles >= numExchange; ++ans) { numBottles -= (numExchange - 1); - ++ans; } return ans; } @@ -117,14 +115,25 @@ public: ```go func numWaterBottles(numBottles int, numExchange int) int { ans := numBottles - for numBottles >= numExchange { + for ; numBottles >= numExchange; ans++ { numBottles -= (numExchange - 1) - ans++ } return ans } ``` +### **TypeScript** + +```ts +function numWaterBottles(numBottles: number, numExchange: number): number { + let ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= numExchange - 1; + } + return ans; +} +``` + ### **PHP** ```php @@ -135,12 +144,12 @@ class Solution { * @return Integer */ function numWaterBottles($numBottles, $numExchange) { - $sum = $numBottles; + $ans = $numBottles; while ($numBottles >= $numExchange) { $numBottles = $numBottles - $numExchange + 1; - $sum++; + $ans++; } - return $sum; + return $ans; } } ``` @@ -154,12 +163,11 @@ class Solution { * @return {number} */ var numWaterBottles = function (numBottles, numExchange) { - let sum = numBottles; - while (numBottles >= numExchange) { - numBottles = numBottles - numExchange + 1; - sum++; + let ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= numExchange - 1; } - return sum; + return ans; }; ``` diff --git a/solution/1500-1599/1518.Water Bottles/README_EN.md b/solution/1500-1599/1518.Water Bottles/README_EN.md index a9bdf2dd9d2d7..8712e5e225ee5 100644 --- a/solution/1500-1599/1518.Water Bottles/README_EN.md +++ b/solution/1500-1599/1518.Water Bottles/README_EN.md @@ -59,9 +59,8 @@ class Solution: class Solution { public int numWaterBottles(int numBottles, int numExchange) { int ans = numBottles; - while (numBottles >= numExchange) { + for (; numBottles >= numExchange; ++ans) { numBottles -= (numExchange - 1); - ++ans; } return ans; } @@ -75,9 +74,8 @@ class Solution { public: int numWaterBottles(int numBottles, int numExchange) { int ans = numBottles; - while (numBottles >= numExchange) { + for (; numBottles >= numExchange; ++ans) { numBottles -= (numExchange - 1); - ++ans; } return ans; } @@ -89,14 +87,25 @@ public: ```go func numWaterBottles(numBottles int, numExchange int) int { ans := numBottles - for numBottles >= numExchange { + for ; numBottles >= numExchange; ans++ { numBottles -= (numExchange - 1) - ans++ } return ans } ``` +### **TypeScript** + +```ts +function numWaterBottles(numBottles: number, numExchange: number): number { + let ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= numExchange - 1; + } + return ans; +} +``` + ### **PHP** ```php @@ -107,12 +116,12 @@ class Solution { * @return Integer */ function numWaterBottles($numBottles, $numExchange) { - $sum = $numBottles; + $ans = $numBottles; while ($numBottles >= $numExchange) { $numBottles = $numBottles - $numExchange + 1; - $sum++; + $ans++; } - return $sum; + return $ans; } } ``` @@ -126,12 +135,11 @@ class Solution { * @return {number} */ var numWaterBottles = function (numBottles, numExchange) { - let sum = numBottles; - while (numBottles >= numExchange) { - numBottles = numBottles - numExchange + 1; - sum++; + let ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= numExchange - 1; } - return sum; + return ans; }; ``` diff --git a/solution/1500-1599/1518.Water Bottles/Solution.cpp b/solution/1500-1599/1518.Water Bottles/Solution.cpp index 0da95016721b2..c507ac0b6f88d 100644 --- a/solution/1500-1599/1518.Water Bottles/Solution.cpp +++ b/solution/1500-1599/1518.Water Bottles/Solution.cpp @@ -1,11 +1,10 @@ -class Solution { -public: - int numWaterBottles(int numBottles, int numExchange) { - int ans = numBottles; - while (numBottles >= numExchange) { - numBottles -= (numExchange - 1); - ++ans; - } - return ans; - } +class Solution { +public: + int numWaterBottles(int numBottles, int numExchange) { + int ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= (numExchange - 1); + } + return ans; + } }; \ No newline at end of file diff --git a/solution/1500-1599/1518.Water Bottles/Solution.go b/solution/1500-1599/1518.Water Bottles/Solution.go index 811e4ccc8ae4f..ef4e296d16ecc 100644 --- a/solution/1500-1599/1518.Water Bottles/Solution.go +++ b/solution/1500-1599/1518.Water Bottles/Solution.go @@ -1,8 +1,7 @@ func numWaterBottles(numBottles int, numExchange int) int { ans := numBottles - for numBottles >= numExchange { + for ; numBottles >= numExchange; ans++ { numBottles -= (numExchange - 1) - ans++ } return ans } \ No newline at end of file diff --git a/solution/1500-1599/1518.Water Bottles/Solution.java b/solution/1500-1599/1518.Water Bottles/Solution.java index e913a6c7b58e9..f0af87acfe923 100644 --- a/solution/1500-1599/1518.Water Bottles/Solution.java +++ b/solution/1500-1599/1518.Water Bottles/Solution.java @@ -1,10 +1,9 @@ -class Solution { - public int numWaterBottles(int numBottles, int numExchange) { - int ans = numBottles; - while (numBottles >= numExchange) { - numBottles -= (numExchange - 1); - ++ans; - } - return ans; - } +class Solution { + public int numWaterBottles(int numBottles, int numExchange) { + int ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= (numExchange - 1); + } + return ans; + } } \ No newline at end of file diff --git a/solution/1500-1599/1518.Water Bottles/Solution.js b/solution/1500-1599/1518.Water Bottles/Solution.js index 0d39c82af1935..b5cfaf50b5185 100644 --- a/solution/1500-1599/1518.Water Bottles/Solution.js +++ b/solution/1500-1599/1518.Water Bottles/Solution.js @@ -4,10 +4,9 @@ * @return {number} */ var numWaterBottles = function (numBottles, numExchange) { - let sum = numBottles; - while (numBottles >= numExchange) { - numBottles = numBottles - numExchange + 1; - sum++; + let ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= numExchange - 1; } - return sum; + return ans; }; diff --git a/solution/1500-1599/1518.Water Bottles/Solution.php b/solution/1500-1599/1518.Water Bottles/Solution.php index b4bafe2f89a9b..f94f316647fbd 100644 --- a/solution/1500-1599/1518.Water Bottles/Solution.php +++ b/solution/1500-1599/1518.Water Bottles/Solution.php @@ -5,11 +5,11 @@ class Solution { * @return Integer */ function numWaterBottles($numBottles, $numExchange) { - $sum = $numBottles; + $ans = $numBottles; while ($numBottles >= $numExchange) { $numBottles = $numBottles - $numExchange + 1; - $sum++; + $ans++; } - return $sum; + return $ans; } } \ No newline at end of file diff --git a/solution/1500-1599/1518.Water Bottles/Solution.ts b/solution/1500-1599/1518.Water Bottles/Solution.ts new file mode 100644 index 0000000000000..b9bdede3bba7c --- /dev/null +++ b/solution/1500-1599/1518.Water Bottles/Solution.ts @@ -0,0 +1,7 @@ +function numWaterBottles(numBottles: number, numExchange: number): number { + let ans = numBottles; + for (; numBottles >= numExchange; ++ans) { + numBottles -= numExchange - 1; + } + return ans; +} diff --git a/solution/1600-1699/1603.Design Parking System/README.md b/solution/1600-1699/1603.Design Parking System/README.md index eb7084d484f8e..6157608375ed4 100644 --- a/solution/1600-1699/1603.Design Parking System/README.md +++ b/solution/1600-1699/1603.Design Parking System/README.md @@ -263,11 +263,11 @@ void parkingSystemFree(ParkingSystem* obj) { public class ParkingSystem { private List cnt; - + public ParkingSystem(int big, int medium, int small) { cnt = new List() {0 , big, medium, small}; } - + public bool AddCar(int carType) { if (cnt[carType] == 0) { return false; diff --git a/solution/1600-1699/1603.Design Parking System/README_EN.md b/solution/1600-1699/1603.Design Parking System/README_EN.md index 2388bd93d4036..bcd209d5e6c47 100644 --- a/solution/1600-1699/1603.Design Parking System/README_EN.md +++ b/solution/1600-1699/1603.Design Parking System/README_EN.md @@ -247,11 +247,11 @@ void parkingSystemFree(ParkingSystem* obj) { public class ParkingSystem { private List cnt; - + public ParkingSystem(int big, int medium, int small) { cnt = new List() {0 , big, medium, small}; } - + public bool AddCar(int carType) { if (cnt[carType] == 0) { return false; @@ -268,7 +268,6 @@ public class ParkingSystem { */ ``` - ### **...** ``` From 1094b1523b4d54d22ab0f3d81b307f0e14ab2301 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 23 Sep 2023 17:13:56 +0800 Subject: [PATCH 2/2] fix: code style --- solution/1600-1699/1603.Design Parking System/Solution.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/1600-1699/1603.Design Parking System/Solution.cs b/solution/1600-1699/1603.Design Parking System/Solution.cs index 351e604436397..62dc3f9fdb59f 100644 --- a/solution/1600-1699/1603.Design Parking System/Solution.cs +++ b/solution/1600-1699/1603.Design Parking System/Solution.cs @@ -1,11 +1,11 @@ public class ParkingSystem { private List cnt; - + public ParkingSystem(int big, int medium, int small) { cnt = new List() {0 , big, medium, small}; } - + public bool AddCar(int carType) { if (cnt[carType] == 0) { return false;