在数组中查找元素的第一个和最后一个位置
2023-03-15 22:52:44 时间
题目:
给定一个的整数数组 nums,
和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
题目解析:
1.给定一个数组,确定的是一个数组, 数组是整数,那么我们可以知道,那么target的也是整数。
2.要求target的在数组中开始位置和结束位置,我们可以先找出来target的在list里面的下标位置,把这些下标位置放到list里面,我们去取list里面的第一个元素和最后一个元素,就是对应的开始位置和结束位置。
那么我们就可以上手去实现我们的代码了。
从这期开始,我们的代码将用python 和java两个版本去实现,同时从两方面去提高我们的,同时 也面向了两门语言的学习者。
首先,我们先看python篇:
def find(nums:list,target:int): listone=[] for i in range(len(nums)): if nums[i]==target: listone.append(i) if len(listone)==0: return False return listone[0],listone[-1]
测试:
class Testcase(unittest.TestCase): def setUp(self) -> None: pass def tearDown(self) -> None: pass def testone(self): result=find([1,2,3,4],5) self.assertFalse(result) def testtwo(self): result=find([1,2,3,4],1) self.assertEqual(result,(0,0)) def testthree(self): result=find([1,2,3,4,1],1) self.assertEqual(result, (0, 4)) def testfour(self): result = find([1, 2, 3, 4, 1], "1") self.assertEqual(result, False) def testfive(self): result = find(["1", 2, 3, 4, 1], 1) self.assertEqual(result, (4, 4)) def testsix(self): result = find([ 1], 1) self.assertEqual(result, (0, 0)) if __name__=="__main__": unittest.main()
测试结果:
我们可以看到目前是没有发现问题的。这样,python版本实现完毕,
接下来我们去看看,对应的java版本是怎么实现的。
实现代码:
public class Find { public Map<String,Integer> findby(List<Integer> list, Integer targert){ List<Integer> integerList=new ArrayList<>(); for (int i=0;i<list.size();i++){ if(list.get(i).equals(targert)){ integerList.add(i); } } Map<String,Integer> map=new HashMap<>(); if (integerList.size()==0){ map.put("first",null); return map; }else { map.put("first",integerList.get(0)); map.put("last",integerList.get(integerList.size()-1)); return map; } } }
测试代码:
public class FindTest { @org.testng.annotations.Test public void testFindby() { List<Integer> integerList=new ArrayList<>(); integerList.add(0); Find find=new Find(); Map<String,Integer>map=find.findby(integerList,1); assertEquals(map.get("first"),null); } @org.testng.annotations.Test public void testFindby1() { List<Integer> integerList=new ArrayList<>(); integerList.add(0); Find find=new Find(); Map<String,Integer>map=find.findby(integerList,0); assertEquals(map.get("first"),new Integer(0)); assertEquals(map.get("first"),new Integer(0)); } @org.testng.annotations.Test public void testFindby2() { List<Integer> integerList=new ArrayList<>(); integerList.add(0); integerList.add(0); Find find=new Find(); Map<String,Integer>map=find.findby(integerList,0); assertEquals(map.get("last"),new Integer(1)); assertEquals(map.get("first"),new Integer(0)); } @org.testng.annotations.Test public void testFindby3() { List<Integer> integerList=new ArrayList<>(); integerList.add(0); integerList.add(0); integerList.add(0); Find find=new Find(); Map<String,Integer>map=find.findby(integerList,0); assertEquals(map.get("last"),new Integer(2)); assertEquals(map.get("first"),new Integer(0)); } @org.testng.annotations.Test public void testFindby4() { List<Integer> integerList=new ArrayList<>(); integerList.add(0); integerList.add(1); integerList.add(0); Find find=new Find(); Map<String,Integer>map=find.findby(integerList,0); assertEquals(map.get("last"),new Integer(2)); assertEquals(map.get("first"),new Integer(0)); } }
测试结果:增加了代码覆盖率,
覆盖率
那么我们测试完毕,根据测试覆盖率来说,我们目前的测试是已经完成了覆盖了百分之百的路径和代码。
后续会陆续给大家分享更多的题目,更多的代码,大家一起成长,一起刷题。雷子说测试,带给你不一样的体验。力争所有的代码都做到100%的覆盖率,所有代码都进行单测。
所有文章优先在公众号推送。
相关文章
- 极简操作!3分钟即可实现的 IIS 站点迁移
- 超越Cookie:当今的客户端数据存储技术
- Hadoop与 Spark - 选择正确的大数据框架
- Linux性能测试工具有什么好用的工具,这里推荐一款
- 如何将Hadoop集成到工作流程中?这6个优秀实践必看
- 太快太不安全:为Mongo Express Web管理界面保驾护航
- 怎样在Kubernetes上运行PostgreSQL
- NoSQL中负载均衡系统如何解决热点问题,提高可用性?
- 服务器部署前端&Node 项目(包括阿里云服务器、nginx 以及 mongoDB 的配置)
- 日均5亿查询量的京东到家订单中心,为什么舍MySQL用ES?
- 如何在MySQL 8中重置root密码
- 浅谈SQL Server内部运行机制
- MySQL8.0里GROUP BY有变化,注意了
- 删库跑路?这篇文章教你如何使用xtraback备份MySQL数据库
- 你的数据仓库还在为企业业务拖后腿吗?
- Linux服务器Redis漏洞被利用挖矿解决方法
- 使用Kafka和MongoDB进行Go异步处理
- 收藏备用,MySQL 8下忘密码后重置密码的办法(MySQL5老方法不灵了)
- 数据库不适合Docker及容器化的7大原因
- MariaDB 10.3首推系统版本表,误删数据不用跑路了!