题目:

给定一个包含 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不相等的更多相关文章

  1. 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数

    今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...

  2. [java大数据面试] 2018年4月百度面试经过+三面算法题:给定一个数组,求和为定值的所有组合.

    给定一个数组,求和为定值的所有组合, 这道算法题在leetcode应该算是中等偏下难度, 对三到五年工作经验主要做业务开发的同学来说, 一般较难的也就是这种程度了. 简述经过: 不算hr面,总计四面, ...

  3. 给定一个数组,求如果排序之后,相邻两数的最大差值,要求时间复杂度为O(N),且要求不能用非基于比较的排序

    题目: 给定一个数组,求如果排序之后,相邻两数的最大差值,要求时间复杂度为O(N),且要求不能用非基于比较的排序 public static int maxGap(int nums[]) { if ( ...

  4. C#LeetCode刷题之#189-旋转数组(Rotate Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3700 访问. 给定一个数组,将数组中的元素向右移动 k 个位置, ...

  5. 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  6. 【IT笔试面试题整理】给定一个数组a[N]构造数组b [N]

    [来源]:腾讯2013实习生笔试   给定一个数组a[N],我们希望构造数组b [N],其中b[j]=a[0]*a[1]-a[N-1] / a[j])空间复杂度和O(n)的时间复杂度:除遍历计数器与a ...

  7. 算法:Manacher,给定一个字符串str,返回str中最长回文子串的长度。

    [题目] 给定一个字符串str,返回str中最长回文子串的长度 [举例] str="123", 1 str="abc1234321ab" 7 [暴力破解] 从左 ...

  8. 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 ...

  9. C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...

随机推荐

  1. writeAsBytes writeAsString

    import 'dart:io';import 'dart:convert'; main()async{ File a = File('C:\\aria2\\1.txt'); var c = read ...

  2. JavaScript实现网页回到顶部效果

    在浏览网页时,当我们浏览到网页底部,想要立刻回到网页顶部时,这时候一般网页会提供一个回到顶部的按钮来提升用户体验,以下代码实现了该功能 HTML代码: <p id="back-top& ...

  3. Java框架之MyBatis框架(一)

    一.框架介绍: MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创建sta ...

  4. JWT生成token及过期处理方案

    业务场景 在前后分离场景下,越来越多的项目使用token作为接口的安全机制,APP端或者WEB端(使用VUE.REACTJS等构建)使用token与后端接口交互,以达到安全的目的.本文结合stacko ...

  5. Redis:Linux安装与使用

    Redis的存储结构:字符类型,散列类型,列表类型,集合类型,有序集合. Redis功能: 可以为每个key设置超时时间. 可以通过列表类型来实现分布式队列的操作. 支持发布订阅的消息模式. 为什么使 ...

  6. C++中string的实现原理

    C++中string的实现原理 背景 当我刚开始学习C++,对C还是有一部分的了解,所以以C的思维去学C++,导致我很长一段时间的学习都处于一个懵逼的状态,C++的各种特性,标准库,模板还有版本的迭代 ...

  7. PHP编程实现阳历转换为阴历的方法

    php类: 2 /** 3 *PHP编程实现阳历转换为阴历的方法 4 *根据实际情况所需进行调用 5 * 6 / 7 10 <?php class Lunar { public $MIN_YEA ...

  8. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Relief 过滤式特征选择

    给定训练集{(x1,y1),(x2,y2).....(xm,ym)} ,对每个示例xi,Relief在xi的同类样本中寻找其最近邻xi,nh(猜中近邻),再从xi的异类样本中寻找其最近邻xi,nm(猜 ...

  10. windows 上运行 sslocal 提示 libcrypto(OpenSSL) not found 解决办法

    1.下载最新版ss客户端,使用pip安装的并不是最新版,去github下载最新版安装 2.安装openssl客户端 OpenSSL for Windows:https://wiki.openssl.o ...