Skip to content

Commit

Permalink
add 1415
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Apr 21, 2020
1 parent 26daa2b commit bdfc7b4
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,4 +709,5 @@ LeetCode
|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|c|[c++](./src/1410-HTML-Entity-Parser/1410.cpp)|[python](./src/1410-HTML-Entity-Parser/1410.py)|[go](./src/1410-HTML-Entity-Parser/1410.go)|[js](./src/1410-HTML-Entity-Parser/1410.js)|[java](./src/1410-HTML-Entity-Parser/1410.java)|Medium|
|1411|[Number of Ways to Paint N×3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/)|c|[c++](./src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.cpp)|[python](./src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.py)|[go](./src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.go)|[js](./src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.js)|[java](./src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.java)|Hard|
|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|
|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|
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
string getHappyString(int n, int k) {
int prem = 1 << (n - 1);
if (k > 3 * prem) return "";

k--;
string s = string(1, 'a' + k / prem);
while (prem > 1) {
k %= prem;
prem >>= 1;
s += k / prem == 0 ? 'a' + (s.back() == 'a') : 'b' + (s.back() != 'c');
}
return s;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func getHappyString(n int, k int) string {
prem := 1 << (n - 1)
if prem * 3 < k {
return ""
}

k--
s := []byte{byte(97 + k / prem)}
for prem > 1 {
k %= prem
prem >>= 1
if k / prem == 0 {
if s[len(s) - 1] == 'a' {
s = append(s, 'b')
} else {
s = append(s, 'a')
}
} else {
if s[len(s) - 1] != 'c' {
s = append(s, 'c')
} else {
s = append(s, 'b')
}
}
}
return string(s)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public String getHappyString(int n, int k) {
int prem = 1 << (n - 1);
if (k > 3 * prem) return "";

k--;
StringBuilder s = new StringBuilder();
s.append((char)(97 + k / prem));
while (prem > 1) {
k %= prem;
prem >>= 1;
s.append(k / prem == 0 ? s.charAt(s.length() - 1) == 'a' ? 'b' : 'a' :
s.charAt(s.length() - 1) != 'c' ? 'c' : 'b');
}
return s.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var getHappyString = function(n, k) {
let prem = 1 << (n - 1);
if (k > 3 * prem) return "";

k--;
let s = String.fromCharCode(97 + Math.floor(k / prem));
while (prem > 1) {
k %= prem;
prem >>= 1;
s += Math.floor(k / prem) == 0 ? s.charAt(s.length - 1) == 'a' ? 'b' : 'a' :
s.charAt(s.length - 1) != 'c' ? 'c' : 'b';
}
return s;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def getHappyString(self, n: int, k: int) -> str:
prem = 1 << (n - 1)
if prem * 3 < k: return ""

k -= 1
s = chr(97 + k // prem)
while prem > 1:
k %= prem
prem >>= 1
s += chr(97 + int(s[-1] == 'a')) if k // prem == 0 else chr(98 + int(s[-1] != 'c'))
return s
6 changes: 3 additions & 3 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os, bisect

# 题目名称
name = "Find the Minimum Number of Fibonacci Numbers Whose Sum Is K"
ID = 1414
url = "https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/"
name = "The k-th Lexicographical String of All Happy Strings of Length n"
ID = 1415
url = "https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/"
difficult = "Medium"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']

Expand Down

0 comments on commit bdfc7b4

Please sign in to comment.