53-反转字符串
来源:互联网 发布:搭建网站软件 编辑:程序博客网 时间:2024/05/12 16:55
4.20
原来这两段如果写反了的话,差别还是很大的。while(i < length && s.charAt(i)==' '){ i++; } while(i < length && s.charAt(i)!=' '){ tmp = tmp + Character.toString(s.charAt(i)); i++; }
正确代码:
public class Solution { /** * @param s : A string * @return : A string */ public String reverseWords(String s) { // write your code if(s == null){ return s; } int length = s.length(); String res = ""; for(int i = 0;i < length;i++){ String tmp = ""; while(i < length && s.charAt(i)==' '){ i++; } while(i < length && s.charAt(i)!=' '){ tmp = tmp + Character.toString(s.charAt(i)); i++; } if(tmp != ""){ if(res == ""){ res = tmp; } else{ res = tmp + " " + res; } } } return res; }}
0 0
- 53-反转字符串
- 字符串反转,单词反转
- 字符串反转
- 反转字符串
- 字符串反转
- 字符串反转
- 反转字符串
- 字符串反转
- 字符串反转
- 字符串反转
- 反转字符串
- 字符串反转
- 反转字符串
- 字符串反转
- 字符串反转
- 反转字符串
- 反转字符串
- 反转字符串
- JPA安装配置
- 分页(简版)
- OpenWRT开发准备工作
- camera api1 和api2
- JUnit custom runner with Spring application context
- 53-反转字符串
- aspx中的WebMethod方法如何调用Session
- Docker之四种网络模式
- JPA实体管理器
- ios::sync_with_stdio(false)-关闭同步输出流
- 显示InputDialog输入对话框实现对用户输入的英文单词进行简单处理
- 14.元表
- 线程同步和线程死锁
- 实现文件的复制