首页 > 试题广场 >

首个重复字符

[编程题]首个重复字符
  • 热度指数:42620 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

对于一个字符串,请设计一个高效算法,找到第一次重复出现的字符。

给定一个字符串(不一定全为字母)A及它的长度n。请返回第一个重复出现的字符。保证字符串中有重复字符,字符串的长度小于等于500。

测试样例:
"qywyer23tdd",11
返回:y
推荐
classFirstRepeat {
public:
    charfindFirstRepeat(string A, intn) {
        bool times[256] = {0};
        if(A.size()==0|| n==0)
            return0;
        for(inti=0;i<n;i++) {
            if(!times[A[i]])
                times[A[i]] = 1;
            else
                returnA[i];
        }
    }
};
利用hash的方式,把每个字符是否出现记录到一个数组中,初始化时都没出现,遍历字符串,将对应字符的位置置1,表示出现了,如果在某个字符位置上已经为1了,表示前面出现过该字符,那么这个字符就是第一个重复出现的字符,返回即可
编辑于 2015-11-26 00:48:53 回复(28)
class FirstRepeat:
    def findFirstRepeat(self, A, n):
        for i in range(1,n):
            if A[:i].find(A[i]) != -1:
                return A[i]
发表于 2018-10-20 11:15:00 回复(0)
class FirstRepeat:
    def findFirstRepeat(self, A, n):
        tmp=''#保存已出现过的字符
        for i in A:
            if i in tmp:#在之前已出现过
                return i
            else:
                tmp=tmp+i

发表于 2018-07-12 15:47:04 回复(0)
# -*- coding:utf-8 -*-

class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        string = ''
        for i in range(int(n)):
            if string.find(A[i]):
                print A[i]
            else:
                string.join(A[i])



为何报错 expected a character buffer object  

发表于 2017-09-24 16:34:15 回复(0)

python解法

def findFirstRepeat(self, A, n):
        chars=[]
        for i in A:
            if i not in chars:
                chars.append(i)
            else:
                return i
编辑于 2017-09-12 10:35:29 回复(2)
class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        temp = ""
        for s in A:
            if s not in temp:
                temp = temp + s
            else:
                break
        return s

发表于 2017-07-20 11:22:50 回复(0)
# -*- coding:utf-8 -*-

class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        x=set()
        for i in range(n):
            if A[i] in x:
                return A[i]
            x.add(A[i])

发表于 2017-05-11 18:50:01 回复(0)
class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        checkset=set()
        for i in xrange(0,n):
            tmplen=len(checkset)
            checkset.add(A[i])
            if len(checkset)==tmplen:
                return A[i]
发表于 2017-04-24 16:10:40 回复(0)
class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        y=[]
        for i in A:
            if i not in y:
                y.append(i)
            else :
                return i
        	

编辑于 2017-04-19 15:03:00 回复(0)
lass FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        re = []
        for i in A:
            if i in re:
                return i
            else:
            re.append(i)

发表于 2017-04-02 16:21:15 回复(0)
class FirstRepeat:
    def findFirstRepeat(self, A, n):
        for i in range(1,n):
            a=A[i]
            B=A[:i]
            if B.find(a)!=int(-1):
                return a
            
发表于 2017-03-01 20:14:28 回复(0)
class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        table = {}
        for i in A:
            if not i in table:
                table[i] = 0
            else:
                return i
                break

发表于 2016-12-25 22:40:20 回复(0)
# python
# -*- coding:utf-8 -*- class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        R = set()
        x = 1
        for i in A:
            R.add(i)
            if len(R) != x:
                return i
            x += 1
            
        return None 

发表于 2016-09-09 00:00:10 回复(0)
# -*- coding:utf-8 -*-

class FirstRepeat:
    def findFirstRepeat(self, A, n):
        # write code here
        flag = set()
        for i in A:
            if i in flag:
                return i
            else:
                flag.add(i)

发表于 2016-08-30 09:03:13 回复(0)

 
编辑于 2016-08-07 15:40:32 回复(0)