当前位置:网站首页>LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
2022-08-10 22:34:00 【The man fishing alone like snow.】
题目如下:
解题思路:运用两个StringBuilder,One to carry the reversed string One is to record reverse words
Use characters to determinecwhen including spaces Then add the data of the reversed word to the first carrying the reversed string and add spaces,然后newA new function that adds reversed strings.最后return返回结果
class Solution {
public String reverseWords(String s) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
int len = s.length();//Reduce consumed memory
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c == ' ') {
sb1.append(sb2.reverse());
sb1.append(' ');
sb2 = new StringBuilder();
} else {
sb2.append(c);
}
}
sb1.append(sb2.reverse());
return sb1.toString();//返回结果
}
}
边栏推荐
- [SQL brush questions] Day3----Special exercises for common functions that SQL must know
- 学会开会|成为有连接感组织的重要技能
- shell编程之正则表达式与文本处理器
- shell (text printing tool awk)
- Labelme-5.0.1 version edit polygon crash
- The Thread State,
- file IO-buffer
- 为什么一般公司面试结束后会说「回去等消息」,而不是直接告诉面试者结果?
- 企业云存储日常运行维护实践经验分享
- FPGA - 7系列 FPGA内部结构之Memory Resources -03- 内置纠错功能
猜你喜欢
QT笔记——vs + qt 创建一个带界面的 dll 和 调用带界面的dll
What are the concepts, purposes, processes, and testing methods of interface testing?
An article to teach you a quick start and basic explanation of Pytest, be sure to read
这些不可不知的JVM知识,我都用思维导图整理好了
从斐波那契 - 谈及动态规划 - 优化
阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
RK3399平台开发系列讲解(内核驱动外设篇)6.35、IAM20680陀螺仪介绍
新一代网络安全防护体系的五个关键特征
C # Hex file transfer skills necessary article 】 【 bin file code implementation
解码2022中国网安强星丨正向建、反向查,华为构建数字化时代的网络安全防线
随机推荐
黑猫带你学Makefile第13篇:Makefile编译问题合集
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
LeetCode每日一题(1573. Number of Ways to Split a String)
过滤器
配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)
文件IO-缓冲区
BM13 determines whether a linked list is a palindrome
camera预览流程 --- 从HAL到OEM
Labelme-5.0.1 version edit polygon crash
Self-organization is a two-way journey between managers and members
《DevOps围炉夜话》- Pilot - CNCF开源DevOps项目DevStream简介 - feat. PMC成员胡涛
企业云存储日常运行维护实践经验分享
解码2022中国网安强星丨正向建、反向查,华为构建数字化时代的网络安全防线
华为HCIE云计算之Fusion Access桌面云
测试4年感觉和1、2年时没什么不同?这和应届生有什么区别?
shell (text printing tool awk)
美味石井饭菜
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
String类的常用方法
TCP连接过程中如果拔掉网线会发生什么?