Write a function that takes a string as input and reverse only the vowels of a string.java
Example 1:數組
Input: "hello"
Output: "holle"app
Example 2:spa
Input: "leetcode"
Output: "leotcede"指針
Note: The vowels does not include the letter "y".code
這道題的意思是讓咱們交換首尾對應的元音,元音包括大小寫一共有10個(a,e,i,o,u,A,E,I,O,U),能夠寫個判斷是否爲元音的方法,遍歷字符串,當left和right都爲元音時交換,若是left是元音,則right--,若是right是元音,left++。orm
解法一:(最後顯示超出時間限制)排序
public class Solution {
public String reverseVowels(String s) {
if (s == null){
return null;
}
char[] chars = s.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left <= right){
//若是兩個恰好都是元音,交換
if (isVowel(chars[left]) && isVowel(chars[right])){
swap(chars, left, right);
}else if(isVowel(chars[left])){
//左邊是元音,停下,右邊移動
right--;
}else {
left++;
}
}
return chars.toString();
}
//交換元音
public void swap(char[] chars, int left, int right){
char tmp = chars[left];
chars[left] = chars[right];
chars[right] = tmp;
}
//判斷是否爲元音
public boolean isVowel(char c){
HashSet<Character> set = new HashSet<Character>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
return set.contains(c);
}
}
複製代碼
將數組交換換成新建數組進行填充以後問題獲得解決
class Solution {
private final static HashSet<Character> vowels = new HashSet<>(
Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
public String reverseVowels(String s) {
if (s == null) return null;
int i = 0, j = s.length() - 1;
char[] result = new char[s.length()];
while (i <= j) {
char ci = s.charAt(i);
char cj = s.charAt(j);
if (!vowels.contains(ci)) {
result[i++] = ci;
} else if (!vowels.contains(cj)) {
result[j--] = cj;
} else {
result[i++] = cj;
result[j--] = ci;
}
}
return new String(result);
}
}
複製代碼
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.ci
Example 1:element
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
題意是判斷字符串是否爲迴文字結構,而且能夠刪除一個字符,那麼就能夠用兩個指針start、end從頭和尾一塊兒開始判斷,遇到相同的就直接跳過,遇到不一樣時就刪除,關鍵在於刪除能夠刪start或者end對應的字符,就是說有兩種狀況,那麼兩種狀況都嘗試
class Solution {
public boolean validPalindrome(String s) {
//定義兩個指針
int start = 0;
int end = s.length() - 1;
for(start = 0, end = s.length() - 1; start <= end; start++, end--){
if(s.charAt(start) != s.charAt(end)){
//刪除左邊字符或者右邊字符
return valid(s,start + 1, end) || valid(s,start, end - 1);
}
}
return true;
}
private boolean valid(String s, int start, int end){
while(start <= end){
if(s.charAt(start++) != s.charAt(end--)){
return false;
}
}
return true;
}
}
複製代碼
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
題意是給定兩個數組nums1和nums2,兩個初始化參數m、n表示數組中初始化的元素個數,能夠假設nums1有足夠的空間。若是從nums1頭部開始歸併,會覆蓋掉後面未排序的元素,因此採用從後面歸併的方式。
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
//準備兩個指針
int index1 = m - 1;
int index2 = n - 1;
int mergeIndex = m + n - 1;
while(index1 >= 0 && index2 >= 0){
if(index1 < 0){
//若是nums1先歸併完
nums1[mergeIndex--] = nums2[index2--];
}else if(index2 < 0){
//若是nums2先歸併完
nums1[mergeIndex--] = nums1[index1--];
}else if(nums1[index1] > nums2[index2]){
nums1[mergeIndex--] = nums1[index1--];
}else{
nums1[mergeIndex--] = nums2[index2--];
}
}
}
}
複製代碼
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
題意大概是給你一個單鏈表,而後讓你判斷這個鏈表是否有環,首先能夠想到的第一種解法是準備一個set,遍歷到一個就存儲一個,而後檢查set裏面有無重複的。第二種方法是用快慢指針,快指針一次走一=兩步,慢指針一次走一步,若是慢指針走到最後都沒相遇,那麼就無環,不然有環
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null){
return false;
}
ListNode fast = head.next;
ListNode slow = head;
while(fast != null && slow != null && fast.next != null){
if(fast == slow){
return true;
}
fast = fast.next.next;
slow = slow.next;
}
return false;
}
}
複製代碼
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"
Example 2:
Input:
s = "abpcplea", d = ["a","b","c"]
Output:
"a"
Note:
題意:刪除 s 中的一些字符,使得它構成字符串列表d中的一個字符串,找出能構成的最長字符串。若是有多個相同長度的結果,返回字典序的最小字符串。
經過刪除字符串 s 中的一個字符能獲得字符串t是s的子序列,咱們能夠使用雙指針來判斷一個字符串是否爲另外一個字符串的子序列。
class Solution {
public String findLongestWord(String s, List<String> d) {
String result = "";
//遍歷給定字符串數組
for(String w : d){
int len1 = result.length();
int len2 = w.length();
//若是result的長度大於w,或者長度相等,可是result字典序比較小,不更新result
if(len1 > len2 || (len1 == len2 && result.compareTo(w) < 0)){
continue;
}
if(isSub(s, w)){
result = w;
}
}
return result;
}
private boolean isSub(String s, String w){
int i = 0; //s的指針
int j = 0; //w的指針
while (i < s.length() && j < target.length()) {
if(s.charAt(i) == w.charAt(j)){
j++;
}
i++;
}
return j == w.length();
}
複製代碼