LeetCode之字符串处理题java

344. Reverse String

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

Subscribe to see which companies asked this question

public class Solution {
    public String reverseString(String s) {
        if(s==null)
            return "";
        char c[] = s.toCharArray();
        int len = s.length();
        int i=0;
        int j=len-1;
        while(i<j){
            char tmp = c[i];
            c[i] = c[j];
            c[j] = tmp;
            ++i;
            --j;
        }
        return new String(c);
    }
}

345. Reverse Vowels of a String 

Total Accepted: 4116 Total Submissions: 11368 Difficulty: Easy

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".

Subscribe to see which companies asked this question

采用快排的partition函数来对字符串进行翻转
public class Solution {
    public  String reverseVowels(String s) {
        if(s==null){
            return "";
        }
        char[] c = s.toCharArray();
        int left = 0;
        int right = c.length-1;
        while(left<right){
            while(left<right&&!isVowel(c[left])){
                ++left;
            }
            while(left<right&&!isVowel(c[right])){
                --right;
            }
            char tmp = c[left];
            c[left] = c[right];
            c[right] = tmp;
            ++left;
            --right;
        }
        return new String(c);
    }
  //检查一个字符是否是元音字符
public boolean isVowel(char c){ if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U') return true; else return false; } }

168. Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB 

解题思路:26进制操作
  当n=1时,(n-1)%26+'A'='A';
  当n=26时,(n-1)%26+'A'='Z';
  当n=27时,进1,进位carry = (n-1)/26=1-->‘A’;依次循环
public class Solution {
    public String convertToTitle(int n) {
        StringBuilder str = new StringBuilder();
        if (n<=0){
            return " ";
        }
        while(n!=0){
            str.append((char)((n-1)%26 + 'A'));
            n = (n-1)/26;
        }
        return str.reverse().toString();
    }
}

242. Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

 解题思路一:
简易的哈希表,将每个字符串使用哈希表统计每个字符串中的数据,接着,校验两个哈希表中的字母的数量是否一致,如果所有的都一致,则为true,否则为false。这样,时间复杂度为O(n),空间消耗为256*2,空间复杂度为O(n)
public boolean isAnagram(String s, String t) {
        if(s==null||t==null)
            return false;
        int count_s[] = count(s);
        int count_t[] = count(t);
        for(int i=0;i<count_t.length;i++){
            if(count_t[i]==count_s[i]){
                continue;
            }else{
                return false;
            }
        }
        return true;
    }
    
    public int[] count(String str){
        if(str==null&&str.length()==0){
            return null;
        }
        int[] count = new int[256];
        for(int i=0;i<str.length();i++){
            count[str.charAt(i)]++;
        }
        return count;
    }

 

389. Find the Difference

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.
 思路1:
  采用异或操作,相同的字符异或结果为0,因为t是s中的字符随机排列后在随机增加一个字符后的字符串,两个字符串通过异或,最后的结果就是唯一不同的字符;
思路2:
  采用+/-操作,在t字符串中各字符使用+,再- s中字符串中各字符,最后剩下的就是唯一不同的字符;
//采用异或的方法:
public
class Solution { public char findTheDifference(String s, String t) { char c = 0; for(int i=0;i<s.length();i++){ c ^= s.charAt(i); } for(int i=0;i<t.length();i++){ c ^= t.charAt(i); } return c; } }
//采用+/-的方式
public
char findTheDifference(String s, String t) { char res = t.charAt(t.length() - 1); for (int i = 0; i < s.length(); i++) { res += t.charAt(i); res -= s.charAt(i); } return res; }

 

383. Ransom Note


Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
the 
magazines,
 write 
a 
function 
that 
will 
return 
true 
if 
the 
ransom 
 note 
can 
be 
constructed 
from 
the 
magazines ; 
otherwise, 
it 
will 
return 
false. 



Each 
letter
 in
 the
 magazine 
string 
can
 only 
be
 used 
once
 in
 your 
ransom
 note.

Note:
You may assume that both strings contain only lowercase letters.

canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true

思路:统计字符的个数,ransomNote中的各个字符总数是否<=magazine中对应的字符总数;
public class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] count = new int[26];
        for(char c:magazine.toCharArray()){
            count[c-'a']++;
        }
        for(char c:ransomNote.toCharArray()){
            if(--count[c-'a']<0)
                return false;
        }
        return true;
    }
}

 

387. First Unique Character in a String

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

思路1:1)首先都是小写字母,则使用数组统计每个字符出现的次数;

    2)再次从头到尾遍历字符串,将字符出现次数为1的字符(首次出现)的下标返回;

public class Solution {
    public int firstUniqChar(String s) {
        if(s==null||s.length()==0)
            return -1;
        int[] count = new int[26];//统计次数
        for(int i=0;i<s.length();i++){
            count[s.charAt(i)-'a']++;
        }
        for(int i=0;i<s.length();i++){
            if(count[s.charAt(i)-'a']==1)
                return i;
        }
        return -1;
    }
}

 

 

posted @ 2016-04-26 13:29  sunny_2015  阅读(1589)  评论(0编辑  收藏  举报