Skip to content

Commit

Permalink
add 1417
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Apr 22, 2020
1 parent 1528f69 commit f127b16
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,4 +711,5 @@ LeetCode
|1413|[Minimum Value to Get Positive Step by Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)|c|[c++](./src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.cpp)|[python](./src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.py)|[go](./src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.go)|[js](./src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.js)|[java](./src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.java)|Easy|
|1414|[Find the Minimum Number of Fibonacci Numbers Whose Sum Is K](https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/)|c|[c++](./src/1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/1414.cpp)|[python](./src/1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/1414.py)|[go](./src/1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/1414.go)|[js](./src/1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/1414.js)|[java](./src/1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/1414.java)|Medium|
|1415|[The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/)|c|[c++](./src/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415.cpp)|[python](./src/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415.py)|[go](./src/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415.go)|[js](./src/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415.js)|[java](./src/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415.java)|Medium|
|1416|[Restore The Array](https://leetcode.com/problems/restore-the-array/)|c|[c++](./src/1416-Restore-The-Array/1416.cpp)|[python](./src/1416-Restore-The-Array/1416.py)|[go](./src/1416-Restore-The-Array/1416.go)|[js](./src/1416-Restore-The-Array/1416.js)|[java](./src/1416-Restore-The-Array/1416.java)|Hard|
|1416|[Restore The Array](https://leetcode.com/problems/restore-the-array/)|c|[c++](./src/1416-Restore-The-Array/1416.cpp)|[python](./src/1416-Restore-The-Array/1416.py)|[go](./src/1416-Restore-The-Array/1416.go)|[js](./src/1416-Restore-The-Array/1416.js)|[java](./src/1416-Restore-The-Array/1416.java)|Hard|
|1417|[Reformat The String](https://leetcode.com/problems/reformat-the-string/)|c|[c++](./src/1417-Reformat-The-String/1417.cpp)|[python](./src/1417-Reformat-The-String/1417.py)|[go](./src/1417-Reformat-The-String/1417.go)|[js](./src/1417-Reformat-The-String/1417.js)|[java](./src/1417-Reformat-The-String/1417.java)|Easy|
27 changes: 27 additions & 0 deletions src/1417-Reformat-The-String/1417.cpp
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();
}
};
40 changes: 40 additions & 0 deletions src/1417-Reformat-The-String/1417.go
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
}
21 changes: 21 additions & 0 deletions src/1417-Reformat-The-String/1417.java
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();
}
}
17 changes: 17 additions & 0 deletions src/1417-Reformat-The-String/1417.js
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;
};
15 changes: 15 additions & 0 deletions src/1417-Reformat-The-String/1417.py
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
8 changes: 4 additions & 4 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os, bisect

# 题目名称
name = "Restore The Array"
ID = 1416
url = "https://leetcode.com/problems/restore-the-array/"
difficult = "Hard"
name = "Reformat The String"
ID = 1417
url = "https://leetcode.com/problems/reformat-the-string/"
difficult = "Easy"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']


Expand Down

0 comments on commit f127b16

Please sign in to comment.