zoukankan      html  css  js  c++  java
  • 刷题了刷题了

    准备每天刷一题,提高自己的算法能力,要坚持下去!!!!!!!!!

    2018年4月2号开始刷题:

    Two Sum

    • 第1题

    英文描述:Given an array of integers, return indices of the two numbers such that they add up to a specific

    target.You may assume that each input would have exactly one solution, and you may not use the same

    element twice.

    例子:

        Given nums = [2, 7, 11, 15], target = 9,
        Because nums[0] + nums[1] = 2 + 7 = 9,
        return [0, 1].

    题目描述: 找到一个数组中两个值得和等于固定值(target)返回这俩值得索引

    #sumtwo问题
    
    list1=[1,8,4,7]
    def sumtwo(list1,target):
        if len(list1)<=1:#如果目标值数量少于1,不符合要求
            return  False
        buff_dict={}#自己定义的一个字典放入的东西为{3:0}
        for i in range(len(list1)):
            if list1[i] in buff_dict:
                return  [buff_dict[list1[i]],i]#这里的i是放回的东西
            else:
                buff_dict[target-list1[i]]=i#这个数据结构是{3:0}
    print(sumtwo(list1,9))#结果是[0, 1]

     第二题:

    英文描述: Given a 32-bit signed integer, reverse digits of an integer.

    note: Assume we are dealing with an environment which could only hold integers within the 
    
    32-bit signed integer range. For the purpose of this problem, assume that your function
    
    returns 0 when the reversed integer overflows.
    

    例子:

    Input: 123  Output:  321 
    Input: -123   Output: -321
    Input: 120  Output: 21

    中文描述:返转一个正数,如果溢出返回0       就是如果大于32位,就返回0;

    #翻转数字  如果是负数还是负数;否则直接翻转数字
    def reverse2(x):
        str_x=str(abs(x))#首选把数字绝对值变成字符串
        print(str_x)#123
        num=int(str_x[::-1])#用切片的方式把数字翻转
        print(num)#321
        if num>2147483648:return  0
        if x<0: return  -num  #如果是本身是小于零,那就加上一个符号
        return num
    s=reverse2(-123)
    print(s)#-321

     第三题:

    题目】Determine whether an integer is a palindrome. Do this without extra space.

    【解答】Palindrome指的是回文,而这里需要找的是回文数,指的是1、121、34543这样从左往右看和从右往左看都相等的数。先找到数字总共有几位,然后判断高位和低位是否相等,相等是回文数。

    #判断一个整数是不是回文数,就是从左到右或者从右到左,都是同一个数字。这题的解法是把数字反转一半进行比较的
    def js(x):
        if(x<0):
            return  False
        elif(x!=0 and x%10==0):
            return  False
        reverse=0
        #需要把数字变成整数进行判断
        while(int(reverse) < int(x)):
            print(reverse,x)
            reverse=reverse*10+x%10
            x=x/10
        print(reverse,x)
        return (int(x)==int(reverse)) |(int(x)==int(reverse/10))
    print(js(3553))

    第四题:

    英文描述: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from

    1 to 3999.

    中文描述: 将罗马数字转成中文,其中假设罗马数字在1—3999中。

    罗马数字转成整数描述:

    罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000)。按照下面的规则可以表示任意正整数。
    重复数次:一个罗马数字重复几次,就表示这个数的几倍。 
    右加左减:在一个较大的罗马数字的右边记上一个较小的罗马数字,表示大数字加小数字。在一个较大的数字的左边记上一个较小的罗马数字,表示大数字减小数字。但是,左减不能跨越等级。比如,99不可以用IC表示,用XCIX表示。 
    加线乘千:在一个罗马数字的上方加上一条横线或者在右下方写M,表示将这个数字乘以1000,即是原数的1000倍。同理,如果上方有两条横线,即是原数的1000000倍。 
    单位限制:同样单位只能出现3次,如40不能表示为XXXX,而要表示为XL。
    def romnatoint(s):
        sum=0
        convert={'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1}
        for i in range(len(s)-1):
            if convert[s[i]]<convert[s[i+1]]:#如果前面的数据比后面的数据小那么,就需要用现在的求和数减去这个小的数
                sum=sum-convert[s[i]]
            else:
                sum=sum+convert[s[i]]#如果前面的数比后面的数据大,那么就加上前面的数
        return sum+convert[s[-1]]#直接加上最后一个数
    s=romnatoint('IIM')
    print(s)

     第五题

    英文描述:Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is

    valid.The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.

    中文描述:判断一个字符串标点是否合法

    第六题

    求连续子集和为k的个数

    class Solution(object):
    
        def subarraySum(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: int
            """
            sums = {0:1} # prefix sum array
            res = s = 0
            for n in nums:
                s += n # increment current sum
                res += sums.get(s - k, 0) # check if there is a prefix subarray we can take out to reach k
                sums[s] = sums.get(s, 0) + 1 # add current sum to sum count
            return res    
  • 相关阅读:
    YZR.Data 事务处理(Tranaction)
    啊Ran讲微信开发(.net) :公众号(服务号)+自定义服务器(OAuth授权登录)
    啊Ran讲微信开发(.net) 目录结构
    啊Ran讲微信开发(.net) :公众号(订阅号)+自定义服务器(自定义菜单)
    js和jQuery的总结
    Javascript重要解析
    IntelliJ-项目配置,解决no artifacts的warnings
    农夫过河问题(java版)
    redis安装常见问题
    idea 项目中 maven 编译出错Fatal error compiling: 无效的目标发行版: 1.8 -> [Help 1] 解决方法
  • 原文地址:https://www.cnblogs.com/1a2a/p/8689972.html
Copyright © 2011-2022 走看看