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

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

题目:


给定一个的整数数组 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()


测试结果:


image.png


我们可以看到目前是没有发现问题的。这样,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));
    }
}


测试结果:增加了代码覆盖率


image.png


覆盖率


image.png


那么我们测试完毕,根据测试覆盖率来说,我们目前的测试是已经完成了覆盖了百分之百的路径和代码。

相关文章
|
2月前
|
存储 算法 C语言
找到数组位置
找到数组位置
|
2月前
在排序数组中查找元素的第一个和最后一个位置
在排序数组中查找元素的第一个和最后一个位置
|
2月前
|
算法
leetcode-34:在排序数组中查找元素的第一个和最后一个位置
leetcode-34:在排序数组中查找元素的第一个和最后一个位置
21 0
|
9月前
|
算法
【算法专题突破】二分查找 - 在排序数组中查找元素的第一个和最后一个位置(17)
【算法专题突破】二分查找 - 在排序数组中查找元素的第一个和最后一个位置(17)
35 0
|
11月前
|
算法
LeetCode-34 在排序数组中查找元素的第一个和最后一个位置
LeetCode-34 在排序数组中查找元素的第一个和最后一个位置
|
12月前
|
算法 C语言 C++
【二分查找】34. 在排序数组中查找元素的第一个和最后一个位置
二分查找是一种高效的查找算法,其时间复杂度为 O(log n)。在许多情况下,我们需要在一个有序数组中找到某个目标值的搜索范围。本文将介绍一种基于二分查找的搜索范围查找算法,该算法能够快速找到目标值在数组中的起始和结束位置。
55 0
|
算法 安全 Swift
LeetCode - #34 在排序数组中查找元素的第一个和最后一个位置(Top 100)
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
|
算法
每日一题—— 在排序数组中查找元素的第一个和最后一个位置
每日一题—— 在排序数组中查找元素的第一个和最后一个位置
|
算法
力扣34题. 在排序数组中查找元素的第一个和最后一个位置
力扣34题. 在排序数组中查找元素的第一个和最后一个位置
73 0
|
算法
LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
93 0
LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置