557. 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.
Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.






    public String reverseWords(String s) {
        String[] str = s.split(" ");
        for (int i = 0; i < str.length; i++) str[i] = new StringBuilder(str[i]).reverse().toString();
        StringBuilder result = new StringBuilder();
        for (String st : str) result.append(st + " ");
        return result.toString().trim();
    } 



 Without using pre-defined split and reverse function [Accepted]

public class Solution {
    public String reverseWords(String s) {
        String words[] = split(s);
        StringBuilder res=new StringBuilder();
        for (String word: words)
            res.append(reverse(word) + " ");
        return res.toString().trim();
    }
    public String[] split(String s) {
        ArrayList < String > words = new ArrayList < > ();
        StringBuilder word = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                words.add(word.toString());
                word = new StringBuilder();
            } else
                word.append( s.charAt(i));
        }
        words.add(word.toString());
        return words.toArray(new String[words.size()]);
    }
    public String reverse(String s) {
      StringBuilder res=new StringBuilder();
        for (int i = 0; i < s.length(); i++)
            res.insert(0,s.charAt(i));
        return res.toString();
    }
}




// String index out of range: 27
// bug , 
本身用ide 跳跳, 而後 得出答案來再看看別人的答案
總結下,別人作得好的地方,

class Solution {
    public String reverseWords(String s) {
        // reverse every word 
        // append it to a sb
        StringBuilder sb = new StringBuilder();
        // need to find i and j , the two ends of a word 
        // get the reversed , and append it to the sb
        // keep going to the next word for a new pair of i and j , 
        // do the same as above 
        int i = 0;
        int j = 0;
        while(j < s.length()){
            while(s.charAt(j) == ' ') j++; // not " " , this is a string 
            i = j;
            while(s.charAt(j + 1) != ' ' || j + 1 < s.length()){
                j++;
            }
            // j+ 1 is out of bound or j + 1 is an empty space now 
            sb.append(reverse(i, j, s));
            sb.append(" ");
            // after append it , move on to the new word if j is not hitting the end already
            j++;  
            
        }
        //sb.setLength(sb.length() - 1);
        return sb.toString();
    }
    
    private String reverse(int i, int j, String s){
        char[] substr = s.substring(i, j + 1).toCharArray();
        while(i < j){
            
            char tmp = s.charAt(i);
            substr[i] = substr[j];
            substr[j] = tmp;  
            i++;
            j++;
        }
        return new String(substr);
    // how to convert a charArray to a string ? 
        // Here we are using Arrays.toString(char[] ca) method to print the char array in readable format.

// char[] charArrays = new char[]{'1', '2', '3', 'A', 'B', 'C'};

        // String newString1 = new String(charArrays);
        
    }
}
相關文章
相關標籤/搜索