博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Reverse Words in a String III
阅读量:5237 次
发布时间:2019-06-14

本文共 1189 字,大约阅读时间需要 3 分钟。

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.

1 class Solution { 2     public String reverseWords(String s) { 3         if(s == null || s.length() == 0) 4             return s; 5         String []words = s.split(" "); 6         StringBuilder result = new StringBuilder(); 7         for(String word : words){ 8             String newWord = reverseWord(word); 9             result.append(newWord);10             result.append(" ");11         }12         return result.toString().substring(0, result.toString().length() - 1);13     }14     private String reverseWord(String word){15         char []arr = word.toCharArray();16         for(int i = 0, j = arr.length - 1; i <= j; i++, j--){17             char temp = arr[i];18             arr[i] = arr[j];19             arr[j] = temp;20         }21         return new String(arr);22     }23 }

 

转载于:https://www.cnblogs.com/luckygxf/p/7747318.html

你可能感兴趣的文章
128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
查看>>
定制jackson的自定义序列化(null值的处理)
查看>>
auth模块
查看>>
javascript keycode大全
查看>>
前台freemark获取后台的值
查看>>
log4j.properties的作用
查看>>
游戏偶感
查看>>
Leetcode: Unique Binary Search Trees II
查看>>
C++ FFLIB 之FFDB: 使用 Mysql&Sqlite 实现CRUD
查看>>
Spring-hibernate整合
查看>>
c++ map
查看>>
exit和return的区别
查看>>
discuz 常用脚本格式化数据
查看>>
洛谷P2777
查看>>
PHPStorm2017设置字体与设置浏览器访问
查看>>
SQL查询总结 - wanglei
查看>>
安装cocoa pods时出现Operation not permitted - /usr/bin/xcodeproj的问题
查看>>
GIT笔记:将项目发布到码云
查看>>
JavaScript:学习笔记(7)——VAR、LET、CONST三种变量声明的区别
查看>>
JavaScript 鸭子模型
查看>>