前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >刷题3:给定一个数组 nums,判断 nums 中是否存在三个下标 a,b,c数相加等于targe且a,b,c不相等

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

作者头像
雷子
发布2021-03-15 15:21:41
5520
发布2021-03-15 15:21:41
举报
文章被收录于专栏:雷子说测试开发

题目:

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

解析:

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

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

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

代码语言:javascript
复制
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

那么我们看下测试代码

代码语言:javascript
复制
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版本的实现

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
    }

}

测试代码:

代码语言:javascript
复制
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);
    }
}

测试结果:

覆盖率:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 雷子说测试开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档