From 3c6d7e5966eccb5bec63c40a59ee56256194e039 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 4 Jul 2025 06:24:33 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3307 No.3307.Find the K-th Character in String Game II --- .../README.md | 55 ++++++++++++++ .../README_EN.md | 55 ++++++++++++++ .../Solution.cs | 20 +++++ .../Solution.php | 25 +++++++ .../README.md | 73 ++++++++++--------- solution/DATABASE_README.md | 2 +- solution/README.md | 2 +- 7 files changed, 194 insertions(+), 38 deletions(-) create mode 100644 solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.cs create mode 100644 solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.php diff --git a/solution/3300-3399/3307.Find the K-th Character in String Game II/README.md b/solution/3300-3399/3307.Find the K-th Character in String Game II/README.md index 5065706f87f45..b2a5316dfdbeb 100644 --- a/solution/3300-3399/3307.Find the K-th Character in String Game II/README.md +++ b/solution/3300-3399/3307.Find the K-th Character in String Game II/README.md @@ -251,6 +251,61 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public char KthCharacter(long k, int[] operations) { + long n = 1; + int i = 0; + while (n < k) { + n *= 2; + ++i; + } + int d = 0; + while (n > 1) { + if (k > n / 2) { + k -= n / 2; + d += operations[i - 1]; + } + n /= 2; + --i; + } + return (char)('a' + (d % 26)); + } +} +``` + +#### PHP + +```php +class Solution { + /** + * @param Integer $k + * @param Integer[] $operations + * @return String + */ + function kthCharacter($k, $operations) { + $n = 1; + $i = 0; + while ($n < $k) { + $n *= 2; + ++$i; + } + $d = 0; + while ($n > 1) { + if ($k > $n / 2) { + $k -= $n / 2; + $d += $operations[$i - 1]; + } + $n /= 2; + --$i; + } + return chr(ord('a') + ($d % 26)); + } +} +``` + diff --git a/solution/3300-3399/3307.Find the K-th Character in String Game II/README_EN.md b/solution/3300-3399/3307.Find the K-th Character in String Game II/README_EN.md index 3ead9e974af75..f8ad4b7e28c7b 100644 --- a/solution/3300-3399/3307.Find the K-th Character in String Game II/README_EN.md +++ b/solution/3300-3399/3307.Find the K-th Character in String Game II/README_EN.md @@ -248,6 +248,61 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public char KthCharacter(long k, int[] operations) { + long n = 1; + int i = 0; + while (n < k) { + n *= 2; + ++i; + } + int d = 0; + while (n > 1) { + if (k > n / 2) { + k -= n / 2; + d += operations[i - 1]; + } + n /= 2; + --i; + } + return (char)('a' + (d % 26)); + } +} +``` + +#### PHP + +```php +class Solution { + /** + * @param Integer $k + * @param Integer[] $operations + * @return String + */ + function kthCharacter($k, $operations) { + $n = 1; + $i = 0; + while ($n < $k) { + $n *= 2; + ++$i; + } + $d = 0; + while ($n > 1) { + if ($k > $n / 2) { + $k -= $n / 2; + $d += $operations[$i - 1]; + } + $n /= 2; + --$i; + } + return chr(ord('a') + ($d % 26)); + } +} +``` + diff --git a/solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.cs b/solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.cs new file mode 100644 index 0000000000000..e5b4528c3de93 --- /dev/null +++ b/solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.cs @@ -0,0 +1,20 @@ +public class Solution { + public char KthCharacter(long k, int[] operations) { + long n = 1; + int i = 0; + while (n < k) { + n *= 2; + ++i; + } + int d = 0; + while (n > 1) { + if (k > n / 2) { + k -= n / 2; + d += operations[i - 1]; + } + n /= 2; + --i; + } + return (char)('a' + (d % 26)); + } +} diff --git a/solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.php b/solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.php new file mode 100644 index 0000000000000..7c56a34a4d892 --- /dev/null +++ b/solution/3300-3399/3307.Find the K-th Character in String Game II/Solution.php @@ -0,0 +1,25 @@ +class Solution { + /** + * @param Integer $k + * @param Integer[] $operations + * @return String + */ + function kthCharacter($k, $operations) { + $n = 1; + $i = 0; + while ($n < $k) { + $n *= 2; + ++$i; + } + $d = 0; + while ($n > 1) { + if ($k > $n / 2) { + $k -= $n / 2; + $d += $operations[$i - 1]; + } + $n /= 2; + --$i; + } + return chr(ord('a') + ($d % 26)); + } +} \ No newline at end of file diff --git a/solution/3600-3699/3601.Find Drivers with Improved Fuel Efficiency/README.md b/solution/3600-3699/3601.Find Drivers with Improved Fuel Efficiency/README.md index 77f86b85e4aa2..6ff91ebff701d 100644 --- a/solution/3600-3699/3601.Find Drivers with Improved Fuel Efficiency/README.md +++ b/solution/3600-3699/3601.Find Drivers with Improved Fuel Efficiency/README.md @@ -8,7 +8,7 @@ tags: -# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency) +# [3601. 寻找燃油效率提升的驾驶员](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency) [English Version](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md) @@ -16,7 +16,7 @@ tags: -

Table: drivers

+

表:drivers

 +-------------+---------+
@@ -25,11 +25,11 @@ tags:
 | driver_id   | int     |
 | driver_name | varchar |
 +-------------+---------+
-driver_id is the unique identifier for this table.
-Each row contains information about a driver.
+driver_id 是这张表的唯一主键。
+每一行都包含一个司机的信息。
 
-

Table: trips

+

表:trips

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

Write a solution to find drivers whose fuel efficiency has improved by comparing their average fuel efficiency in the first half of the year with the second half of the year.

+

编写一个解决方案,通过 比较 司机在 上半年下半年平均燃油效率 来找出 燃油效率有所提高 的司机。

-

Return the result table ordered by efficiency improvement in descending order, then by driver name in ascending order.

+

返回结果表按提升效率 降序 排列,然后按司机姓名 升序 排列。

-

The result format is in the following example.

+

结果格式如下所示。

 

-

Example:

+ +

示例:

-

Input:

+

输入:

-

drivers table:

+

drivers 表:

 +-----------+---------------+
@@ -79,7 +80,7 @@ Each row represents a trip made by a driver, including the distance traveled and
 +-----------+---------------+
 
-

trips table:

+

trips 表:

 +---------+-----------+------------+-------------+---------------+
@@ -100,7 +101,7 @@ Each row represents a trip made by a driver, including the distance traveled and
 +---------+-----------+------------+-------------+---------------+
 
-

Output:

+

输出:

 +-----------+---------------+------------------+-------------------+------------------------+
@@ -111,39 +112,39 @@ Each row represents a trip made by a driver, including the distance traveled and
 +-----------+---------------+------------------+-------------------+------------------------+
 
-

Explanation:

+

解释:

-

The output table is ordered by efficiency improvement in descending order then by name in ascending order.

+

输出表按提升效率降序排列,然后按司机名字升序排列。

diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 86044f6de4227..2e5a1023c9be4 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -320,7 +320,7 @@ | 3570 | [查找无可用副本的书籍](/solution/3500-3599/3570.Find%20Books%20with%20No%20Available%20Copies/README.md) | `数据库` | 简单 | | | 3580 | [寻找持续进步的员工](/solution/3500-3599/3580.Find%20Consistently%20Improving%20Employees/README.md) | `数据库` | 中等 | | | 3586 | [寻找 COVID 康复患者](/solution/3500-3599/3586.Find%20COVID%20Recovery%20Patients/README.md) | `数据库` | 中等 | | -| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | | +| 3601 | [寻找燃油效率提升的驾驶员](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | | ## 版权 diff --git a/solution/README.md b/solution/README.md index ef7a292d3f0f9..c117f02609a23 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3611,7 +3611,7 @@ | 3598 | [相邻字符串之间的最长公共前缀](/solution/3500-3599/3598.Longest%20Common%20Prefix%20Between%20Adjacent%20Strings%20After%20Removals/README.md) | `数组`,`字符串` | 中等 | 第 456 场周赛 | | 3599 | [划分数组得到最小 XOR](/solution/3500-3599/3599.Partition%20Array%20to%20Minimize%20XOR/README.md) | `位运算`,`数组`,`动态规划`,`前缀和` | 中等 | 第 456 场周赛 | | 3600 | [升级后最大生成树稳定性](/solution/3600-3699/3600.Maximize%20Spanning%20Tree%20Stability%20with%20Upgrades/README.md) | `贪心`,`并查集`,`图`,`二分查找`,`最小生成树` | 困难 | 第 456 场周赛 | -| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | | +| 3601 | [寻找燃油效率提升的驾驶员](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | | ## 版权