数组求和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;}
- 数组求和leetcode
- leetcode求和
- 数组求和
- 数组求和
- 数组求和
- 数组求和
- 数组求和
- 数组求和
- LeetCode 303. Range Sum Query - Immutable(数组求和)
- Leetcode全求和问题
- leetcode求和题目
- [leetcode]求和类问题
- 不定数组求和问题
- 数组求和算法系列
- 数组求和算法系列
- 树状数组--区间求和
- 数组求和问题
- 数组求和问题
- 安装ADT的时候,提示“Cannot complete the install because one or more required items could not be
- 蓝牙解析(part6):BLE地址类型
- GZIP压缩解压类
- IO 文件合并
- win7+eclipse+mapreduce环境搭建
- 数组求和leetcode
- Java基本语法
- 448. Find All Numbers Disappeared in an Array
- 最小二乘法求色彩还原矩阵
- jquery.from.js 上传excel用法小结
- 服务端进程突然奔溃客会发生什么?
- 楼层效果
- quizup中的好友聊天框中的挑战消息是可点击的按钮,这个在安卓上怎么实现呢?如下图 求高人指教!
- 一份BUG提交规范