数组求和leetcode

来源:互联网 发布:linux vlan 编辑:程序博客网 时间:2024/04/29 09:24

leetcode上面的有关数组求和的题:
求数组中2个数值的和为s,例如array(-1,2,3,5,6) s=8 结果为(2,6,) (3,5)

法一:先排序后O(NLOGN) +固定一个然后二分查找:NLOG(N)最终时间复杂度:NLOG(N)或者+二头夹 O(n)最终时间复杂度:NLOG(N) function findSum($arr,$s){    if(false===is_array($arr)) return false;     sort($arr);    $countNum=count($arr);    for($i=0;$i<$countNum;$i++){      $j=$countNum-1;       while($i<$j){          $tar=$arr[$i]+$arr[$j];          if($tar==$s){                 $result[]=$arr[$i].$arr[$j];                 break;          }else if($tar>$s){                $j--;          }else{                $i++;          }         }   }//end for   return $result;}或者使用hashmap 遍历一遍 对每个数value,查找是否存在$s-$value 时间复杂度为O(n)

2.1.7 3Sum
描述
Given an array S of n integers, are there elements a; b; c in S such that a+b+c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
• Elements in a triplet (a; b; c) must be in non-descending order. (ie, a  b  c)
• The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)

//排序后固定一个,然后两边夹 O(n2);function findSum($arr,$k=3){    if(false===is_array($arr)||($countNum=count($arr))<$k) return false;     sort($arr);    for($i=0;$i<$countNum;$i++){          $j=$i+1;          $g=$countNum-1;          while($j<$g){              $sum=$arr[$i]+$arr[$j]+$arr[$g];              if($sum<0)                  $j++;              else if($sum>0){                  $g--;               }else                   break;                           }          if($j<$g&&$sum==0)                $result[]=array($arr[$i],$arr[$j],$arr[$g]);     }      return $result;}

2.1.8 3Sum Closest
描述
Given an array S of n integers, find three integers in S such that the sum is closest to a given number,
target. Return the sum of the three integers. You may assume that each input would have exactly one
solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

a+b+c比较接近target即求sum =a+b+c-taget最小的采用深度优先遍历吧~最慢了采用固定一个然后夹O(n2)function findSum($arr,$target){    if(false===is_array($arr)) return false;    sort($arr);    $countNum=count($arr);    $minD=10000;    $result=array();    for($i=0;$i<$countNum;$i++){        $j=$i+1;        $g=$countNum-1;        while($j<$g){            $sum=abs($arr[$i]+$arr[$j]+$arr[$g]-$target);            if($sum<$minD){                $minD=$sum;                $result=$arr[$i].'-'.$arr[$j].'-'.$arr[$g];                $j++;            }else if($sum>=$minD){//这里写等号,是因为题目中说假设结果只有一个                $g--;            }        }//end while    }//end for    return $result;}

2.1.9 4Sum
描述
Given an array S of n integers, are there elements a; b; c, and d in S such that a+b+c+d = target?
Find all unique quadruplets in the array which gives the sum of target.
Note:
2.1.13
• Elements in a quadruplet (a; b; c; d) must be in non-descending order. (ie, a  b  c  d)
• e solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)

//排序后固定2个,然后两边夹 O(n3);或者先缓冲前二个数的和,然后遍历后2个树 时间复杂度O(n3)

给定一整数n,求1~n中的数和为n的
例如n=15 结果为 1+2+3+4+5=15; 4+5+6=15;7+8=15 输入1~5 4~6 7~8

function f($n){    $result=array();    $begin=1;    $i=2;    $sum=$begin;    while($begin<$n){        $sum+=$i;        while($sum>$n){          $sum-=$begin;          $begin++;         }         if($sum==$n&&$begin<$n){           $result[]=$begin.'~'.$i;          }        $i++;     }     return $result;}上面的解法不完善,根据题意,while循环$begin < intval(($n+1)/2)function f($n){    $result=array();    $begin=1;$mid=intval($n+1/2);    $i=2;    $sum=$begin;    while($begin<$mid){        $sum+=$i;        while($sum>$n){          $sum-=$begin;          $begin++;         }         if($sum==$n&&$begin<$mid){           $result[]=$begin.'~'.$i;          }        $i++;     }     return $result;}
0 0
原创粉丝点击