Xiaohongshu Computer Vision Intern 2: Return a unique number in an array, requiring a space complexity of O(1)

I recently participated in the internship interview of Xiaohongshu. On the one hand, it was very simple. I asked 3 questions about the data structure. Then I asked about the competition and the project, and the answer was okay. Then I had 2 times yesterday, I asked about the game, and asked about it. Some of the basic knowledge of deep learning, the answer was okay, and finally such an algorithm question was asked, but I didn't get it, and I felt like it was over. . . Hey, I
just don't know the space complexity of pop and remove, but it is also a way of thinking. The interviewer told me to use the double pointer method. At that time, I was a little confused and didn't understand how to do it. . . .

def unique(alist):
    index=0
    while index<len(alist):
        count=0
        mid_value=alist[index]#用mid_value保存临时的可能需要删除的值,因为下面的pop只是删除了数组后面的相同的值
        for j in range(len(alist)-1,index,-1):#逆序遍历
            if alist[j] == mid_value:
                alist.pop(j)
                count+=1
        if count !=0:
            alist.remove(mid_value)
        else:
            index+=1#注意这里,是只有count数等于0时才进行index的前移,count为0证明数组里这个数只出现了一次
    return alist

Guess you like

Origin blog.csdn.net/AWhiteDongDong/article/details/111726867