LeetCode001 Two Sum C语言
1.Givenanarrayofintegers,returnindicesofthetwonumberssuchthattheyadduptoaspecifictarget.Youmayassumethateachinputwouldhaveexactlyonesolution.Example:Givennums=[2,7,11,15],target=9,Becausenums[0]+nums[1]=2+7=9,return[0,1].UPDATE(2016/2/13):Thereturnformathadbeenchangedtozero-basedindices.Pleasereadtheaboveupdateddescriptioncarefully.Subscribetoseewhichcompaniesaskedthisquestio
/***Note:Thereturnedarraymustbemalloced,assumecallercallsfree().*/int*twoSum(int*nums,intnumsSize,inttarget){inti,j;int*a=(int*)malloc(sizeof(int)*2);for(i=0;i<numsSize;i++){for(j=i+1;j<numsSize;j++){if(nums[i]+nums[j]==target){a[0]=i;a[1]=j;break;}}}//printf("%d",a[1]);returna;}
LeetCode第一题!!!!没想到两层循环就解决了,想想还有点激动。看了网上才知道这样
时间复杂度O(N*2)。
好像快点的话还可以hash表?
有机会再说吧[%>_<%]
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。