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: update solutions to lc problems: No.0859,0860
* No.0859.Buddy Strings * No.0860.Lemonade Change
- Loading branch information
Showing
9 changed files
with
219 additions
and
70 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution { | ||
public: | ||
bool lemonadeChange(vector<int>& bills) { | ||
int five = 0, ten = 0; | ||
for (int v : bills) | ||
{ | ||
if (v == 5) ++five; | ||
else if (v == 10) | ||
{ | ||
++ten; | ||
--five; | ||
} | ||
else | ||
{ | ||
if (ten) --ten, --five; | ||
else five -= 3; | ||
} | ||
if (five < 0) return false; | ||
} | ||
return true; | ||
} | ||
}; |
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,22 @@ | ||
func lemonadeChange(bills []int) bool { | ||
five, ten := 0, 0 | ||
for _, v := range bills { | ||
if v == 5 { | ||
five++ | ||
} else if v == 10 { | ||
ten++ | ||
five-- | ||
} else { | ||
if ten > 0 { | ||
ten-- | ||
five-- | ||
} else { | ||
five -= 3 | ||
} | ||
} | ||
if five < 0 { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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 |
---|---|---|
@@ -1,25 +1,24 @@ | ||
class Solution { | ||
public boolean lemonadeChange(int[] bills) { | ||
int fives = 0, tens = 0; | ||
for (int bill : bills) { | ||
if (bill == 5) { | ||
++fives; | ||
} else if (bill == 10) { | ||
++tens; | ||
if (--fives < 0) { | ||
return false; | ||
} | ||
int five = 0, ten = 0; | ||
for (int v : bills) { | ||
if (v == 5) { | ||
++five; | ||
} else if (v == 10) { | ||
++ten; | ||
--five; | ||
} else { | ||
if (tens >= 1 && fives >= 1) { | ||
--tens; | ||
--fives; | ||
} else if (fives >= 3) { | ||
fives -= 3; | ||
if (ten > 0) { | ||
--ten; | ||
--five; | ||
} else { | ||
return false; | ||
five -= 3; | ||
} | ||
} | ||
if (five < 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} |
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