lintcode入门篇十三

646. 第一个独特字符位置

中文English

给出一个字符串。找到字符串中第一个不重复的字符然后返回它的下标。如果不存在这样的字符,返回 -1

样例

样例 1:

输入 : s = "lintcode"
输出 : 0

样例 2:

输入 : s = "lovelintcode"
输出 : 2
class Solution:
    """
    @param s: a string
    @return: it's index
    """
    def firstUniqChar(self, s):
        # write your code here
        dic = {}
        for i in s:
            dic[i] = dic.get(i,0) + 1
        for key,value in dic.items():
            if value == 1:
                return s.index(key)
        return -1

644. 镜像数字

中文English

一个镜像数字是指一个数字旋转180度以后和原来一样(倒着看)。例如,数字"69","88",和"818"都是镜像数字。
写下一个函数来判断是否这个数字是镜像的。数字用字符串来表示。

样例

样例1:

输入 : "69" 
输出 : true

样例 2:

输入 : "68" 
输出 : false
class Solution:
    '''
    大致思路:
    1.如果是镜像数字的话,那么需要符合镜像字典,{'0':'0','1':'1','6':'9','9':'6','8':'8'}
    '''
    def isStrobogrammatic(self,num):
        dic = {'0':'0','1':'1','6':'9','9':'6','8':'8'}
        j = len(num)
        for i in num:
            if i not in dic:
                return False
            j -= 1
            if dic[i] != num[j]:
                return False
        return True

 

posted @ 2020-03-20 00:26  风不再来  阅读(141)  评论(0编辑  收藏  举报