刷题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);
}
}
测试结果:
覆盖率:
刷题还在继续,文章优化在下面公众号首发,

刷题3:给定一个数组 nums,判断 nums 中是否存在三个下标 a,b,c数相加等于targe且a,b,c不相等的更多相关文章
- 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数
今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...
- [java大数据面试] 2018年4月百度面试经过+三面算法题:给定一个数组,求和为定值的所有组合.
给定一个数组,求和为定值的所有组合, 这道算法题在leetcode应该算是中等偏下难度, 对三到五年工作经验主要做业务开发的同学来说, 一般较难的也就是这种程度了. 简述经过: 不算hr面,总计四面, ...
- 给定一个数组,求如果排序之后,相邻两数的最大差值,要求时间复杂度为O(N),且要求不能用非基于比较的排序
题目: 给定一个数组,求如果排序之后,相邻两数的最大差值,要求时间复杂度为O(N),且要求不能用非基于比较的排序 public static int maxGap(int nums[]) { if ( ...
- C#LeetCode刷题之#189-旋转数组(Rotate Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3700 访问. 给定一个数组,将数组中的元素向右移动 k 个位置, ...
- 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...
- 【IT笔试面试题整理】给定一个数组a[N]构造数组b [N]
[来源]:腾讯2013实习生笔试 给定一个数组a[N],我们希望构造数组b [N],其中b[j]=a[0]*a[1]-a[N-1] / a[j])空间复杂度和O(n)的时间复杂度:除遍历计数器与a ...
- 算法:Manacher,给定一个字符串str,返回str中最长回文子串的长度。
[题目] 给定一个字符串str,返回str中最长回文子串的长度 [举例] str="123", 1 str="abc1234321ab" 7 [暴力破解] 从左 ...
- Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】
D. Black Hills golden jewels time limit per test 2 seconds memory limit per test 256 megabytes input ...
- C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...
随机推荐
- IDEA自动清理优化import包
IDEA自动清理优化import包 直接上图: Add unambiguous imports on the fly:快速添加明确的导入. Optimize imports on the fly:快速 ...
- ES5和ES6的继承
ES5继承 构造函数.原型和实例的关系:每一个构造函数都有一个原型对象,每一个原型对象都有一个指向构造函数的指针,而每一个实例都包含一个指向原型对象的内部指针, 原型链实现继承 基本思想:利用原型让一 ...
- Java框架之MyBatis框架(一)
一.框架介绍: MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创建sta ...
- css-博客样式初体验
css学习一周后,写了个基础博客样式. 样式是出来了,但是在写的过程中感觉css写的杂乱无章,可能是写的太少了吧,条例不是很清除,只是在写的过程 中一个点一个点的套,感觉样式出来即可,没有做到由全局出 ...
- 宝石JUELRYE单词JUELRYE珠宝
juelrye n.珠宝 late 14c., juelrye "precious ornaments, jewel juelrye (uncountable) Adornment with ...
- linux技能点三 find grep
find: 1. 按文件名查找 find . -name "a*.txt" 注意双引号: 2. 按文件大小查找 find .-size [+/-] ...
- Flink实战学习资料
这份资料我已经看了一些,感觉挺不错的,在这里分享一下,有需要的可以购买学习.
- vue 对 v-for 中数组进行过滤操作
之前写angularjs的时候,filter是可以直接在ng-repeat中使用.但是到了vue好像这个不起作用. 具体解决办法: 加一个计算属性: computed:{ filterData: fu ...
- 两种方式测试 GNS3 环境
GNS3已经部署好了,怎么测试环境呢?两种方式,一是使用自带的VPC连接交换机互联互通,二是配合VMware连接GNS3中的交换机互联互通. 自带 VPC 测试 使用两台VPC与一台二层交换机相连,测 ...
- kafka安装测试报错 could not be established. Broker may not be available.
修改 config 下配置文件 vim server.properties 配置本机ip listeners=PLAINTEXT://192.168.174.128:9092 执行命令时 bin/ka ...