Leetcode 917.仅仅反转字母(Reverse Only Letters) Leetcode 917.仅仅反转字母 1 题目描述(Leetcode题目链接) 给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。 提示: S.length <= 100 33 <= S[i].ASCIIcode <= 122 S 中不包含 \ or...
1、题目描述 2、题目描述 利用栈实现逆序。 3、代码 1stringreverseOnlyLetters(stringS) {2if(S.size() ==0|| S.size() ==1)3returnS;45stack<string>st;6for(string::iterator it = S.begin(); it != S.end(); it++) {7if( isalpha(*it) ){8stringsub = S.substr(it-S.begin(),1);...
Java实现 1classSolution {2publicString reverseOnlyLetters(String s) {3StringBuilder sb =newStringBuilder(s);4inti = 0;5intj = s.length() - 1;6while(i <j) {7if(!Character.isLetter(sb.charAt(i))) {8i++;9}elseif(!Character.isLetter(sb.charAt(j))) {10j--;11}else{12chartemp =...
Can you solve this real interview question? Reverse Only Letters - Given a string s, reverse the string according to the following rules: * All the characters that are not English letters remain in the same position. * All the English letters (lowerca
public class Solution { public string ReverseOnlyLetters(string s) { int n = s.Length; char[] arr = s.ToCharArray(); int left = 0, right = n - 1; while (true) { while (left < right && !char.IsLetter(s[left])) { // 判断左边是否扫描到字母 left++; } while (right > ...
The string consists of lower English letters only. Length of the given string and k will in the range [1, 10000] 题解: 是Reverse String的进阶题目. 每2k个char中前k个char swap. Time Complexity: O(n), n = s.length(). Space: O(n). ...
# 输入:s = "ab-cd" # 输出:"dc-ba" class Solution(object): def reverseOnlyLetters(self, s): """ :type s: str :rtype: str """ sa = list(s) # 注意转换为数组,s为常量 l, r = 0,len(s)-1 while l<r: if not sa[l].isalpha(): l+=1 elif not sa[r].isalpha(): r-...
The string consists of lower English letters only. Length of the given string and k will in the range [1, 10000] 2、代码实现 public class Solution { public String reverse(String s) { if (s == null || s.length() == 0) {
917 Reverse Only Letters Easy Go 922 Sort Array By Parity II Easy Go 925 Long Pressed Name Easy Go 933 Number of Recent Calls Easy 942 DI String Match Easy Go 977 Squares of a Sorted Array Easy Go 994 Rotting Oranges Medium Go 1002 Find Common Characters Easy Go 1004 Max Consecutive ...
0917 Reverse Only Letters 61.4% Easy 0918 Maximum Sum Circular Subarray Go 38.1% Medium 0919 Complete Binary Tree Inserter 64.9% Medium 0920 Number of Music Playlists Go 50.6% Hard 0921 Minimum Add to Make Parentheses Valid Go 76.4% Medium 0922 Sort Array By Parity II Go 70.7% Easy ...