题目:
给定一个包含 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版本的实现
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;
}
}
测试代码:
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);
}
}
测试结果:
覆盖率: