h*g
2 楼
2)/*
* Given an array and a value, remove all instances of that
* value in place and return the new length. The order of
* elements can be changed. It doesn't matter what you leave
* beyond the new length.
*/
size_t remove_elem(T* array, size_t len, T elem) {
}
这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
换的次数
////////////////////////////////////////////////
这题从前到后sweep一遍就OK了吧? quick sort得咋做?
* Given an array and a value, remove all instances of that
* value in place and return the new length. The order of
* elements can be changed. It doesn't matter what you leave
* beyond the new length.
*/
size_t remove_elem(T* array, size_t len, T elem) {
}
这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
换的次数
////////////////////////////////////////////////
这题从前到后sweep一遍就OK了吧? quick sort得咋做?
h*s
3 楼
【 以下文字转载自 Travel 讨论区 】
发信人: hypnos (sleep!+sleep!), 信区: Travel
标 题: 北京doubletree已经变成Cat 2,要10000 Hilton点了
发信站: BBS 未名空间站 (Fri May 10 18:33:30 2013, 美东)
前几天还是Cat 1,只要5000,现在已经成10000了。
过几天会不会继续涨到3万?
发信人: hypnos (sleep!+sleep!), 信区: Travel
标 题: 北京doubletree已经变成Cat 2,要10000 Hilton点了
发信站: BBS 未名空间站 (Fri May 10 18:33:30 2013, 美东)
前几天还是Cat 1,只要5000,现在已经成10000了。
过几天会不会继续涨到3万?
N*k
4 楼
昨天刚领回家,三个月大。
超级胆小,到现在不敢主动吃不敢主动喝,都要用手喂着才行。
倒是可以憋一晚上nnbb,早上痛快的发泄了。
可是刚才发现crate里面湿了一坨,难道是意外?
超级胆小,到现在不敢主动吃不敢主动喝,都要用手喂着才行。
倒是可以憋一晚上nnbb,早上痛快的发泄了。
可是刚才发现crate里面湿了一坨,难道是意外?
e*l
6 楼
前后2个指针,前面往后走,每次遇到given value,把后面指针的元素填过来,然后后
面的往前一步。直到相遇。
面的往前一步。直到相遇。
g*n
7 楼
。。是不是最近换的人太多了?
呵呵
呵呵
h*n
8 楼
好小啊,小可爱
p*2
9 楼
static int Remove(int[] arr, int v)
{
int start=0;
int end=arr.length-1;
while(start {
while(arr[start]!=v)
start++;
while(arr[end]==v)
end--;
if(start {
arr[start]=arr[end];
arr[end]=v;
start++;
end--;
}
}
return start;
}
{
int start=0;
int end=arr.length-1;
while(start
while(arr[start]!=v)
start++;
while(arr[end]==v)
end--;
if(start
arr[start]=arr[end];
arr[end]=v;
start++;
end--;
}
}
return start;
}
a*e
10 楼
马上定了无锡的一晚。还是5000点~
p*2
12 楼
QuickSort
static void QuickSort(int[] arr, int start, int end)
{
int pivot=arr[(start+end)/2];
int l=start;
int r=end;
while(l {
while(arr[l] l++;
while(arr[r]>pivot)
r--;
if(l<=r)
{
int tmp=arr[l];
arr[l]=arr[r];
arr[r]=tmp;
l++;
r--;
}
}
if(start QuickSort(arr,start,l-1);
if(end>l)
QuickSort(arr,l,end);
}
static void QuickSort(int[] arr, int start, int end)
{
int pivot=arr[(start+end)/2];
int l=start;
int r=end;
while(l
while(arr[l]
while(arr[r]>pivot)
r--;
if(l<=r)
{
int tmp=arr[l];
arr[l]=arr[r];
arr[r]=tmp;
l++;
r--;
}
}
if(start
if(end>l)
QuickSort(arr,l,end);
}
H*e
15 楼
这跟quicksort有啥关系?
从前往后一遍就行了吧,两个指针,一个指向有用的数据最后一个,一个current,如果
current不是target,就把此数据考向前面的指针
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
从前往后一遍就行了吧,两个指针,一个指向有用的数据最后一个,一个current,如果
current不是target,就把此数据考向前面的指针
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
w*c
19 楼
后悔没多订几晚啊。。。。
Z*i
23 楼
才三个月,晚上起来让他上一次厕所吧。
a*n
24 楼
这个应该和 DUTCH Flag那个问题比较象吧。。。。
w*x
25 楼
这一变岂不是HHonors points更不值钱了
f*n
26 楼
三个月,晚上还是辛苦点,起来放出去一次,以后更容易训练。
n*d
27 楼
赞
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
p*i
32 楼
三个月不大可能憋整夜的,不如放出去尿一下的好
h*w
36 楼
good, mark 一下!
Z*i
41 楼
通常来讲三个月,只能HOLD 三小时。。。。
z*t
42 楼
在博客中分析了这个问题:
http://codercareer.blogspot.com/2012/02/no-32-remove-numbers-in
博客中的思路和peking2的思路是一样的。博客中的代码删除了两行不必要的代码(If
中的++和--不是必须的,因为在while循环里这两个操作都要做)。
【在 p*****2 的大作中提到】
:
: 是。
http://codercareer.blogspot.com/2012/02/no-32-remove-numbers-in
博客中的思路和peking2的思路是一样的。博客中的代码删除了两行不必要的代码(If
中的++和--不是必须的,因为在while循环里这两个操作都要做)。
【在 p*****2 的大作中提到】
:
: 是。
y*u
44 楼
欢迎小香肠!才3个月,很难控制的,多训练长大了就好了
j*w
45 楼
void swap(T *array, size_t i, size_t j)
{
if (i != j)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
size_t remove_elem(T* array, size_t len, T elem)
{
size_t i = 0;
size_t j = len-1;
while (( i < len ) && ( i < j ))
{
if (array[i].equals(elem))
{
swap(array, i, j);
j--;
}
else
{
i++;
}
}
return j+1;
}
{
if (i != j)
{
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
size_t remove_elem(T* array, size_t len, T elem)
{
size_t i = 0;
size_t j = len-1;
while (( i < len ) && ( i < j ))
{
if (array[i].equals(elem))
{
swap(array, i, j);
j--;
}
else
{
i++;
}
}
return j+1;
}
n*9
46 楼
我怎么看要2W点
j*x
47 楼
remove erase idiom
把符合条件的先放到末尾,完成之后抹掉
或者前后两个指针,一个维护应该保留的元素,一个维护当前插入的位置
覆盖不需要保留的元素即可
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
把符合条件的先放到末尾,完成之后抹掉
或者前后两个指针,一个维护应该保留的元素,一个维护当前插入的位置
覆盖不需要保留的元素即可
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
H*1
49 楼
不用sort啊
就是一个指针在前,一个指针在后
前面指针遇到elem,就跟后一个指针换,后一个指针前移一位
前面指针如果不是elem,就后移一位,直到前面指针和后面指针相等
length就是后面指针-array
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
就是一个指针在前,一个指针在后
前面指针遇到elem,就跟后一个指针换,后一个指针前移一位
前面指针如果不是elem,就后移一位,直到前面指针和后面指针相等
length就是后面指针-array
【在 h*****g 的大作中提到】
: 2)/*
: * Given an array and a value, remove all instances of that
: * value in place and return the new length. The order of
: * elements can be changed. It doesn't matter what you leave
: * beyond the new length.
: */
: size_t remove_elem(T* array, size_t len, T elem) {
: }
: 这个题目他是想让用quick sort 类似的方法 从两头同时开始找,然后换,尽量减少交
: 换的次数
e*e
50 楼
I think "If 中的++和--不是必须的" is a little bit inefficient.
Because it's 100% sure that after if clause, arr[start] != v and arr[end]==v
is true. We can just increase both start and end variable by 1 inside if
clause. Why wait till enter the next round of the outer while loop and let
two inner while loops do the increasing by 1 work?
If
【在 z******t 的大作中提到】
: 在博客中分析了这个问题:
: http://codercareer.blogspot.com/2012/02/no-32-remove-numbers-in
: 博客中的思路和peking2的思路是一样的。博客中的代码删除了两行不必要的代码(If
: 中的++和--不是必须的,因为在while循环里这两个操作都要做)。
Because it's 100% sure that after if clause, arr[start] != v and arr[end]==v
is true. We can just increase both start and end variable by 1 inside if
clause. Why wait till enter the next round of the outer while loop and let
two inner while loops do the increasing by 1 work?
If
【在 z******t 的大作中提到】
: 在博客中分析了这个问题:
: http://codercareer.blogspot.com/2012/02/no-32-remove-numbers-in
: 博客中的思路和peking2的思路是一样的。博客中的代码删除了两行不必要的代码(If
: 中的++和--不是必须的,因为在while循环里这两个操作都要做)。
i*e
52 楼
这里首先有个判定条件错了,应该是 ( i <= j ) 而不是 ( i < j )。
反例:【4,5】,remove 5.
还有一点就是做了很多不必要的交换,例如:
【1,2,2,2,2】,remove 2.这里用了不必要的三次交换。
反例:【4,5】,remove 5.
还有一点就是做了很多不必要的交换,例如:
【1,2,2,2,2】,remove 2.这里用了不必要的三次交换。
i*e
53 楼
试试:
数组【1】,remove 2.
If
【在 z******t 的大作中提到】
: 在博客中分析了这个问题:
: http://codercareer.blogspot.com/2012/02/no-32-remove-numbers-in
: 博客中的思路和peking2的思路是一样的。博客中的代码删除了两行不必要的代码(If
: 中的++和--不是必须的,因为在while循环里这两个操作都要做)。
数组【1】,remove 2.
If
【在 z******t 的大作中提到】
: 在博客中分析了这个问题:
: http://codercareer.blogspot.com/2012/02/no-32-remove-numbers-in
: 博客中的思路和peking2的思路是一样的。博客中的代码删除了两行不必要的代码(If
: 中的++和--不是必须的,因为在while循环里这两个操作都要做)。
相关阅读
weruva的鸡肉罐头给沫沫买了蛮蛮睡的那个perch bed捡回来的狗狗带狗坐飞机,cargo?奔羞射卖萌的大宝~~!顺便问狗狗打喷嚏的问题~~缘分啊缘分如果遭遇瘦的皮包骨的狗狗,要不要上前向主人问个究竟?求助:第一次带两个4岁猫猫 上飞机- 国内航班 飞5个小时Blue Buffalo cat litter的初始用体会明天送去咔嚓了 有些不忍心家有胖猫,厕所必须DIY~~~真心求建议朋友的朋友的金毛飞机托运死亡弗吉尼亚威廉斯堡附近可怜小猫求包养我崩溃了 彻底无语了求教猫猫搬家(从东部到西部)既然大家如此执着于拖鞋,我就只好让小仙儿再BSO一下了请推荐Houston地区的兽医[BSSD]发包子 (包子发完了)三个背影