首页 > 其他 > 详细

lintcode入门篇十三

时间:2020-03-20 01:24:27      阅读:87      评论:0      收藏:0      [点我收藏+]

646. 第一个独特字符位置

中文English

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

样例

样例 1:

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

样例 2:

输入 : s = "lovelintcode"
输出 : 2
class Solution:
    """
    @param s: a string
    @return: its 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

 

lintcode入门篇十三

原文:https://www.cnblogs.com/yunxintryyoubest/p/12528602.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!