LeetCode 2000. Reverse Prefix of Word【字符串】简单

本文介绍如何解决LeetCode中一个简单的字符串操作题目,通过C++代码实现找到字符ch首次出现的位置并反转相应子串,适用于编程竞赛或日常算法练习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing.

  • For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd".

Return the resulting string.

Example 1:

Input: word = "abcdefd", ch = "d"
Output: "dcbaefd"
Explanation: The first occurrence of "d" is at index 3. 
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd".

Example 2:

Input: word = "xyxzxe", ch = "z"
Output: "zxyxxe"
Explanation: The first and only occurrence of "z" is at index 3.
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe".

Example 3:

Input: word = "abcd", ch = "z"
Output: "abcd"
Explanation: "z" does not exist in word.
You should not do any reverse operation, the resulting string is "abcd".

Constraints:

  • 1 <= word.length <= 250
  • word consists of lowercase English letters.
  • ch is a lowercase English letter.

题意:给你一个下标从 0 开始的字符串 word 和一个字符 ch 。找出 ch 第一次出现的下标 i反转 word 中从下标 0 开始、直到下标 i 结束(含下标 i )的那段字符。如果 word 中不存在字符 ch ,则无需进行任何操作。

  • 例如,如果 word = "abcdefd"ch = "d" ,那么你应该 反转 从下标 0 开始、直到下标 3 结束(含下标 3 )。结果字符串将会是 "dcbaefd"

返回 结果字符串


解法 字符串

很简单的题目,竞赛中我的解法如下:

//C++ version
class Solution {
public:
    string reversePrefix(string word, char ch) {
        size_t pos = word.find_first_of(ch);
        if (pos == string::npos) return word;
        reverse(word.begin(), word.begin() + pos + 1);
        return word;
    }
};
//执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户
//内存消耗:6.1 MB, 在所有 C++ 提交中击败了50.00% 的用户
LeetCode一个非常受欢迎的在线编程平台,提供了大量的算法题目,涵盖了各种难度级别和题型。下面是一些常用的LeetCode算法: 1. 两数之和(Two Sum):给定一个整数数组和一个目标值,找出数组中和为目标值的两个数的索引。 2. 反转字符串Reverse String):反转给定字符串中的字符顺序。 3. 最长公共前缀(Longest Common Prefix):找出一组字符串中最长的公共前缀。 4. 合并两个有序链表(Merge Two Sorted Lists):将两个有序链表合并为一个新的有序链表。 5. 有效的括号(Valid Parentheses):判断给定的字符串中的括号是否有效。 6. 盛最多水的容器(Container With Most Water):给定一组非负整数,表示一组垂直线在x轴上的坐标,找出两条线与x轴组成的容器可以容纳的最大水量。 7. 三数之和(3Sum):给定一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c,使得a + b + c = 0。 8. 最长回文子串(Longest Palindromic Substring):找出给定字符串中的最长回文子串。 9. 二叉树的最大深度(Maximum Depth of Binary Tree):计算二叉树的最大深度,即从根节点到最远叶子节点的最长路径上的节点数。 10. 两个排序数组的中位数(Median of Two Sorted Arrays):给定两个大小为m和n的有序数组,找出这两个数组合并后的中位数。 以上只是LeetCode中的一小部分常用算法题目,LeetCode还有很多其他类型的题目,包括动态规划、回溯、贪心算法等等。如果你有具体的算法问题或者需要更多的题目推荐,请告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值