扫两边? void reorder(int input[]){ int length=sizeof(input)/sizeof(int); int output[length];
int last_negative_index=-1; int i=0; for (i=0; iif(input[i]<0) //want to move this number to the front. { last_negative_index++; // move this number before all the positive numbers. output[last_negative_index]=input[i]; } } // now we have last_negative_index for (i=0; iif (input[i]>0){ output[last_negative_index+1]=input[i]; last_negetive_index++; } } }
【在 b***r 的大作中提到】 : 扫两边? : void reorder(int input[]){ : int length=sizeof(input)/sizeof(int); : int output[length]; : : int last_negative_index=-1; : int i=0; : for (i=0; i: if(input[i]<0) //want to move this number to the front. : {
i*e
6 楼
Already found a bug in your code. --> int length=sizeof(input)/sizeof(int); This will always set lenght as 1. Because input is an int* pointer that's passed into the function, therefore sizeof(input) = sizeof(int*) = 4.
【在 b***r 的大作中提到】 : 扫两边? : void reorder(int input[]){ : int length=sizeof(input)/sizeof(int); : int output[length]; : : int last_negative_index=-1; : int i=0; : for (i=0; i: if(input[i]<0) //want to move this number to the front. : {
【在 b***r 的大作中提到】 : 扫两边? : void reorder(int input[]){ : int length=sizeof(input)/sizeof(int); : int output[length]; : : int last_negative_index=-1; : int i=0; : for (i=0; i: if(input[i]<0) //want to move this number to the front. : {
i*e
8 楼
From the discussions on SO, it seems unlikely there exist a solution with O( N) time and O(1) space (or maybe it's a research problem). Dutch national flag sorting can solve this in O(N) time and O(1) space, but is not stable. http://stackoverflow.com/questions/5347616/how-to-sort-an-integ
b*r
9 楼
牛人啊。bow
【在 i**********e 的大作中提到】 : Already found a bug in your code. : --> int length=sizeof(input)/sizeof(int); : This will always set lenght as 1. Because input is an int* pointer that's : passed into the function, therefore sizeof(input) = sizeof(int*) = 4.
我有一个思路,但是没编程!大家讨论一下! Hint:不知道大家有没有看过reverse the words in a sentence的例子,我简单说一下 I love you => you love I 程序: 1)reverse 每一个单词:I evol uoy 2) reverse 每整个句子: you love I 根据上面的例子,我们可以同理做这个题(我们先简化为只有前负后正):5,7,9,- 1,-4 (不知道有大家有木有灵感,其实我们可以把上题的空格和这题的正负交界同比) 1) reverse 正和负的sub array: 9,7,5,-4,-1 2) reverse 这两个sub arrays:-1,-4,5,7,9 所以,把上面的例子复杂化: -1,-4,5,7,9,-1231,-123,54,3462 1)reverse 前面两个正和负的sub arrays:-1,-4,9,7,5,-123,-1231,54, 3462 2)reverse 这两个sub arrays:-1,-4,-1231,-123,5,7,9,,54,3462 所以总的来说:找到两个sub arrays 且前面的sub array都是正数,后面的sub array 都是负数就用这种方法来in place地换位置。请高手测评一下!
【在 p****z 的大作中提到】 : 我有一个思路,但是没编程!大家讨论一下! : Hint:不知道大家有没有看过reverse the words in a sentence的例子,我简单说一下 : I love you => you love I : 程序: : 1)reverse 每一个单词:I evol uoy : 2) reverse 每整个句子: you love I : 根据上面的例子,我们可以同理做这个题(我们先简化为只有前负后正):5,7,9,- : 1,-4 (不知道有大家有木有灵感,其实我们可以把上题的空格和这题的正负交界同比) : 1) reverse 正和负的sub array: 9,7,5,-4,-1 : 2) reverse 这两个sub arrays:-1,-4,5,7,9
One method I can think of is scanning once to find how many positive and negative numbers are. Then scan second time and put every number is its correct final position. This should be O(N) and O(1)
Think again, that's not O(1) space. Unless you calculate the correct positions and store them in O(n) space, you cannot achieve O(n) in time.
【在 g***o 的大作中提到】 : One method I can think of is scanning once to find how many positive and : negative numbers are. Then scan second time and put every number is its : correct final position. This should be O(N) and O(1)
m*e
17 楼
say you have n negatives neg = 0; //start slot for negative numbers pos = n; //start slot for positive numbers //start examining array[0-N] i = 0 while(negif array[i] < 0 swap array[neg] and array[i], neg++ else swap array[pos] and array[i], pos++ i++
Think again, that's not O(1) space. Unless you calculate the correct positions and store them in O(n) space, you cannot achieve O(n) in time.
【在 g**********y 的大作中提到】 : Think again, that's not O(1) space. : Unless you calculate the correct positions and store them in O(n) space, you : cannot achieve O(n) in time.
g*k
18 楼
read again, it requires stable
【在 m******e 的大作中提到】 : say you have n negatives : neg = 0; //start slot for negative numbers : pos = n; //start slot for positive numbers : //start examining array[0-N] : i = 0 : while(neg: if array[i] < 0 swap array[neg] and array[i], neg++ : else swap array[pos] and array[i], pos++ : i++ :
m*e
19 楼
it is stable
【在 g*****k 的大作中提到】 : read again, it requires stable
g*k
20 楼
think again!
【在 m******e 的大作中提到】 : it is stable
g*y
21 楼
1, -1, 2, -2, 3, -3, 4, -4 so -1 should go pos 0, swap, ok -1, 1, 2, -2, 3, -3, 4, -4 -2 should go pos 1, where should 1 go? pos 4, which is 3 now, swap. Then where does 3 go? you don't know, unless you calculate it.
【在 m******e 的大作中提到】 : say you have n negatives : neg = 0; //start slot for negative numbers : pos = n; //start slot for positive numbers : //start examining array[0-N] : i = 0 : while(neg: if array[i] < 0 swap array[neg] and array[i], neg++ : else swap array[pos] and array[i], pos++ : i++ :
m*e
22 楼
ok, my bad, keep track of range [0, neg) and [numNeg, pos), and do not rearrange those (since they are already in the correct place) use [neg, numNeg) and [pos, N) as circular buffer for un-processed elements #include #include #include using namespace std; void print(const vector& input) { for(size_t i = 0; i < input.size(); ++i) { cout << input[i] << " "; } cout << endl; } int numNegatives(const vector& input) { int cnt = 0; for(size_t i = 0; i < input.size(); ++i) { if(input[i] < 0) ++cnt; } return cnt; } void shuffleNegPos(vector & input, size_t numNeg) { size_t neg = 0; size_t pos = numNeg; size_t i = 0; while(neg< numNeg && pos < input.size()) { if((i >= neg && i < numNeg) || i >= pos ) { if(input[i] < 0) { int temp = input[neg]; input[neg] = input[i]; input[i] = temp; ++neg ; } else { int temp = input[pos]; input[pos] = input[i]; input[i] = temp; ++pos ; } } //print(input); ++i; } } int main(int argc, const char* argv[]) { std::vector input; for(int i=1; i < argc; ++i) { int n; if(sscanf(argv[i], "%d", &n) > 0) { input.push_back(n); } } shuffleNegPos(input, numNegatives(input)); print(input); return 0; }
扫两边? void reorder(int input[]){ int length=sizeof(input)/sizeof(int); int output[length];
int last_negative_index=-1; int i=0; for (i=0; iif(input[i]<0) //want to move this number to the front. { last_negative_index++; // move this number before all the positive numbers. output[last_negative_index]=input[i]; } } // now we have last_negative_index for (i=0; iif (input[i]>0){ output[last_negative_index+1]=input[i]; last_negetive_index++; } } }
【在 b***r 的大作中提到】 : 扫两边? : void reorder(int input[]){ : int length=sizeof(input)/sizeof(int); : int output[length]; : : int last_negative_index=-1; : int i=0; : for (i=0; i: if(input[i]<0) //want to move this number to the front. : {
i*e
35 楼
Already found a bug in your code. --> int length=sizeof(input)/sizeof(int); This will always set lenght as 1. Because input is an int* pointer that's passed into the function, therefore sizeof(input) = sizeof(int*) = 4.
【在 b***r 的大作中提到】 : 扫两边? : void reorder(int input[]){ : int length=sizeof(input)/sizeof(int); : int output[length]; : : int last_negative_index=-1; : int i=0; : for (i=0; i: if(input[i]<0) //want to move this number to the front. : {
【在 b***r 的大作中提到】 : 扫两边? : void reorder(int input[]){ : int length=sizeof(input)/sizeof(int); : int output[length]; : : int last_negative_index=-1; : int i=0; : for (i=0; i: if(input[i]<0) //want to move this number to the front. : {
i*e
37 楼
From the discussions on SO, it seems unlikely there exist a solution with O( N) time and O(1) space (or maybe it's a research problem). Dutch national flag sorting can solve this in O(N) time and O(1) space, but is not stable. http://stackoverflow.com/questions/5347616/how-to-sort-an-integ
b*r
38 楼
牛人啊。bow
【在 i**********e 的大作中提到】 : Already found a bug in your code. : --> int length=sizeof(input)/sizeof(int); : This will always set lenght as 1. Because input is an int* pointer that's : passed into the function, therefore sizeof(input) = sizeof(int*) = 4.
我有一个思路,但是没编程!大家讨论一下! Hint:不知道大家有没有看过reverse the words in a sentence的例子,我简单说一下 I love you => you love I 程序: 1)reverse 每一个单词:I evol uoy 2) reverse 每整个句子: you love I 根据上面的例子,我们可以同理做这个题(我们先简化为只有前负后正):5,7,9,- 1,-4 (不知道有大家有木有灵感,其实我们可以把上题的空格和这题的正负交界同比) 1) reverse 正和负的sub array: 9,7,5,-4,-1 2) reverse 这两个sub arrays:-1,-4,5,7,9 所以,把上面的例子复杂化: -1,-4,5,7,9,-1231,-123,54,3462 1)reverse 前面两个正和负的sub arrays:-1,-4,9,7,5,-123,-1231,54, 3462 2)reverse 这两个sub arrays:-1,-4,-1231,-123,5,7,9,,54,3462 所以总的来说:找到两个sub arrays 且前面的sub array都是正数,后面的sub array 都是负数就用这种方法来in place地换位置。请高手测评一下!
【在 p****z 的大作中提到】 : 我有一个思路,但是没编程!大家讨论一下! : Hint:不知道大家有没有看过reverse the words in a sentence的例子,我简单说一下 : I love you => you love I : 程序: : 1)reverse 每一个单词:I evol uoy : 2) reverse 每整个句子: you love I : 根据上面的例子,我们可以同理做这个题(我们先简化为只有前负后正):5,7,9,- : 1,-4 (不知道有大家有木有灵感,其实我们可以把上题的空格和这题的正负交界同比) : 1) reverse 正和负的sub array: 9,7,5,-4,-1 : 2) reverse 这两个sub arrays:-1,-4,5,7,9
One method I can think of is scanning once to find how many positive and negative numbers are. Then scan second time and put every number is its correct final position. This should be O(N) and O(1)
Think again, that's not O(1) space. Unless you calculate the correct positions and store them in O(n) space, you cannot achieve O(n) in time.
【在 g***o 的大作中提到】 : One method I can think of is scanning once to find how many positive and : negative numbers are. Then scan second time and put every number is its : correct final position. This should be O(N) and O(1)
m*e
46 楼
say you have n negatives neg = 0; //start slot for negative numbers pos = n; //start slot for positive numbers //start examining array[0-N] i = 0 while(negif array[i] < 0 swap array[neg] and array[i], neg++ else swap array[pos] and array[i], pos++ i++
Think again, that's not O(1) space. Unless you calculate the correct positions and store them in O(n) space, you cannot achieve O(n) in time.
【在 g**********y 的大作中提到】 : Think again, that's not O(1) space. : Unless you calculate the correct positions and store them in O(n) space, you : cannot achieve O(n) in time.
g*k
47 楼
read again, it requires stable
【在 m******e 的大作中提到】 : say you have n negatives : neg = 0; //start slot for negative numbers : pos = n; //start slot for positive numbers : //start examining array[0-N] : i = 0 : while(neg: if array[i] < 0 swap array[neg] and array[i], neg++ : else swap array[pos] and array[i], pos++ : i++ :
m*e
48 楼
it is stable
【在 g*****k 的大作中提到】 : read again, it requires stable
g*k
49 楼
think again!
【在 m******e 的大作中提到】 : it is stable
g*y
50 楼
1, -1, 2, -2, 3, -3, 4, -4 so -1 should go pos 0, swap, ok -1, 1, 2, -2, 3, -3, 4, -4 -2 should go pos 1, where should 1 go? pos 4, which is 3 now, swap. Then where does 3 go? you don't know, unless you calculate it.
【在 m******e 的大作中提到】 : say you have n negatives : neg = 0; //start slot for negative numbers : pos = n; //start slot for positive numbers : //start examining array[0-N] : i = 0 : while(neg: if array[i] < 0 swap array[neg] and array[i], neg++ : else swap array[pos] and array[i], pos++ : i++ :