m*****t 发帖数: 334 | 1 void revert(char *str)
How to revert a string in-place and only scan the string once? (Remember, if
you use strlen() then it counts as one scan) |
j********x 发帖数: 2330 | |
x*********s 发帖数: 2604 | 3 require one more parameter, revert(char* str, int n); |
K******g 发帖数: 1870 | 4 在你没有到达string尾端之前,你怎么in-place反转?如果你要到达,肯定至少要过一
遍。想都不用想,不可能的事情。
if
【在 m*****t 的大作中提到】 : void revert(char *str) : How to revert a string in-place and only scan the string once? (Remember, if : you use strlen() then it counts as one scan)
|
K******g 发帖数: 1870 | 5 在你没有到达string尾端之前,你怎么in-place反转?如果你要到达,肯定至少要过一
遍。想都不用想,不可能的事情。
if
【在 m*****t 的大作中提到】 : void revert(char *str) : How to revert a string in-place and only scan the string once? (Remember, if : you use strlen() then it counts as one scan)
|
c***2 发帖数: 838 | 6 revert not reverse
an exmaple
str="abc"
foo(str)
str=="ABC"
revert(str)
str=="abc"
very simple |
r******d 发帖数: 308 | 7 This function will revert string "how are you?" to "you are how?"? |
s****u 发帖数: 1433 | 8 i think it is doable:
StringPointer=0;
while(String[StringPointer]!=null)
{
temp=String[StringPointer];
for(j=Stringpoint;j>0;j--) String[j]=string[j-1];
String[0]=temp;
StringPointer++;
} |
e********s 发帖数: 248 | 9 Looks like it is a O(n^2) algorithm. Worse than scan the string twice?
【在 s****u 的大作中提到】 : i think it is doable: : StringPointer=0; : while(String[StringPointer]!=null) : { : temp=String[StringPointer]; : for(j=Stringpoint;j>0;j--) String[j]=string[j-1]; : String[0]=temp; : StringPointer++; : }
|
b***J 发帖数: 40 | 10 acc
【在 s****u 的大作中提到】 : i think it is doable: : StringPointer=0; : while(String[StringPointer]!=null) : { : temp=String[StringPointer]; : for(j=Stringpoint;j>0;j--) String[j]=string[j-1]; : String[0]=temp; : StringPointer++; : }
|
s*****n 发帖数: 5488 | 11 送分题。
void reverseAString(char * str)
{
if (!str) return;
static char * strStart = str;
if (str)
reverseAString(str ++);
else
{
str--;
}
if (str > strStart)
{
swap(str, starStart);
str --;
strStart ++;
}
}
if
【在 m*****t 的大作中提到】 : void revert(char *str) : How to revert a string in-place and only scan the string once? (Remember, if : you use strlen() then it counts as one scan)
|
e********s 发帖数: 248 | 12 Kind of get your idea. But your code will definitely not work.
BTW, even without bugs, this algorithm still scans the string twice similar
as the simple ones, just making it unnecessarily more complex.
【在 s*****n 的大作中提到】 : 送分题。 : void reverseAString(char * str) : { : if (!str) return; : : static char * strStart = str; : if (str) : reverseAString(str ++); : else : {
|
s****u 发帖数: 1433 | 13 true. but the requirement does not say better than 'scan twice'?
【在 e********s 的大作中提到】 : Looks like it is a O(n^2) algorithm. Worse than scan the string twice?
|