字符旋转问题-Rotate the string
2018-01-20 21:19
225 查看
Problem 4:
Given
a string and an offset, rotate string by offset. (rotate from left to right)
Example
Given
解析:第一次我按字符考虑,想要走for循环,果真Time Error,一个case都没跑过:
class Solution {
public:
/*
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
void rotateString(string &str, int offset) {
// write your code here
while(offset>0){
int x = str.length();
char m = str.at(x-1);
for(int i = x-1 ; i>0;i--){
str[i] = str[i-1];
}
str[0] = m;
}
}
};
然后我就去翻c++ string库的函数了... 再观察case规律发现,只是相对位置发生了变化,本质上是将原字符串根据offset拆分成前后两片,再调换位置的过程,所以写了下面的代码:
class Solution {
public:
/*
* @param str: An array of char
* @param
4000
offset: An integer
* @return: nothing
*/
void rotateString(string &str, int offset) {
// write your code here
int x = str.length();
string sub_1 = str.substr(x-offset,offset);
string sub_2 = str.substr(0,x-offset);
sub_1 += sub_2;
str = sub_1;
}
};结果跑到一半就跪了,看到case是这样的:
嗯...果然自己想得不周到,加上了
offset
= offset%x;进行处理,后来又在一个空字符串上坑了一把,最后AC的代码如下:
class Solution {
public:
/*
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
void rotateString(string &str, int offset) {
// write your code here
int x = str.length();
if(x > 0){
offset = offset%x;
string sub_1 = str.substr(x-offset,offset);
string sub_2 = str.substr(0,x-offset);
sub_1 += sub_2;
str = sub_1;
}
}
};感悟:多看函数库,把程序方方面面想到,尽量一把AC,摒弃for循环。
Given
a string and an offset, rotate string by offset. (rotate from left to right)
Example
Given
"abcdefg".
offset=0 => "abcdefg" offset=1 => "gabcdef" offset=2 => "fgabcde" offset=3 => "efgabcd"
解析:第一次我按字符考虑,想要走for循环,果真Time Error,一个case都没跑过:
class Solution {
public:
/*
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
void rotateString(string &str, int offset) {
// write your code here
while(offset>0){
int x = str.length();
char m = str.at(x-1);
for(int i = x-1 ; i>0;i--){
str[i] = str[i-1];
}
str[0] = m;
}
}
};
然后我就去翻c++ string库的函数了... 再观察case规律发现,只是相对位置发生了变化,本质上是将原字符串根据offset拆分成前后两片,再调换位置的过程,所以写了下面的代码:
class Solution {
public:
/*
* @param str: An array of char
* @param
4000
offset: An integer
* @return: nothing
*/
void rotateString(string &str, int offset) {
// write your code here
int x = str.length();
string sub_1 = str.substr(x-offset,offset);
string sub_2 = str.substr(0,x-offset);
sub_1 += sub_2;
str = sub_1;
}
};结果跑到一半就跪了,看到case是这样的:
嗯...果然自己想得不周到,加上了
offset
= offset%x;进行处理,后来又在一个空字符串上坑了一把,最后AC的代码如下:
class Solution {
public:
/*
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
void rotateString(string &str, int offset) {
// write your code here
int x = str.length();
if(x > 0){
offset = offset%x;
string sub_1 = str.substr(x-offset,offset);
string sub_2 = str.substr(0,x-offset);
sub_1 += sub_2;
str = sub_1;
}
}
};感悟:多看函数库,把程序方方面面想到,尽量一把AC,摒弃for循环。
相关文章推荐
- FFmpeg获取视频旋转角度rotate获取不到的问题解决
- 遇到的问题-----java The type java.lang.String cannot be resolved. It is indirectly referenced fr
- HDU 4998 Rotate 简单几何旋转问题
- JAVA中String字符串特殊字符的分割问题
- 完美解决某些字符插入到mysql数据库出现incorrect string value的问题
- android string.xml 转译、特殊字符问题
- Requst.QueryString[]取得的值是中文时,有时少了一个或者多个字符的问题
- IMP-00008: unrecognized statement in the export file: string的问题分析
- 遇到的问题-----java The type java.lang.String cannot be resolved. It is indirectly referenced fr
- 解决String字符里面存在"\r\n",用replaceAll解决不了的问题
- 如何解决这类问题:The string "--" is not permitted within comments.
- The request filtering module is configured to deny a request where the query string is too long问题解决
- cout<<字符数组 getline(流,string,delimit character) 遇到的问题
- 采集时输出字符问题Error: unterminated string literal
- MFC CString转换为string的宽字符问题解决
- string中含有‘\0’的字符转成char* 后被截断的问题
- 问题1-The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
- VC7(VS2002)调试时 std::string 超过15字符乱码问题
- 问题解决:javax.servlet.jsp.el.ELException: The "." operator was supplied with an index value of type "java.lang.String" to be applie
- hdu_1048_The Hardest Problem Ever_字符输入输出问题水题一道