刷题3:给定一个数组 nums,判断 nums 中是否存在三个下标 a,b,c数相加等于targe且a,b,c不相等


题目:

   

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,
下标 ,a ,b , c 对应数相加等于 targe 找出所有满足条件且不重复的三元组下标

解析:

   在一个list里面找出来三个数字使这三个数字相加等于目标targe,

   这里是一个list 我们去循环这里面的元素,我们利用for循环, 第一个取来,然后后剩下的元素分别取循环上一个循环剩下的元素。这样保证了不重复,最后验证下,如果找出来的数字的值满足a+b+c=targe ,且三个数不相等,我们认为查询正确。

那么我们看下python代码是如何实现的

def findthree(nums:list,targe:int):
    if len(nums)<3:
        return False
    result=[]
    for a in range(len(nums)):
        for b in range(len(nums))[a:]:
            for c in range(len(nums))[b:]:
                try:
                    if nums[a]+nums[b]+nums[c]==targe and a!=b and b!=c and a!=c :
                        result.append((a,b,c))
                except:
                    return False
    return result

  


那么我们看下测试代码
import  unittest
from findthree import findthree
class TestCae(unittest.TestCase):
    def setUp(self) -> None:
        pass
    def tearDown(self) -> None:
        pass
    def testone(self):
        reslt=findthree([],6)
        self.assertFalse(reslt)
    def testtwo(self):
        reslt = findthree([1], 6)
        self.assertFalse(reslt)
    def testthree(self):
        reslt = findthree([1,'2'], 6)
        self.assertFalse(reslt)
    def testFor(self):
        reslt = findthree([1,'2',"2"], 6)
        self.assertFalse(reslt)
    def testfive(self):
        reslt = findthree([1,2,3], 6)
        self.assertEqual(reslt,[(0,1,2)])
    def testsix(self):
        reslt = findthree([1,2,3,3], 6)
        self.assertEqual(reslt,[(0,1,2),(0,1,3)])
if __name__=="__main__":
    unittest.main()

  

看下运行结果:

看下最后代码的覆盖率

这样我们就测试完毕我们写的代码了。 那么我们认为目前测试用例覆盖了百分之百路径下面所有的​分支,认为代码没有bug,测试通过。

   接下来,我们看下java版本的实现

public class Fintrhee {
    public List<Map<String,Integer>> find(List<Integer> list, Integer targert){
        List<Map<String,Integer>> resultList=new ArrayList<>();
        if (list.size()<3){
            return null;
        }
        for (int a=0;a<list.size();a++ ){
            for(int b=a;b<list.size();b++){
                for(int c=b;c<list.size();c++){
                    if (list.get(a)+list.get(b)+list.get(c)==targert && !new Integer(a).equals(new Integer(b))&&!new Integer(a).equals(new Integer(c))&&!new Integer(c).equals(new Integer(b))){
                        Map<String,Integer> map=new HashMap<>();
                        map.put("a",a);
                        map.put("b",b);
                        map.put("c",c);
                        resultList.add(map);

                    }
                }
            }
        }
        return resultList;
    }

}

  

测试代码:
public class FintrheeTest {

    @Test
    public void testFind() {
        Fintrhee fintrhee=new Fintrhee();
        List<Integer> integerList=new ArrayList<>();
        integerList.add(0);
        List<Map<String,Integer>> maps=fintrhee.find(integerList,1);
        assertEquals(null,maps);
    }
    @Test
    public void test2Find() {
        Fintrhee fintrhee=new Fintrhee();
        List<Integer> integerList=new ArrayList<>();
        integerList.add(1);
        integerList.add(2);
        integerList.add(3);
        List<Map<String,Integer>> maps=fintrhee.find(integerList,1);
        List<Map<String,Integer>> mapList=new ArrayList<>();
        assertEquals(maps,mapList);
    }
    @Test
    public void test3Find() {
        Fintrhee fintrhee=new Fintrhee();
        List<Integer> integerList=new ArrayList<>();
        integerList.add(1);
        integerList.add(2);
        integerList.add(3);
        List<Map<String,Integer>> maps=fintrhee.find(integerList,6);
        List<Map<String,Integer>> mapList=new ArrayList<>();
        Map<String,Integer> map=new HashMap<>();
        map.put("a",0);
        map.put("b",1);
        map.put("c",2);
        mapList.add(map);
        assertEquals(maps,mapList);
    }
}

  

测试结果​:

​覆盖率:

​刷题还在继续,文章优化在下面公众号首发,

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。 面试题-python3 个包含n个整数的数组nums,判断nums中是否存在三个元素,a,b,c,使得a+b+c=0 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组 Leetcode练习(Python):数组类:第15题:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。 注意:答案中不可以包含重复的三元组。 python3面试题-一个包含n个整数的数组a,判断a中是否存在三个元素,a,b,c,使得a+b+c=0 给定一个整数数组 nums 和一个目标值 k,请实现一个方法判断 nums 中是否存在某个片段(即若干个相连元素)之和等于 k。要求时间复杂度为 O(n)。 算法题:三个数相加等于某个特定值 刷题:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 最接近的三数之和(给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数, 使得它们的和与 target 最接近。返回这三个数的和)
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM