Leetcode 448. Find All Numbers Disappeared in an Array JAVA语言151. Reverse Words in a String

Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.

题意:反转字符串中的单词,注意空格的处理!!!

public class Solution {
    public String reverseWords(String s) {
        int length=s.length();
        // char[] ch=s.toCharArray();
        if(s==null || length==0)return s;
        int end=length;
        StringBuilder ret=new StringBuilder();
        for(int i=length-1;i>=0;i--){
            if(s.charAt(i)==‘ ‘)end=i;
            else if(i==0 || s.charAt(i-1)==‘ ‘){
            //如果里面有单词,新加单词时先加空格
                if(ret.length()!=0)
                    ret.append(‘ ‘);
                ret.append(s.substring(i,end));
            }
        }
        return ret.toString();
    }
}

PS:群里大神思路。挺简洁的。从后往前遍历。找到单词就添加到ret,

时间: 2024-10-17 07:20:25

Leetcode 448. Find All Numbers Disappeared in an Array JAVA语言151. Reverse Words in a String的相关文章

Leetcode 448. Find All Numbers Disappeared in an Array

Leetcode  448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions Total Accepted: 31266 Total Submissions: 58997 Difficulty: Easy Contributors: yuhaowang001 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of

LeetCode 448 Find All Numbers Disappeared in an Array 解题报告

题目要求 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) run

leetcode之Find All Numbers Disappeared in an Array

问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到了这个题目.我有些吃惊,这个题我虽然知道两种解法,但本身还是有难度的,居然通过率这么高.然后就搜索相关网页,看到一个和它很接近的题目<Find All Duplicates in an Array>,然后就释然了.这两个题目有相同的题干,只是问题略微不同,解法有相似之处.估计是因为题号太接近了,会

448. Find All Numbers Disappeared in an Array

题目 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runti

448. Find All Numbers Disappeared in an Array Add to List

题目描述 题目分析 有个[1,n]的条件要充分利用起来. 题目代码 public class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { List<Integer> list=new ArrayList<Integer>(); if(nums==null || nums.length==0) return list; if(nums.length == 1 ){ list.ad

[Array]448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime?

[LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers

[LeetCode]151.Reverse Words in a String

题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification.

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a