package point;/**题目:反转单词* y: i am busy. --> i ma .ysub* @Author Gavin* @date 2022年01月10日 22:36*/public class solution_3 {/*** 解题思路:使用两个游标进行移动* 当快游标移动到空格或者字符串长度的时候就翻转游标之间的单词*/public static String solution(String s){if(s==null||s.length()==0)return s;char[] c=s.toCharArray();int start=0,end=0;while (start<c.length){while (end<c.length&&c[end]!=' ')++end;//注意,假如start,end指向的都为'',那么就会跳过这个for循环然后跳过这个空串for (int i = start,j=end-1; i < j; i++,--j) {char tmp=c[i];c[i]=c[j];c[j]=tmp;}start=end+1;//假如开头为' '的时候就会跳过,start=end+1会跳过这个空串end=start;}return new String(c);}public static void main(String[] args) {System.out.println(solution(" abc"));}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。