@snuffles
2015-04-04T11:39:43.000000Z
字数 1608
阅读 891
leetcode
20150404
4.long time no see 终于AC了我的亲娘啊,但是为何第一行行一定要放在函数外面才能AC??
20150309
1.我的亲娘啊,必须把VS2010重装了,太费劲了配置link,用gcc写makefile的日子真是美好啊
2.一开始没有用ide调试起来好痛苦
3.测试用例想不明白是怎么输出错的
int b[2]={-1,-1};//
int *searchRange(int A[], int n, int target) {
int flag=0;
int start=0,end=0;
int pos=0;
for(pos=0;pos<n;pos++){
if(A[pos]==target){
flag=1;
start=pos;
break;
}
}
if(flag==1){
for(pos=n-1;pos>=0;pos--){
if(A[pos]==target){
end=pos;
b[0]=start;
b[1]=end;
return b;
}
}
}
b[0]=-1;
b[1]=-1;
return b;
}
Input: [2,2], 1
Output: [0,0]
Expected: [-1,-1]
int b[2]={-1,-1};
int *searchRange(int A[], int n, int target) {
int flag=0;
int start,end;
for(int pos=0;pos<=n-1;pos++){
if(flag==0 && A[pos]==target){
flag=1;
start=pos;
}
if(flag==1 && (pos+1)==n)
{
b[0]=start;
b[1]=n-1;
return b;
}
if(flag==1 && A[pos]==!target){
end=pos;
b[0]=start;
b[1]=end;
return b;
}
}
return b;
}
开始想时间上有要求是不是要用二分(请跟我唱~这就是傻~分明写不清楚~)WA2
int b[2]={0,0};
int half(int start,int end)
{
int n=start+end;
if(n%2==0) return n/2;
else return (n+1)/2;
}
int *searchRange(int A[], int n, int target) {
int start=0;
int end=n-1;
int pos=0;
if (target<A[0]||target>A[n-1]){
b[0]=-1;
b[1]=-1;
return b;
}
for(;;){
if(start==end||A[start]==target){
b[0]=start;
b[1]=end;
return b;
}
pos=half(start,end);
if(A[pos]<target) start=pos+1;
if(A[pos]>target) end=pos-1;
if(A[pos]==target){
if(pos+1<n && A[pos+1]==target){
start=pos;
for(;pos!=end;end--){
if(A[end]==target){
b[0]=start;
b[1]=end;
return b;
}
}
}
if(pos-1>=0 && A[pos-1]==target){
end=pos;
for(;pos!=start;start++){
if(A[start]==target){
b[0]=start;
b[1]=end;
return b;
}
}
}
else{
b[0]=pos;
b[1]=pos;
return b;
}
}
}
b[0]=-1;
b[1]=-1;
return b;
}
int main()
{
int A[1]={1};
int n=1;
int target=1;
searchRange(A,n,target);
cout<<b[0]<<endl;
cout<<b[1]<<endl;
return 0;
}