Skip to content

Commit 54fe618

Browse files
add 1165
1 parent f3d5a42 commit 54fe618

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,5 @@ LeetCode
482482
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/) | c | [c++](./src/1160-Find-Words-That-Can-Be-Formed-by-Characters/1160.cpp) |[python](./src/1160-Find-Words-That-Can-Be-Formed-by-Characters/1160.py)|[go](./src/1160-Find-Words-That-Can-Be-Formed-by-Characters/1160.go)|[js](./src/1160-Find-Words-That-Can-Be-Formed-by-Characters/1160.js)|Easy|
483483
|1161|[Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) | c | [c++](./src/1161-Maximum-Level-Sum-of-a-Binary-Tree/1161.cpp) |[python](./src/1161-Maximum-Level-Sum-of-a-Binary-Tree/1161.py)|[go](./src/1161-Maximum-Level-Sum-of-a-Binary-Tree/1161.go)|[js](./src/1161-Maximum-Level-Sum-of-a-Binary-Tree/1161.js)|Medium|
484484
|1162|[As Far from Land as Possible](https://leetcode.com/problems/as-far-from-land-as-possible/) | c | [c++](./src/1162-As-Far-from-Land-as-Possible/1162.cpp) |[python](./src/1162-As-Far-from-Land-as-Possible/1162.py)|[go](./src/1162-As-Far-from-Land-as-Possible/1162.go)|[js](./src/1162-As-Far-from-Land-as-Possible/1162.js)|Medium|
485-
|1163|[Last Substring in Lexicographical Order](https://leetcode.com/problems/last-substring-in-lexicographical-order/) | c | [c++](./src/1163-Last-Substring-in-Lexicographical-Order/1163.cpp) |[python](./src/1163-Last-Substring-in-Lexicographical-Order/1163.py)|[go](./src/1163-Last-Substring-in-Lexicographical-Order/1163.go)|[js](./src/1163-Last-Substring-in-Lexicographical-Order/1163.js)|Hard|
485+
|1163|[Last Substring in Lexicographical Order](https://leetcode.com/problems/last-substring-in-lexicographical-order/) | c | [c++](./src/1163-Last-Substring-in-Lexicographical-Order/1163.cpp) |[python](./src/1163-Last-Substring-in-Lexicographical-Order/1163.py)|[go](./src/1163-Last-Substring-in-Lexicographical-Order/1163.go)|[js](./src/1163-Last-Substring-in-Lexicographical-Order/1163.js)|Hard|
486+
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/) | c | [c++](./src/1165-Single-Row-Keyboard/1165.cpp) |[python](./src/1165-Single-Row-Keyboard/1165.py)|[go](./src/1165-Single-Row-Keyboard/1165.go)|[js](./src/1165-Single-Row-Keyboard/1165.js)|Easy|

src/1165-Single-Row-Keyboard/1165.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution
2+
{
3+
public:
4+
int calculateTime(string keyboard, string word)
5+
{
6+
unordered_map<char, int> d;
7+
for (int i = 0; i < keyboard.size(); ++i)
8+
{
9+
d[keyboard[i]] = i;
10+
}
11+
12+
int res = 0, pre = 0;
13+
for (char c : word)
14+
{
15+
res += abs(d[c] - pre);
16+
pre = d[c];
17+
}
18+
return res;
19+
}
20+
};

src/1165-Single-Row-Keyboard/1165.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func calculateTime(keyboard string, word string) int {
2+
d := make(map[rune]int)
3+
for i, c := range keyboard {
4+
d[c] = i
5+
}
6+
7+
res, pre := 0, 0
8+
for _, c := range word {
9+
if d[c] > pre {
10+
res += d[c] - pre
11+
} else {
12+
res += pre - d[c]
13+
}
14+
pre = d[c]
15+
}
16+
return res
17+
}

src/1165-Single-Row-Keyboard/1165.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var calculateTime = function(keyboard, word) {
2+
var d = new Map();
3+
for (var i = 0; i < keyboard.length; ++i) {
4+
d.set(keyboard[i], i);
5+
}
6+
7+
var res = 0, pre = 0;
8+
for (let c of word) {
9+
res += Math.abs(d.get(c) - pre);
10+
pre = d.get(c);
11+
}
12+
return res;
13+
};

src/1165-Single-Row-Keyboard/1165.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def calculateTime(self, keyboard: str, word: str) -> int:
3+
d = dict()
4+
for i, v in enumerate(keyboard):
5+
d[v] = i
6+
7+
res, pre = 0, 0
8+
for c in word:
9+
res += abs(d[c] - pre)
10+
pre = d[c]
11+
return res

0 commit comments

Comments
 (0)