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

题目:

给定一个的整数数组 nums,

和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

 

题目解析:

    1.给定一个数组,确定的是一个数组, 数组是整数,那么我们可以知道,那么target的也是整数。

    2.要求target的在数组中开始位置和结束位置,我们可以先找出来target的在list里面的下标位置,把这些下标位置放到list里面,我们去取list里面的第一个元素和最后一个元素,就是对应的开始位置和结束位置。

 

  那么我们就可以上手去实现我们的代码了。

  从这期开始,我们的代码将用python 和java两个版本去实现,同时从两方面去提高我们的,同时 也面向了两门语言的学习者。

   首先,我们先看python篇:

     

1
2
3
4
5
6
7
8
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]

  

测试:

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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版本是怎么实现的。

 实现代码:

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
        }
    }
 
 
}

  

测试代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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%的覆盖率,​所有代码都进行单测。​

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

    

 

posted @   北漂的雷子  阅读(2170)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示