在数组中查找元素的第一个和最后一个位置

孤街醉人 提交于 2019-12-02 18:09:31

题目:

给定一个的整数数组 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%的覆盖率,​所有代码都进行单测。​

     所有文章优先在公众号推送。

    

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!