917. Reverse Only Letters

2023-12-21 16:18
文章标签 reverse letters 917

本文主要是介绍917. Reverse Only Letters,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

917. 仅仅反转字母

给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。

 

    示例 1:

    输入:"ab-cd"
    输出:"dc-ba"
    

    示例 2:

    输入:"a-bC-dEf-ghIj"
    输出:"j-Ih-gfE-dCba"
    

    示例 3:

    输入:"Test1ng-Leet=code-Q!"
    输出:"Qedo1ct-eeLg=ntse-T!"
    

     

    提示:

    1. S.length <= 100
    2. 33 <= S[i].ASCIIcode <= 122 
    3. S 中不包含 \ or "

    解法一

    //时间复杂度O(n), 空间复杂度O(1)
    class Solution {
    public:string reverseOnlyLetters(string S) {int n = S.size();int i = 0, j = n - 1;while(i < n && (S[i] < 'A' || S[i] > 'Z' && S[i] < 'a')) i++;while(j >= 0 && (S[j] < 'A' || S[j] > 'Z' && S[j] < 'a')) j--;while(i < j) {int temp = S[i];S[i] = S[j];S[j] = temp;i++, j--;while(i < n && (S[i] < 'A' || S[i] > 'Z' && S[i] < 'a')) i++;while(j >= 0 && (S[j] < 'A' || S[j] > 'Z' && S[j] < 'a')) j--;}return S;}
    };

    思路:

    1. 使用一对指针i,j,分别指向字符串的开头和结尾元素。
    2. 将i向右移动到首个字母元素;将j向左移动到首个字母元素上;
    3. 交换i和j所指的元素,重复进行第2步,直到i >= j。

    和反转字符串的思路一样。

    2019/08/05 21:58

    这篇关于917. Reverse Only Letters的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/520659

    相关文章

    [Vulnhub] Sleepy JDWP+Tomcat+Reverse+Reverse-enginnering

    信息收集 Server IP AddressPorts Opening192.168.8.100TCP:21,8009,9001 $ nmap -sV -sC 192.168.8.100 -p- --min-rate 1000 -Pn Starting Nmap 7.92 ( https://nmap.org ) at 2024-06-20 05:06 EDTNmap scan repor

    LeetCode第25题之Reverse Nodes in k-Group

    基本思想是对每k个节点采用头插法,时间复杂度为O(n)。 C++代码: #include <vector>#include <iostream>using namespace std;/*** Definition for singly-linked list. */struct ListNode {int val;ListNode *next;ListNode(int x) : val(

    Reverse-Proxy微软开源:高效构建HTTP反向代理的利器

    Reverse-Proxy: 简化你的网络架构,用微软的反向代理加速你的服务。- 精选真开源,释放新价值。 概览 微软的reverse-proxy项目是一个高性能的HTTP反向代理应用程序开发工具包。它提供了一种灵活的方式来构建能够处理大量并发连接的代理服务。这个工具包不仅能够转发请求,还能够提供负载均衡、SSL终端、缓存和其他高级功能,使其成为构建现代微服务架构和云原生应用的理想选择。

    About the reverse_iterator

    reverse_iterator: 反向迭代器 A copy of the original iterator (the base iterator) is kept internally and used to reflect the operations performed on the reverse_iterator: whenever the reverse_iterator is in

    reverse-android-实战喜马拉雅-ollvm

    资料 1. apk: com.ximalaya.ting.android.apk. 2020年8月 可以使用 2.  抓包分析 java层分析 so层分析 登录的算法so是在 liblogin_encrypt.so中。 32位的, 用 IDA打开,查看 静态的导出函数。 打开 一个 首先看到 IDA VIEW 是一个横向 比较多的分支, 可能就是被

    2.5 how do I iterate over a sequence in reverse order

    So: 序列(包含列表、元祖以及字符串)中好像就只有列表有a.reverse() 但都有reversed(a) 一、http://www.mianwww.com/html/2009/08/3615.html 如果是一个list, 最快的解决方案是: list.reverse() try: for x in list: “do something with x” finally: li

    JS 的split()\reverse()\join()方法

    1、split()方法用于把一个字符串分割成字符串数组 语法split接受两个参数, separator 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。 howmany 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数 指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。 <script type="tex

    Reverse Integer问题及解法

    问题描述: Reverse digits of an integer. 示例: Example1: x = 123, return 321 Example2: x = -123, return -321 话不多说,代码很清楚 class Solution {public:int reverse(int x) {long long rev = 0;while(x != 0){r

    Reverse Words in a String III问题及解法

    问题描述: Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. 示例: Input: "Let's take LeetCode contest"

    Reverse String II问题及解法

    问题描述: Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all