forked from luliyucoordinate/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.
- Loading branch information
1 parent
1528f69
commit f127b16
Showing
7 changed files
with
126 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
public: | ||
string reformat(string s) { | ||
stringstream schars, snums; | ||
for (char c : s) { | ||
if (c >= '0' && c <= '9') snums << c; | ||
else schars << c; | ||
} | ||
|
||
string chars = schars.str(), nums = snums.str(); | ||
if (abs(int(chars.size() - nums.size())) > 1) return ""; | ||
stringstream res; | ||
|
||
bool flag = nums.size() >= chars.size() ? true : false; | ||
for (int i = 0; i < s.size(); i++) { | ||
if (flag) { | ||
res << nums.back(); | ||
nums.pop_back(); | ||
} else { | ||
res << chars.back(); | ||
chars.pop_back(); | ||
} | ||
flag = !flag; | ||
} | ||
return res.str(); | ||
} | ||
}; |
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,40 @@ | ||
func reformat(s string) string { | ||
digits := []rune{} | ||
chars := []rune{} | ||
for _, c := range s { | ||
if c >= '0' && c <= '9' { | ||
digits = append(digits, c) | ||
} else { | ||
chars = append(chars, c) | ||
} | ||
} | ||
|
||
if abs(len(digits) - len(chars)) > 1 { | ||
return "" | ||
} | ||
|
||
var res strings.Builder | ||
flag := false | ||
if len(digits) >= len(chars) { | ||
flag = true | ||
} | ||
|
||
for i := 0; i < len(s); i++ { | ||
if flag { | ||
res.WriteRune(digits[len(digits) - 1]) | ||
digits = digits[:len(digits) - 1] | ||
} else { | ||
res.WriteRune(chars[len(chars) - 1]) | ||
chars = chars[:len(chars) - 1] | ||
} | ||
flag = !flag | ||
} | ||
return res.String() | ||
} | ||
|
||
func abs(a int) int { | ||
if a < 0 { | ||
return -a | ||
} | ||
return a | ||
} |
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,21 @@ | ||
class Solution { | ||
public String reformat(String s) { | ||
Queue<Character> digits = new ArrayDeque(); | ||
Queue<Character> chars = new ArrayDeque(); | ||
|
||
for(char c : s.toCharArray()) { | ||
if (Character.isDigit(c)) digits.add(c); | ||
else chars.add(c); | ||
} | ||
if (Math.abs(digits.size() - chars.size()) > 1) return ""; | ||
|
||
StringBuilder res = new StringBuilder(); | ||
boolean flag = digits.size() >= chars.size() ? true : false; | ||
for (int i = 0; i < s.length(); i++){ | ||
if (flag) res.append(digits.poll()); | ||
else res.append(chars.poll()); | ||
flag = !flag; | ||
} | ||
return res.toString(); | ||
} | ||
} |
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 @@ | ||
var reformat = function(s) { | ||
let digits = [], chars = []; | ||
for(let c of s) { | ||
if (c >= '0' && c <= '9') digits.push(c); | ||
else chars.push(c); | ||
} | ||
if (Math.abs(digits.length - chars.length) > 1) return ""; | ||
|
||
let res = ""; | ||
let flag = digits.length >= chars.length ? true : false; | ||
for (let i = 0; i < s.length; i++){ | ||
if (flag) res += digits.pop(); | ||
else res += chars.pop(); | ||
flag = !flag; | ||
} | ||
return res; | ||
}; |
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,15 @@ | ||
class Solution: | ||
def reformat(self, s: str) -> str: | ||
nums, chars = [], [] | ||
for i in s: | ||
if '0' <= i <= '9': nums.append(i) | ||
else: chars.append(i) | ||
|
||
if abs(len(nums) - len(chars)) > 1: return "" | ||
res = "" | ||
flag = True if len(nums) >= len(chars) else False | ||
for _ in range(len(s)): | ||
if flag: res += nums.pop() | ||
else: res += chars.pop() | ||
flag = not flag | ||
return res |
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