From f127b16242b23b3d830bbca51614bb8f93060139 Mon Sep 17 00:00:00 2001 From: luliyucoordinate Date: Wed, 22 Apr 2020 10:12:55 +0800 Subject: [PATCH] add 1417 --- README.md | 3 +- src/1417-Reformat-The-String/1417.cpp | 27 +++++++++++++++++ src/1417-Reformat-The-String/1417.go | 40 ++++++++++++++++++++++++++ src/1417-Reformat-The-String/1417.java | 21 ++++++++++++++ src/1417-Reformat-The-String/1417.js | 17 +++++++++++ src/1417-Reformat-The-String/1417.py | 15 ++++++++++ src/addProb.py | 8 +++--- 7 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 src/1417-Reformat-The-String/1417.cpp create mode 100644 src/1417-Reformat-The-String/1417.go create mode 100644 src/1417-Reformat-The-String/1417.java create mode 100644 src/1417-Reformat-The-String/1417.js create mode 100644 src/1417-Reformat-The-String/1417.py diff --git a/README.md b/README.md index 033b9234..51ed5f38 100644 --- a/README.md +++ b/README.md @@ -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| \ No newline at end of file +|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| \ No newline at end of file diff --git a/src/1417-Reformat-The-String/1417.cpp b/src/1417-Reformat-The-String/1417.cpp new file mode 100644 index 00000000..0991486c --- /dev/null +++ b/src/1417-Reformat-The-String/1417.cpp @@ -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(); + } +}; \ No newline at end of file diff --git a/src/1417-Reformat-The-String/1417.go b/src/1417-Reformat-The-String/1417.go new file mode 100644 index 00000000..edad01b5 --- /dev/null +++ b/src/1417-Reformat-The-String/1417.go @@ -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 +} \ No newline at end of file diff --git a/src/1417-Reformat-The-String/1417.java b/src/1417-Reformat-The-String/1417.java new file mode 100644 index 00000000..0abb1e53 --- /dev/null +++ b/src/1417-Reformat-The-String/1417.java @@ -0,0 +1,21 @@ +class Solution { + public String reformat(String s) { + Queue digits = new ArrayDeque(); + Queue 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(); + } +} \ No newline at end of file diff --git a/src/1417-Reformat-The-String/1417.js b/src/1417-Reformat-The-String/1417.js new file mode 100644 index 00000000..5dd4d78f --- /dev/null +++ b/src/1417-Reformat-The-String/1417.js @@ -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; +}; \ No newline at end of file diff --git a/src/1417-Reformat-The-String/1417.py b/src/1417-Reformat-The-String/1417.py new file mode 100644 index 00000000..f19dc260 --- /dev/null +++ b/src/1417-Reformat-The-String/1417.py @@ -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 \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index 825b8127..72572a88 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -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']