zoukankan      html  css  js  c++  java
  • [LeetCode] 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.

    题意,翻转每个字符串中的单词

    道理还是翻转字符串就是换了个形式罢了,按空格拆就好

    class Solution {
        private String reverseString(String s) {
            String str = "";
            for (int i = s.length() - 1; i >= 0; i --)
                str += s.charAt(i);
            return str;
        }
        public String reverseWords(String s) {
            String str = "";
            String temp = "";
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == ' ') {
                    str += reverseString(temp);
                    str += " ";
                    temp = "";
                }
                else temp += s.charAt(i);
            }
            str += reverseString(temp);
            return str;
        }
    }
  • 相关阅读:
    HDU 2276
    HDU 2254
    HDU 1536 & 1944
    HDU 1538
    HDU 2177
    HDU 2176
    HDU 1209
    HDU 1254
    c++ 11 default delete
    ssh免密登录
  • 原文地址:https://www.cnblogs.com/Moriarty-cx/p/9763587.html
Copyright © 2011-2022 走看看