1. 程式人生 > >345. Reverse Vowels of a String | 逆置字串中的母音字母

345. Reverse Vowels of a String | 逆置字串中的母音字母

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

Subscribe to see which companies asked this question.

public class Solution {
   public String reverseVowels(String s) {
		int i, j;
		String string = "aeiouAEIOU";
		i = 0;
		j = s.length() - 1;
		char t;
		StringBuilder sBuilder = new StringBuilder(s);
		while (j >= i) {
			if (string.indexOf(s.charAt(i)) != -1 && string.indexOf(s.charAt(j)) != -1) {
				t = sBuilder.charAt(i);
				sBuilder.setCharAt(i, sBuilder.charAt(j));
				sBuilder.setCharAt(j, t);
				i++;
				j--;
			} else if (string.indexOf(s.charAt(j)) == -1) {
				j--;
			} else if (string.indexOf(s.charAt(i)) == -1) {
				i++;
			} else {
				i++;
				j--;
			}
		}
		return sBuilder.toString();
	}
}


相關推薦

345. Reverse Vowels of a String | 字串母音字母

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". E

【LeetCode】345.Reverse Vowels of a String(反轉字串母音字母)-C++實現

本題為谷歌面試題。 問題描述: 一、第一種方法:對撞指標法 #include <iostream> #include <vector> #include <string> #include <cassert> #inc

345. Reverse Vowels of a String【easy】

not blog ive 參考 char wap ast otc emp 345. Reverse Vowels of a String【easy】 Write a function that takes a string as input and reverse o

345. Reverse Vowels of a String

轉字符串 ont pan == while arr toc problem val 原題鏈接:https://leetcode.com/problems/reverse-vowels-of-a-string/description/ 跟反轉字符串那道題目類似,直接雙指針方法

345 Reverse Vowels of a String 反轉字符串的元音字母

class ble otc -s else 字符串 反轉字符串 一個 -a 編寫一個函數,以字符串作為輸入,反轉該字符串中的元音字母。示例 1:給定 s = "hello", 返回 "holle".示例 2:給定 s = "leetcode", 返回 "leotcede".

345. Reverse Vowels of a String(python+cpp)

題目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle"

leetcode:(345) Reverse Vowels of a String(java)

題目:         Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hell

Leetcode 345. Reverse Vowels of a String

文章作者:Tyan 部落格:noahsnail.com  |  CSDN  |  簡書 1. Description 2. Solution Version 1 class Solution {

[LeetCode] 345. Reverse Vowels of a String

題目 Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle

345 reverse vowels of a string

關鍵:找一個容器儲存可換位置的字母 1. 自己 使用陣列儲存 class Solution {     public String reverseVowels(String s) {         int[] map= new int[256];         ma

leetcode 345 Reverse Vowels of a String(翻轉支付串母音字母

題目要求 寫一個函式,實現對於給定的字串中的母音字母進行翻轉。 注意輸入均為小寫,或者大寫字母。 演示示例 Example 1 Input: “hello” Output: “holle” Example 2 Input: “leetcode” Output: “l

LeetCode#345. Reverse Vowels of a String

題目:反轉字串中的母音字母 難度:Easy 思路:借鑑二分查詢的思想,分別從前後進行查詢,找到母音字母,就交換 程式碼: public class Solution { public

345. Reverse Vowels of a String(交換元音字母)(leetcode)

alt not in 分享 clu input hello png div hat Write a function that takes a string as input and reverse only the vowels of a string. Example

【LeetCode】345. 反轉字串母音字母Reverse Vowels of a String

【 英文練習 | 中文練習 】 題目描述: 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 示例: 輸入: "hello" 輸出: "holle" 解題思路: 雙指標典型題目,注意母音字母不要只考慮小寫的。 public String reverseVowe

C#LeetCode刷題之#345-反轉字串母音字母​​​​​​​(Reverse Vowels of a String

問題 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 輸入: "hello" 輸出: "holle" 輸入: "leetcode" 輸出: "leotcede" 說明:母音字母不包含字母"y"。 Write a function that tak

LeetCode 345 反轉字串母音字母Reverse Vowels of a String

用一個容器按序填入所有所有的母音,並用一個字元替換。再將容器逆序輸出填到對應的字元上即可實現。程式碼如下: class Solution { public: string reverseVowels(string s) { vector&

345.Reverse Vowel of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "lee

4.4.1 python 字串雙指標/雜湊演算法1—— Reverse Vowels of a String & Longest Substring Without Repeating Char

這一部分開始,我們應用雙指標及雜湊等常見的簡單的演算法,解決一些字串的難題。 345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the vow

leetcode (Reverse Vowels of a String

Title:Reverse Vowels of a String   345 Difficulty:Easy 原題leetcode地址:https://leetcode.com/problems/reverse-vowels-of-a-string/   1.

[LeetCode] Reverse Vowels of a String 翻轉字串母音字母

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s =