提交 b99eff18 编写于 作者: 锅巴胸's avatar 锅巴胸

feature: 两数之和

php/js
上级
.idea
.git
/**
* 获取数组中的两个值之和等于指定值的下标
* @param {number[]} nums 数组
* @param {*} target 指定值
*/
const sum = (nums, target) => {
for (let i = 0; i < nums.length; ++i) if (nums[i] < 9) {
for (let ii = i + 1; ii < nums.length; ++ii) {
if (nums[i] + nums[ii] === target) {
return [i, ii];
}
}
}
};
console.log(sum([123, 11, 223, 1, 2, 3, 4, 6, 9, 10, 11], 9))
\ No newline at end of file
<?php
/**
* 获取数组中的两个值之和等于指定值的下标
* @param $nums array 数组
* @param $target int 指定值
* @return int[]
*/
function sum(array $nums, int $target): array
{
for ($i = 0; $i < count($nums); ++$i) if ($nums[$i] < 9) {
for ($ii = $i + 1; $ii < count($nums); ++$ii) {
echo "<pre>";
if ($nums[$i] + $nums[$ii] === $target) {
return [$i, $ii];
}
}
}
}
print_r(sum([123, 11, 223, 1, 2, 3, 4, 6, 9, 10, 11], 9));
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册