leetcode
来源:互联网 发布:知乎怎么发表问题 编辑:程序博客网 时间:2024/06/07 23:20
旋转字符串:
Reverse Words in a String:
算法: 先是整体旋转,啊然后是旋转个体。 “the sky is blue”,
“blue is sky the”.
细节部分
1. 对于空格怎么处理,然后就是 像这样的字符串处理“1 ”;
用一个Count计数,当count >= 1 , 判断后面是否有汉子,有的 + ‘ ’;
否则直接退出
代码:
class Solution {public: void reverseWords(string &s) { int n = s.size(); if(n == 0) { s = ""; return; } int i=0,j=n-1; while(i < j) { char temp = s[i]; s[i] = s[j]; s[j] = temp; i++; j--; } i=0,j=0; int l=0,Count=0; //Count 计算后面是否需要加“ ”; while(i < n) { while(i < n && s[i] == ' ') { i++; } if(i == n) //判断后面是有单词 { break; } if(Count) { s[j] = ' '; //前面有的话, 就+‘ ’; j++; } l = j; while(i < n && s[i] != ' ') { s[j] = s[i]; i++; j++; } int d1 = l, d2 = j-1; while(d1 < d2) { char temp = s[d1]; s[d1] = s[d2]; s[d2] = temp; d1++; d2--; } Count++; } s.resize(j); }};
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- Codeforces 766D Mahmoud and a Dictionary(并查集+map)
- h5增强的页面元素
- 第三讲:进程线程模型
- easyui-calendar周数
- 欧几里得算法及其扩展以及运用
- leetcode
- Java8总结之函数式编程
- 被粉碎的线段树seg
- 部署服务器,安装vsftpd所遇问题总结
- (crm笔记1-4)Struts2层搭建
- mybatis 拦截器分页
- Jzoj3882 近邻
- Gym 100792H Hashing (DP)
- hibernate 入门(一)Exception:MappingException