Skip to content

Commit ab77f1f

Browse files
committed
feat: add solutions to lc problems: No.1165,1741
1 parent 3f1eeb9 commit ab77f1f

File tree

9 files changed

+218
-6
lines changed

9 files changed

+218
-6
lines changed

Diff for: solution/1100-1199/1165.Single-Row Keyboard/README.md

+68-2
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,93 @@
4646
<li><code>word[i]</code>&nbsp;是一个小写英文字母</li>
4747
</ul>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
5352

53+
哈希表实现。
54+
5455
<!-- tabs:start -->
5556

5657
### **Python3**
5758

5859
<!-- 这里可写当前语言的特殊实现逻辑 -->
5960

6061
```python
61-
62+
class Solution:
63+
def calculateTime(self, keyboard: str, word: str) -> int:
64+
index = {c: i for i, c in enumerate(keyboard)}
65+
res = t = 0
66+
for c in word:
67+
res += abs(index[c] - t)
68+
t = index[c]
69+
return res
6270
```
6371

6472
### **Java**
6573

6674
<!-- 这里可写当前语言的特殊实现逻辑 -->
6775

6876
```java
77+
class Solution {
78+
public int calculateTime(String keyboard, String word) {
79+
Map<Character, Integer> index = new HashMap<>();
80+
for (int i = 0; i < keyboard.length(); ++i) {
81+
index.put(keyboard.charAt(i), i);
82+
}
83+
int res = 0, t = 0;
84+
for (char c : word.toCharArray()) {
85+
res += Math.abs(index.get(c) - t);
86+
t = index.get(c);
87+
}
88+
return res;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int calculateTime(string keyboard, string word) {
99+
unordered_map <char, int> index;
100+
for (int i = 0; i < keyboard.size(); ++i) {
101+
index[keyboard[i]] = i;
102+
}
103+
int res = 0, t = 0;
104+
for (char c : word) {
105+
res += abs(index[c] - t);
106+
t = index[c];
107+
}
108+
return res;
109+
}
110+
};
111+
```
69112
113+
### **Go**
114+
115+
```go
116+
func calculateTime(keyboard string, word string) int {
117+
index := map[byte]int{}
118+
for i := 0; i < len(keyboard); i++ {
119+
index[keyboard[i]] = i
120+
}
121+
res := 0
122+
t := 0
123+
for i := 0; i < len(word); i++ {
124+
res += abs(index[word[i]] - t)
125+
t = index[word[i]]
126+
}
127+
return res
128+
}
129+
130+
func abs(x int) int {
131+
if x < 0 {
132+
return -x
133+
}
134+
return x
135+
}
70136
```
71137

72138
### **...**

Diff for: solution/1100-1199/1165.Single-Row Keyboard/README_EN.md

+66-2
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,85 @@ Total time = 2 + 1 + 1 = 4.
3737
<li><code>word[i]</code> is an English lowercase letter.</li>
3838
</ul>
3939

40-
4140
## Solutions
4241

4342
<!-- tabs:start -->
4443

4544
### **Python3**
4645

4746
```python
48-
47+
class Solution:
48+
def calculateTime(self, keyboard: str, word: str) -> int:
49+
index = {c: i for i, c in enumerate(keyboard)}
50+
res = t = 0
51+
for c in word:
52+
res += abs(index[c] - t)
53+
t = index[c]
54+
return res
4955
```
5056

5157
### **Java**
5258

5359
```java
60+
class Solution {
61+
public int calculateTime(String keyboard, String word) {
62+
Map<Character, Integer> index = new HashMap<>();
63+
for (int i = 0; i < keyboard.length(); ++i) {
64+
index.put(keyboard.charAt(i), i);
65+
}
66+
int res = 0, t = 0;
67+
for (char c : word.toCharArray()) {
68+
res += Math.abs(index.get(c) - t);
69+
t = index.get(c);
70+
}
71+
return res;
72+
}
73+
}
74+
```
75+
76+
### **C++**
77+
78+
```cpp
79+
class Solution {
80+
public:
81+
int calculateTime(string keyboard, string word) {
82+
unordered_map <char, int> index;
83+
for (int i = 0; i < keyboard.size(); ++i) {
84+
index[keyboard[i]] = i;
85+
}
86+
int res = 0, t = 0;
87+
for (char c : word) {
88+
res += abs(index[c] - t);
89+
t = index[c];
90+
}
91+
return res;
92+
}
93+
};
94+
```
5495
96+
### **Go**
97+
98+
```go
99+
func calculateTime(keyboard string, word string) int {
100+
index := map[byte]int{}
101+
for i := 0; i < len(keyboard); i++ {
102+
index[keyboard[i]] = i
103+
}
104+
res := 0
105+
t := 0
106+
for i := 0; i < len(word); i++ {
107+
res += abs(index[word[i]] - t)
108+
t = index[word[i]]
109+
}
110+
return res
111+
}
112+
113+
func abs(x int) int {
114+
if x < 0 {
115+
return -x
116+
}
117+
return x
118+
}
55119
```
56120

57121
### **...**
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int calculateTime(string keyboard, string word) {
4+
unordered_map <char, int> index;
5+
for (int i = 0; i < keyboard.size(); ++i) {
6+
index[keyboard[i]] = i;
7+
}
8+
int res = 0, t = 0;
9+
for (char c : word) {
10+
res += abs(index[c] - t);
11+
t = index[c];
12+
}
13+
return res;
14+
}
15+
};
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func calculateTime(keyboard string, word string) int {
2+
index := map[byte]int{}
3+
for i := 0; i < len(keyboard); i++ {
4+
index[keyboard[i]] = i
5+
}
6+
res := 0
7+
t := 0
8+
for i := 0; i < len(word); i++ {
9+
res += abs(index[word[i]] - t)
10+
t = index[word[i]]
11+
}
12+
return res
13+
}
14+
15+
func abs(x int) int {
16+
if x < 0 {
17+
return -x
18+
}
19+
return x
20+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int calculateTime(String keyboard, String word) {
3+
Map<Character, Integer> index = new HashMap<>();
4+
for (int i = 0; i < keyboard.length(); ++i) {
5+
index.put(keyboard.charAt(i), i);
6+
}
7+
int res = 0, t = 0;
8+
for (char c : word.toCharArray()) {
9+
res += Math.abs(index.get(c) - t);
10+
t = index.get(c);
11+
}
12+
return res;
13+
}
14+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def calculateTime(self, keyboard: str, word: str) -> int:
3+
index = {c: i for i, c in enumerate(keyboard)}
4+
res = t = 0
5+
for c in word:
6+
res += abs(index[c] - t)
7+
t = index[c]
8+
return res

Diff for: solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ Result table:
6363
### **SQL**
6464

6565
```sql
66-
66+
# Write your MySQL query statement below
67+
SELECT
68+
event_day AS day,
69+
emp_id,
70+
SUM(out_time - in_time) AS total_time
71+
FROM
72+
Employees
73+
GROUP BY
74+
emp_id, event_day;
6775
```
6876

6977
<!-- tabs:end -->

Diff for: solution/1700-1799/1741.Find Total Time Spent by Each Employee/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ Employee 2 has two events: one on day 2020-11-28 with a total of (33 - 3) = 30,
6262
### **SQL**
6363

6464
```sql
65-
65+
# Write your MySQL query statement below
66+
SELECT
67+
event_day AS day,
68+
emp_id,
69+
SUM(out_time - in_time) AS total_time
70+
FROM
71+
Employees
72+
GROUP BY
73+
emp_id, event_day;
6674
```
6775

6876
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
event_day AS day,
4+
emp_id,
5+
SUM(out_time - in_time) AS total_time
6+
FROM
7+
Employees
8+
GROUP BY
9+
emp_id, event_day;

0 commit comments

Comments
 (0)