首页 > 其他分享 >leetcode-557-easy

leetcode-557-easy

时间:2023-01-03 21:45:57浏览次数:40  
标签:arr String StringBuilder 557 easy Input sb leetcode

Reverse Words in a String III

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

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

Input: s = "God Ding"
Output: "doG gniD"
Constraints:

1 <= s.length <= 5 * 104
s contains printable ASCII characters.
s does not contain any leading or trailing spaces.
There is at least one word in s.
All the words in s are separated by a single space.

思路一:分割字符串,然后反转每一个单词

    public String reverseWords(String s) {

        String[] arr = s.split("\\s");

        StringBuilder sb = new StringBuilder();
        for (String s1 : arr) {
            sb.append(new StringBuilder(s1).reverse()).append(" ");
        }

        return sb.substring(0, sb.length() - 1);
    }

标签:arr,String,StringBuilder,557,easy,Input,sb,leetcode
From: https://www.cnblogs.com/iyiluo/p/17023449.html

相关文章

  • leetcode-627-easy
    IslandPerimeterYouaregivenrowxcolgridrepresentingamapwheregrid[i][j]=1representslandandgrid[i][j]=0representswater.Gridcellsareconn......
  • leetcode-121-easy
    BestTimetoBuyandSellStockYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitb......
  • leetcode-441-easy
    ArrangingCoinsYouhavencoinsandyouwanttobuildastaircasewiththesecoins.Thestaircaseconsistsofkrowswheretheithrowhasexactlyicoins.Th......
  • leetcode-459-easy
    RepeatedSubstringPatternGivenastrings,checkifitcanbeconstructedbytakingasubstringofitandappendingmultiplecopiesofthesubstringtogether......
  • leetcode-492-easy
    ConstructtheRectangleAwebdeveloperneedstoknowhowtodesignawebpage'ssize.So,givenaspecificrectangularwebpage’sarea,yourjobbynowisto......
  • leetcode-144-easy
    BinaryTreePreorderTraversalGiventherootofabinarytree,returnthepreordertraversalofitsnodes'values.Example1:Input:root=[1,null,2,3]Out......
  • [Leetcode Weekly Contest]326
    链接:LeetCode[Leetcode]2520.统计能整除数字的位数给你一个整数num,返回num中能整除num的数位的数目。如果满足nums%val==0,则认为整数val可以整除nums......
  • 【队列】LeetCode 232. 用栈实现队列
    题目链接232.用栈实现队列思路设置一个主栈mainStack和一个辅助栈assistantStack,在进行入队的时候,将mainStack中的元素全部放入assistantStack中,再将x入队,然......
  • [LeetCode] 1325. Delete Leaves With a Given Value 删除给定值的叶子结点
    Givenabinarytree root andaninteger target,deleteallthe leafnodes withvalue target.Notethatonceyoudeletealeafnodewithvalue target, ......
  • 【队列】LeetCode 225. 用队列实现栈
    题目链接225.用队列实现栈思路设置一个主队列mainQueue和一个辅助队列assistantQueue,在进行压栈的时候,将mainQueue中的元素全部放入assistantQueue中,再将x压......