zoukankan      html  css  js  c++  java
  • leetcode 两数之和 整数反转 回文数 罗马数字转整数

    1.两数之和

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

    你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

    class Solution:
        def twoSum(self, nums: List[int], target: int) -> List[int]:
            dict_dog = dict()
            dict_pig = dict()
            for i, j in enumerate(nums):
                dict_pig[j] = i
            for m, n in enumerate(nums):
                dict_dog[m] = n
            for x, y in dict_dog.items():
                ttt = target - y
                w = dict_pig.get(ttt)
                if ttt in dict_dog.values():
                    if x != w and w is not None:
                        if x < w:
                            return x,w
                else:
                    continue
    

    这个题目Nick老师讲过一次,但是我觉得我的想法不一样,我就按照我自己的想法写了这一题。(想了一下午,毕竟学习的还不够)

    2.整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

    class Solution:
        def reverse(self, x: int) -> int:
            str_x = str(x)
            list_x = []
            y = ""
            if str_x[0:1] == "0":
                return 0
            elif str_x[0:1] == "-":
                for i in range(len(str_x) - 1):
                    list_x.append(str_x[i + 1])
                list_x.reverse()
                for j in list_x:  # type:str
                    y = y + j
                    x = "-" + y
                x = int(x)
                if x < (2 ** 31 - 1) and x > ((-2) ** 31):
                    return x
                else:
                    return 0
            else:
                for i in range(len(str_x)):
                    list_x.append(str_x[i])
                list_x.reverse()
                for j in list_x:  # type:str
                    y = y + j
                y = int(y)
                if y < (2 ** 31 - 1) and y > ((-2) ** 31):
                    return y
                else:
                    return 0
    

    3.回文数

    判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

    class Solution:
        def isPalindrome(self, x: int) -> bool:
                list_x = []
                list_re = []
                if x < 0:
                    return False
                else:
                    str_x = str(x)
                    list_x.extend(str_x)
                    list_re.extend(str_x)
                    list_x.reverse()
                    if list_x == list_re:
                        return True
                    else:
                        return False
    

    4.罗马数字转整数

    罗马数字包含以下七种字符: IVXLCDM

    字符 数值

    I 1
    V 5
    X 10
    L 50
    C 100
    D 500
    M 1000
    例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。

    通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

    class Solution:
        def romanToInt(self, s: str) -> int:
                dic_roman = {'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, 'XL': 40, 'L': 50, 'XC': 90, 'C': 100, 'CD': 400, 'D': 500,
                         'CM': 900, 'M': 1000}
                list_s = list()
                list_ag = list()
                count = 0
                for i in s:
                    list_s.append(i)
                for t in range(len(list_s) - 1):
                    tm = list_s[t] + list_s[t + 1]
                    if tm in dic_roman:
                        list_ag.append(tm)
                for j in list_ag:
                    count += dic_roman[j]
                    s = s.replace(j, "")
                for q in s:
                    count += dic_roman[q]
                return count
    

    总结一下,这些是我这几天写的题目中的一部分,感觉大部分是考虑的比较多,因为测试用例多了,然后就是自己对一些函数以及方法的应用很欠缺。再有就是每次写完去浏览别人的代码的时候发现,别人只写了几行,而我写的基本上都是他的五倍左右的代码量。

  • 相关阅读:
    POJ 2411 Mondriaan's Dream -- 状压DP
    codeforces 792A-D
    codeforces 796A-D
    Acdream1201 SuSu's Power
    HDU 2818 Building Block
    C# NetStream
    基于Duff's Device的C简易无栈协程实现
    CentOS 多版本 GCC 共存
    2017杭电多校第一场
    2019杭电多校第十场
  • 原文地址:https://www.cnblogs.com/xiongchao0823/p/11252932.html
Copyright © 2011-2022 走看看