英文:
return the unique pairs of integers from a an array that add up to a specific sum
问题
这是我迄今为止得到的内容。它可以工作,但它不会检查是否已经计算了一对,这可能会导致重复。我应该怎么做?
英文:
I am writing a function that takes in an array of integers and returns the number of unique pairs of integers that add up to a specific sum. For example, given the array [2, 4, 6, 2, 8, 4, 7, 2, 5, 9] and a sum of 10, the function should return 2 + the pairs number[].
function countPairs($arr, $sum) {
$pairCount = 0;
$pairs = [];
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i + 1; $j < count($arr); $j++) {
if ($arr[$i] + $arr[$j] == $sum) {
$pairCount++;
$pairs[] = [$arr[$i], $arr[$j]];
}
}
}
return ['pairCount' => $pairCount, 'pairs' => $pairs];
}
this is what I got so far. it works but it doesn't check whether a pair has already been counted or not, which can result in duplicates. what should do ?
答案1
得分: 0
以下是代码的翻译部分:
一个解决方法是将值合并在一起并将它们作为键。然后只需检查数组是否在对数数组中。我还对值进行了排序,以使8,2和2,8成为相同的。
function countPairs($arr, $sum) {
$pairCount = 0;
$pairs = [];
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i + 1; $j < count($arr); $j++) {
if ($arr[$i] + $arr[$j] == $sum) {
$pair = [$arr[$i], $arr[$j]];
asort($pair);
$key = implode(".", $pair);
if (in_array($pair, $pairs) == false) {
$pairs[$key] = $pair;
$pairCount++;
}
}
}
}
return ['pairCount' => $pairCount, 'pairs' => $pairs];
}
英文:
One solution is combine the values together and make them the key. Then just check to see if the array is in the pairs array. I also sorted the values so that 8,2 and 2,8 will be the same.
function countPairs($arr, $sum) {
$pairCount = 0;
$pairs = [];
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i + 1; $j < count($arr); $j++) {
if ($arr[$i] + $arr[$j] == $sum) {
$pair = [$arr[$i], $arr[$j]];
asort($pair);
$key = implode(".",$pair);
if(in_array($pair,$pairs) == false){
$pairs[$key] = $pair;
$pairCount++;
}
}
}
}
return ['pairCount' => $pairCount, 'pairs' => $pairs];
}
答案2
得分: 0
这段代码的翻译如下:
这个代码应该能解决问题:
if (!in_array([$arr[$i], $arr[$j]], $pairs) && !in_array([$arr[$j], $arr[$i]], $pairs)) {}
它检查当前数值对是否已经被计数并存储在`$pairs`数组中。如果这对数值尚未在数组中,它将与唯一数值对的数量`$pairCount`一起添加到`$pairs`数组中。
function countPairs($arr, $sum) {
$pairCount = 0;
$pairs = [];
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i + 1; $j < count($arr); $j++) {
if ($arr[$i] + $arr[$j] == $sum) {
// 检查该数值对是否已被计数
if (!in_array([$arr[$i], $arr[$j]], $pairs) && !in_array([$arr[$j], $arr[$i]], $pairs)) {
$pairCount++;
$pairs[] = [$arr[$i], $arr[$j]];
}
}
}
}
return ['pairCount' => $pairCount, 'pairs' => $pairs];
}
$result = countPairs([2, 4, 6, 2, 8, 4, 7, 2, 5, 9], 10);
echo "唯一数值对的数量: " . $result['pairCount'] . "\n";
echo "数值对:\n";
foreach ($result['pairs'] as $pair) {
echo " [" . $pair[0] . ", " . $pair[1] . "]\n";
}
英文:
This should so the trick:
if (!in_array([$arr[$i], $arr[$j]], $pairs) && !in_array([$arr[$j], $arr[$i]], $pairs)) {}
It checks if the current pair of values has already been counted and stored in the $pairs
array. If the pair is not in the array yet, it is added to the $pairs
array along with the number of unique pairs, $pairCount
.
function countPairs($arr, $sum) {
$pairCount = 0;
$pairs = [];
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i + 1; $j < count($arr); $j++) {
if ($arr[$i] + $arr[$j] == $sum) {
// Check if the pair has already been counted
if (!in_array([$arr[$i], $arr[$j]], $pairs) && !in_array([$arr[$j], $arr[$i]], $pairs)) {
$pairCount++;
$pairs[] = [$arr[$i], $arr[$j]];
}
}
}
}
return ['pairCount' => $pairCount, 'pairs' => $pairs];
}
$result = countPairs([2, 4, 6, 2, 8, 4, 7, 2, 5, 9], 10);
echo "Number of unique pairs: " . $result['pairCount'] . "\n";
echo "Pairs:\n";
foreach ($result['pairs'] as $pair) {
echo " [" . $pair[0] . ", " . $pair[1] . "]\n";
}
答案3
得分: 0
以下是代码部分的中文翻译:
首先,从数组中筛选出重复项比尝试确定结果是否已经出现要简单得多。
然后,您还可以对输入进行排序,如果您正在处理过大的值,可以提前退出。
最后,查找重复项和计算结果是两个不一定需要耦合在一起的单独操作。
function findPairs($arr, $sum) {
$arr = array_unique($arr);
sort($arr);
$pairs = [];
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i + 1; $j < count($arr); $j++) {
if ($arr[$i] + $arr[$j] == $sum) {
$pairs[] = [$arr[$i], $arr[$j]];
echo "!";
break;
} else if ($arr[$i] + $arr[$j] > $sum) {
echo 'X';
break;
}
echo '.';
}
echo PHP_EOL;
}
return $pairs;
}
$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5];
$pairs = findPairs($arr, 15);
$count = count($pairs);
var_dump(json_encode($pairs), $count);
输出:
............!
..........!
........!
......!
....!
..!
!
X
X
X
X
X
X
X
X
X
X
X
X
string(48) "[[1,14],[2,13],[3,12],[4,11],[5,10],[6,9],[7,8]]"
int(7)
您甚至可以进一步进行二分搜索数组以查找$sum - $arr[$i]
,而不是在内部循环中遍历整个子数组。
英文:
It's far more simple to filter duplicates from the array first, rather than trying to figure out if your result has already been seen.
Then you can also sort the input and then quit early if you're touching values that are too large.
Lastly, finding duplicates and counting the result are two separate operations which don't necessarily need to be coupled like this.
function findPairs($arr, $sum) {
$arr = array_unique($arr);
sort($arr);
$pairs = [];
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i + 1; $j < count($arr); $j++) {
if ($arr[$i] + $arr[$j] == $sum) {
$pairs[] = [$arr[$i], $arr[$j]];
echo "!";
break;
} else if ($arr[$i] + $arr[$j] > $sum) {
echo 'X';
break;
}
echo '.';
}
echo PHP_EOL;
}
return $pairs;
}
$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5];
$pairs = findPairs($arr, 15);
$count = count($pairs);
var_dump(json_encode($pairs), $count);
Output:
............!
..........!
........!
......!
....!
..!
!
X
X
X
X
X
X
X
X
X
X
X
X
string(48) "[[1,14],[2,13],[3,12],[4,11],[5,10],[6,9],[7,8]]"
int(7)
You could even go a step further and do a binary search on the array for $sum - $arr[$i]
instead of looping through the entire sub-array in the inner loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论