在數組中查找元素的第一個和最后一個位置


題目:

給定一個的整數數組 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%的覆蓋率,​所有代碼都進行單測。​

     所有文章優先在公眾號推送。

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM