Skip to content

Commit db8cc79

Browse files
authored
feat: add solutions to lc problem: No.3307 (#4545)
No.3307.Find the K-th Character in String Game II
1 parent c692eef commit db8cc79

File tree

7 files changed

+194
-38
lines changed

7 files changed

+194
-38
lines changed

solution/3300-3399/3307.Find the K-th Character in String Game II/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,61 @@ impl Solution {
251251
}
252252
```
253253

254+
#### C#
255+
256+
```cs
257+
public class Solution {
258+
public char KthCharacter(long k, int[] operations) {
259+
long n = 1;
260+
int i = 0;
261+
while (n < k) {
262+
n *= 2;
263+
++i;
264+
}
265+
int d = 0;
266+
while (n > 1) {
267+
if (k > n / 2) {
268+
k -= n / 2;
269+
d += operations[i - 1];
270+
}
271+
n /= 2;
272+
--i;
273+
}
274+
return (char)('a' + (d % 26));
275+
}
276+
}
277+
```
278+
279+
#### PHP
280+
281+
```php
282+
class Solution {
283+
/**
284+
* @param Integer $k
285+
* @param Integer[] $operations
286+
* @return String
287+
*/
288+
function kthCharacter($k, $operations) {
289+
$n = 1;
290+
$i = 0;
291+
while ($n < $k) {
292+
$n *= 2;
293+
++$i;
294+
}
295+
$d = 0;
296+
while ($n > 1) {
297+
if ($k > $n / 2) {
298+
$k -= $n / 2;
299+
$d += $operations[$i - 1];
300+
}
301+
$n /= 2;
302+
--$i;
303+
}
304+
return chr(ord('a') + ($d % 26));
305+
}
306+
}
307+
```
308+
254309
<!-- tabs:end -->
255310

256311
<!-- solution:end -->

solution/3300-3399/3307.Find the K-th Character in String Game II/README_EN.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,61 @@ impl Solution {
248248
}
249249
```
250250

251+
#### C#
252+
253+
```cs
254+
public class Solution {
255+
public char KthCharacter(long k, int[] operations) {
256+
long n = 1;
257+
int i = 0;
258+
while (n < k) {
259+
n *= 2;
260+
++i;
261+
}
262+
int d = 0;
263+
while (n > 1) {
264+
if (k > n / 2) {
265+
k -= n / 2;
266+
d += operations[i - 1];
267+
}
268+
n /= 2;
269+
--i;
270+
}
271+
return (char)('a' + (d % 26));
272+
}
273+
}
274+
```
275+
276+
#### PHP
277+
278+
```php
279+
class Solution {
280+
/**
281+
* @param Integer $k
282+
* @param Integer[] $operations
283+
* @return String
284+
*/
285+
function kthCharacter($k, $operations) {
286+
$n = 1;
287+
$i = 0;
288+
while ($n < $k) {
289+
$n *= 2;
290+
++$i;
291+
}
292+
$d = 0;
293+
while ($n > 1) {
294+
if ($k > $n / 2) {
295+
$k -= $n / 2;
296+
$d += $operations[$i - 1];
297+
}
298+
$n /= 2;
299+
--$i;
300+
}
301+
return chr(ord('a') + ($d % 26));
302+
}
303+
}
304+
```
305+
251306
<!-- tabs:end -->
252307

253308
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public char KthCharacter(long k, int[] operations) {
3+
long n = 1;
4+
int i = 0;
5+
while (n < k) {
6+
n *= 2;
7+
++i;
8+
}
9+
int d = 0;
10+
while (n > 1) {
11+
if (k > n / 2) {
12+
k -= n / 2;
13+
d += operations[i - 1];
14+
}
15+
n /= 2;
16+
--i;
17+
}
18+
return (char)('a' + (d % 26));
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
/**
3+
* @param Integer $k
4+
* @param Integer[] $operations
5+
* @return String
6+
*/
7+
function kthCharacter($k, $operations) {
8+
$n = 1;
9+
$i = 0;
10+
while ($n < $k) {
11+
$n *= 2;
12+
++$i;
13+
}
14+
$d = 0;
15+
while ($n > 1) {
16+
if ($k > $n / 2) {
17+
$k -= $n / 2;
18+
$d += $operations[$i - 1];
19+
}
20+
$n /= 2;
21+
--$i;
22+
}
23+
return chr(ord('a') + ($d % 26));
24+
}
25+
}

solution/3600-3699/3601.Find Drivers with Improved Fuel Efficiency/README.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ tags:
88

99
<!-- problem:start -->
1010

11-
# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency)
11+
# [3601. 寻找燃油效率提升的驾驶员](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency)
1212

1313
[English Version](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md)
1414

1515
## 题目描述
1616

1717
<!-- description:start -->
1818

19-
<p>Table: <code>drivers</code></p>
19+
<p>表:<code>drivers</code></p>
2020

2121
<pre>
2222
+-------------+---------+
@@ -25,11 +25,11 @@ tags:
2525
| driver_id | int |
2626
| driver_name | varchar |
2727
+-------------+---------+
28-
driver_id is the unique identifier for this table.
29-
Each row contains information about a driver.
28+
driver_id 是这张表的唯一主键。
29+
每一行都包含一个司机的信息。
3030
</pre>
3131

32-
<p>Table: <code>trips</code></p>
32+
<p>表:<code>trips</code></p>
3333

3434
<pre>
3535
+---------------+---------+
@@ -41,31 +41,32 @@ Each row contains information about a driver.
4141
| distance_km | decimal |
4242
| fuel_consumed | decimal |
4343
+---------------+---------+
44-
trip_id is the unique identifier for this table.
45-
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
44+
trip_id 是这张表的唯一主键。
45+
每一行表示一名司机完成的一次行程,包括该次行程行驶的距离和消耗的燃油量。
4646
</pre>
4747

48-
<p>Write a solution to find drivers whose <strong>fuel efficiency has improved</strong> by <strong>comparing</strong> their average fuel efficiency in the<strong> first half</strong> of the year with the <strong>second half</strong> of the year.</p>
48+
<p>编写一个解决方案,通过 <strong>比较</strong> 司机在 <strong>上半年</strong> <strong>下半年</strong> 的 <strong>平均燃油效率</strong> 来找出 <strong>燃油效率有所提高</strong> 的司机。</p>
4949

5050
<ul>
51-
<li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li>
52-
<li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li>
53-
<li>Only include drivers who have trips in <strong>both halves</strong> of the year</li>
54-
<li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li>
55-
<li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li>
51+
<li>通过&nbsp;<code>distance_km / fuel_consumed</code>&nbsp;计算 <strong>每次</strong>&nbsp;行程的 <strong>燃油效率</strong>。</li>
52+
<li><strong>上半年:</strong>一月到六月,<strong>下半年:</strong>七月到十二月</li>
53+
<li>只包含在上半年和下半年都有行程的司机</li>
54+
<li>通过(<code>second_half_avg - first_half_avg</code>)计算 <strong>提升效率</strong>。</li>
55+
<li>将所有结果 <strong>四舍五入</strong> 到小数点后 <code>2</code>&nbsp;位</li>
5656
</ul>
5757

58-
<p>Return <em>the result table ordered by efficiency improvement in <strong>descending</strong> order, then by driver name in <strong>ascending</strong> order</em>.</p>
58+
<p>返回结果表按提升效率&nbsp;<strong>降序</strong> 排列,然后按司机姓名 <strong>升序</strong> 排列。</p>
5959

60-
<p>The result format is in the following example.</p>
60+
<p>结果格式如下所示。</p>
6161

6262
<p>&nbsp;</p>
63-
<p><strong class="example">Example:</strong></p>
63+
64+
<p><strong class="example">示例:</strong></p>
6465

6566
<div class="example-block">
66-
<p><strong>Input:</strong></p>
67+
<p><strong>输入:</strong></p>
6768

68-
<p>drivers table:</p>
69+
<p>drivers 表:</p>
6970

7071
<pre class="example-io">
7172
+-----------+---------------+
@@ -79,7 +80,7 @@ Each row represents a trip made by a driver, including the distance traveled and
7980
+-----------+---------------+
8081
</pre>
8182

82-
<p>trips table:</p>
83+
<p>trips 表:</p>
8384

8485
<pre class="example-io">
8586
+---------+-----------+------------+-------------+---------------+
@@ -100,7 +101,7 @@ Each row represents a trip made by a driver, including the distance traveled and
100101
+---------+-----------+------------+-------------+---------------+
101102
</pre>
102103

103-
<p><strong>Output:</strong></p>
104+
<p><strong>输出:</strong></p>
104105

105106
<pre class="example-io">
106107
+-----------+---------------+------------------+-------------------+------------------------+
@@ -111,39 +112,39 @@ Each row represents a trip made by a driver, including the distance traveled and
111112
+-----------+---------------+------------------+-------------------+------------------------+
112113
</pre>
113114

114-
<p><strong>Explanation:</strong></p>
115+
<p><strong>解释:</strong></p>
115116

116117
<ul>
117118
<li><strong>Alice Johnson (driver_id = 1):</strong>
118119

119120
<ul>
120-
<li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
121-
<li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li>
122-
<li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
123-
<li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li>
124-
<li>Efficiency improvement: 14.02 - 11.97 = 2.05</li>
121+
<li>上半年行程(一月到六月):Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
122+
<li>上半年平均效率:(11.81 + 12.12) / 2 = 11.97</li>
123+
<li>下半年行程(七月到十二月):Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
124+
<li>下半年平均效率:(13.64 + 14.40) / 2 = 14.02</li>
125+
<li>效率提升:14.02 - 11.97 = 2.05</li>
125126
</ul>
126127
</li>
127128
<li><strong>Bob Smith (driver_id = 2):</strong>
128129
<ul>
129-
<li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
130-
<li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li>
131-
<li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li>
132-
<li>Second half average efficiency: 13.33</li>
133-
<li>Efficiency improvement: 13.33 - 11.24 = 2.09</li>
130+
<li>上半年行程:Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
131+
<li>上半年平均效率:(11.11 + 11.36) / 2 = 11.24</li>
132+
<li>下半年行程:Oct 5 (200.0/15.0 = 13.33)</li>
133+
<li>下半年平均效率:13.33</li>
134+
<li>效率提升:13.33 - 11.24 = 2.09</li>
134135
</ul>
135136
</li>
136-
<li><strong>Drivers not included:</strong>
137+
<li><strong>未包含的司机:</strong>
137138
<ul>
138-
<li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li>
139-
<li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li>
140-
<li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li>
139+
<li>Carol Davis (driver_id = 3):只有上半年的行程(三月,五月)</li>
140+
<li>David Wilson (driver_id = 4):只有下半年的行程(七月,十一月)</li>
141+
<li>Emma Brown (driver_id = 5):只有上半年的行程(二月)</li>
141142
</ul>
142143
</li>
143144

144145
</ul>
145146

146-
<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p>
147+
<p>输出表按提升效率降序排列,然后按司机名字升序排列。</p>
147148
</div>
148149

149150
<!-- description:end -->

solution/DATABASE_README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320
| 3570 | [查找无可用副本的书籍](/solution/3500-3599/3570.Find%20Books%20with%20No%20Available%20Copies/README.md) | `数据库` | 简单 | |
321321
| 3580 | [寻找持续进步的员工](/solution/3500-3599/3580.Find%20Consistently%20Improving%20Employees/README.md) | `数据库` | 中等 | |
322322
| 3586 | [寻找 COVID 康复患者](/solution/3500-3599/3586.Find%20COVID%20Recovery%20Patients/README.md) | `数据库` | 中等 | |
323-
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
323+
| 3601 | [寻找燃油效率提升的驾驶员](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
324324

325325
## 版权
326326

solution/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3611,7 +3611,7 @@
36113611
| 3598 | [相邻字符串之间的最长公共前缀](/solution/3500-3599/3598.Longest%20Common%20Prefix%20Between%20Adjacent%20Strings%20After%20Removals/README.md) | `数组`,`字符串` | 中等 | 第 456 场周赛 |
36123612
| 3599 | [划分数组得到最小 XOR](/solution/3500-3599/3599.Partition%20Array%20to%20Minimize%20XOR/README.md) | `位运算`,`数组`,`动态规划`,`前缀和` | 中等 | 第 456 场周赛 |
36133613
| 3600 | [升级后最大生成树稳定性](/solution/3600-3699/3600.Maximize%20Spanning%20Tree%20Stability%20with%20Upgrades/README.md) | `贪心`,`并查集`,`图`,`二分查找`,`最小生成树` | 困难 | 第 456 场周赛 |
3614-
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
3614+
| 3601 | [寻找燃油效率提升的驾驶员](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
36153615

36163616
## 版权
36173617

0 commit comments

Comments
 (0)