【每日一题0906】数字间隔

:woman_mage:请编写一个函数,接收两个参数:一个是数字列表nums,一个是数字n。返回列表中任意两个数字的差值刚好是n的所有组合的个数。
示例:
输入:(nums=[1, 1, 5, 6, 9, 16, 27], n=4),输出3。因为组合情况可以是:(1,5), (1,5), (5,9)

题目难度:简单
题目来源:codewars

def int_diff(nums: list, n: int) -> int:
    pass

assert int_diff([1, 1, 5, 6, 9, 16, 27], 4) == 3
assert int_diff([1, 1, 3, 3], 2) == 4

def int_diff(nums: list, n: int):
count=0
for i in range(0,len(nums)):
for j in range(0,len(nums)):
if nums[i]-nums[j]==n:
count+=1
print(count)
return count

@jisonglin 同学太优秀了~~
考虑到题解的交流和分享,帮忙优化一下题解的格式哈,写成markdown格式的。
操作方法:将代码块的前后使用一对 ``` 符号包裹起来。
例如你的题解改成:

def int_diff(nums: list, n: int):
    count=0
    for i in range(0,len(nums)):
        for j in range(0,len(nums)):
            if nums[i]-nums[j]==n:
                count += 1
    return count
def int_diff(nums: list, n: int) -> tuple:
    count = 0
    result = list()
    for i in range(len(nums) - 1):
        for j in range(i + 1, len(nums)):
            # 计算绝对值
            if abs(nums[i] - nums[j]) == n:
                count += 1
                result.append((nums[i], nums[j]))
    return count, result
def int_diff(nums: list, n: int) -> int:
    # 定义匹配的组合个数
    group_count = 0
    # 第一层 for 循环
    for i in range(len(nums)):
        # 取第一个数
        num1 = nums[i]
        # 第二层 for 循环,以 i+1 开始
        for j in range(i + 1, len(nums)):
            # 取第二个数
            num2 = nums[j]
            # 判断两个数的差值是否为 n
            if abs(num2 - num1) == n:
                # 是则匹配的组合个数 +1
                group_count = group_count + 1
    # 返回匹配的组合个数
    return group_count

assert int_diff([1, 1, 5, 6, 9, 16, 27], 4) == 3
assert int_diff([1, 1, 3, 3], 2) == 4

image

   /**
     * 请编写一个函数,接收两个参数:一个是数字列表nums,一个是数字n。返回列表中任意两个数字的差值刚好是n的所有组合的个数。
     * 示例:
     * 输入:(nums=[1, 1, 5, 6, 9, 16, 27], n=4),输出3。因为组合情况可以是:(1,5), (1,5), (5,9)
     * @param arrayList
     * @param integer
     * @return
     */
    public Integer IntDiff(ArrayList<Integer> arrayList,Integer integer){
        int result =0;
        for (int i = 0; i < arrayList.size(); i++) {
            for (int j = i; j <arrayList.size() ; j++) {
                if (arrayList.get(j) - arrayList.get(i) == integer){
                    result++;
                }
            }
        }
        return result;
    }

    @Test
    public void TestIntDiff(){
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(1);
        integers.add(5);
        integers.add(6);
        integers.add(9);
        integers.add(16);
        integers.add(27);
        assert IntDiff(integers, 4) == 3;
        ArrayList<Integer> integers1 = new ArrayList<>();
        integers1.add(1);
        integers1.add(1);
        integers1.add(3);
        integers1.add(3);
        assert IntDiff(integers1, 2) ==4;

    }

def int_diff(nums: list, n: int) → int:
list1=
a=len(nums)
for i in range(0,a):
for j in range(0,a):
if nums[j]-nums[i]==n:
list1.append((nums[i],nums[j]))
c=len(list1)
return c

参考题解

import itertools

def int_diff(nums: list, n: int) -> list:

    pairs = list(itertools.combinations(nums, 2))
    valid = [(x, y) for x, y in pairs if abs(x - y) == n]

    return len(valid)

解题思路:

  1. 利用内置生成器函数combinations,得到nums任意2个元素配对的组合结果pairs
  2. 遍历pairs,统计两个数的差值刚好是n的配对的个数
def int_diff(nums: list, n: int) -> int:
    return sum([nums.count(num + n) for num in nums if (num + n ) in nums])

assert int_diff([1, 1, 5, 6, 9, 16, 27], 4) == 3
assert int_diff([1, 1, 3, 3], 2) == 4
1 个赞

老师,请问写成下面这样: 为什么符合要求的数据会被打印两遍呢?
def solution(nums: list,n : int)-> int :
m=0
#b=
for i in range(0, len(nums)-1):
for j in range(i,len(nums)):
if nums[j]-nums[i]==n:
#b.append(nums[i])
#b.append(nums[j])
print(nums[i],nums[j]) 为什么被打印了两遍呢

            m += 1
return m

if name == ‘main’:
a=[1, 1, 5, 6, 9, 16, 27]
n=4
solution(a,4)
print(solution(a, 4))

以下是运行结果:
1 5
1 5
5 9
1 5
1 5
5 9
3

    def get_num(nums:list, n:int)->int:
        sum1 = 0
        for i in nums:
            if i+n in nums[i:]:
                sum1+= nums.count(i+n)
        return sum1
def int_diff(nums: list, n: int) -> int:
    count = 0
    for i in range(len(nums)):
        for j in range(i+1,len(nums)):
            if abs(nums[j] - nums[i]) == n:
                count+=1
    return count
import itertools,math
def int_diff(nums:list,n:int) -> int:
    return len([list(itertools.combinations(nums,2))[i] for i in range(len(list(itertools.combinations(nums,2)))) if int(math.fabs(list(itertools.combinations(nums,2))[i][0]-list(itertools.combinations(nums,2))[i][1]))== n])

assert int_diff([1,1,5,6,9,16,27],4)==3
assert int_diff([1,1,3,3],2)==4




def int_diff(nums: list, n: int) -> int:
    count = 0
    # result = []
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if abs(nums[i] - nums[j]) == n:
                count += 1
                # result.append((nums[i],nums[j]))
    # print(result)
    return count


assert int_diff([1, 1, 5, 6, 9, 16, 27], 4) == 3
assert int_diff([1, 1, 3, 3], 2) == 4