Easy:字符串按单词反转
来源:互联网 发布:gamemaker 不需要编程 编辑:程序博客网 时间:2024/06/09 21:10
字符串按单词反转
输入格式是:”this is a string”
输出格式是: “string a is this”
要求不能用任何库函数方法:首先将整个句子按字符翻转,然后再将其中每个单词的字符旋转。
时间复杂度:O(n),空间复杂度:O(1);
public class Main { public static void main(String[] args){ String str="I am a student"; char[] chars=str.toCharArray(); int left=0; int right=chars.length-1; swapDic(chars,left,right); left=0; right=0; for(int i=0;i<chars.length;i++){ if(chars[i]==' '){ swapDic(chars,left,right-1); left=right+1; }else if(i==chars.length-1){ swapDic(chars,left,right); } right++; } System.out.println(chars); } /*翻转字符串*/ public static void swapDic(char[] chars,int left,int right){ while(left<right){ swap(chars,left,right); left++; right--; } } /*字母对调*/ public static void swap(char[] chars,int n,int m){ if(n==m){ return; } char temp=chars[n]; chars[n]=chars[m]; chars[m]=temp; }}
输入:I am a student
输出:student a am I
0 0
- Easy:字符串按单词反转
- 按单词反转字符串
- 按单词反转字符串
- 按单词反转字符串
- 按单词反转字符串
- 按单词反转字符串
- 按单词反转字符串
- 字符串反转,单词反转
- LeetCode按单词反转字符串
- 笔试题-反转字符串和按单词反转字符串
- 面试问题之 按单词反转字符串
- 面试问题之 按单词反转字符串
- 简单面试题::字符串按单词反转
- Python字符串处理:按单词反转
- 字符串单词顺序反转
- 反转字符串中的单词
- 字符串中单词反转
- 字符串单词反转
- Linux IO实时监控iostat命令详解
- MongoDB基本操作
- 常见排序算法代码整理
- BigDecimal设置小数点位数
- libsvm中grid.py参数自动寻优
- Easy:字符串按单词反转
- 对与RestFul的一些理解
- Qt-图像绘制控制之QMatrix类
- C运行时库CRT(C Run-time Library)详解
- 数据库的基础知识学习笔记(用的是wampserver中的mysql数据库)
- ajax的学习笔记
- python for 循环
- linux系统开机自启过程
- 在debian系统下安装R以及Rstudio的经历