zoukankan      html  css  js  c++  java
  • 刷题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);
        }
    }
    

      

    测试结果​:

    ​覆盖率:

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

  • 相关阅读:
    iptables
    linux时间同步
    iftop使用
    linux目录结构及定时任务
    awk基本用法
    二、Java面向对象(6)_深入变量
    二、Java面向对象(5)_static修饰符
    二、Java面向对象(4)_构造函数
    二、Java面向对象(3)_类和对象
    二、Java面向对象(2)_软件开发方式
  • 原文地址:https://www.cnblogs.com/leiziv5/p/11758222.html
Copyright © 2011-2022 走看看