zoukankan      html  css  js  c++  java
  • python小题目

    1、Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
    #方法一:
     1 class Solution:
     2     def twoSum(self,nums,target):
     3         l = []
     4         count = len(nums)
     5         for i in range(0, count):
     6             for j in range(i + 1, count):
     7                 if nums[i] + nums[j] == target:
     8                     l.append(i)
     9                     l.append(j)
    10                     return l

    方法二:
     1 class Solution():
     2     def twoSum(self, nums, target):
     3         """
     4         :type nums: List[int]
     5         :type target: int
     6         :rtype: List[int]
     7         """
     8         new_list=[]
     9         sum_index=len(nums)
    10         if sum_index>1:
    11             for i in range(sum_index):
    12                 diff_num=target-nums[i]
    13                 if diff_num in nums[i+1:]:
    14                     if i not in new_list:
    15                         new_list=[i,nums[i+1:].index(diff_num)+i+1]
    16                         return new_list
    nums=[1,1,4,1,2,23,2]
    a=Solution()
    print(a.twoSum(nums,27))
    
    
    2、一个列表里面随意三个数字相加等于0,然后把这三个数字以列表的形式记录下来,不可以重复
     1 class Solution:
     2     def threeSum(self, nums):
     3         d1 = {}
     4         count = len(nums)
     5         for i in range(0, count):
     6             for j in range(i + 1, count):
     7                 for k in range(j + 1, count):
     8                     if nums[i] + nums[j] + nums[k] == 0:
     9                         l1 = (nums[i], nums[j], nums[k])
    10                         d1[tuple(sorted(l1))] = 1
    11 
    12         return [list(z) for z in d1]
    
    
    S = [-1,0,1,2,-1,-4]
    a = Solution()
    print(a.threeSum(S))

    3、按顺序依次取7个数相加,结果增加至新列表 data=[3,18,17,16,8,9,8,11,17,9,16,6,7,7,8,7,6,4,4,5,10,10]
     1 l=[]
     2 num=6
     3 index=0
     4 sun=0
     5 for i in data:
     6     sun+=i
     7     index+=1
     8     if index>num:
     9         l.append(sun)
    10         index =0
    11         sun=0
    12 print(l)
     
    4、将数字顺序反过来
    方法一:
     1 class Solution:
     2     def reverse(self, x):
     3         symbol='-'
     4         flag=False
     5         x=str(x)
     6         new_x=''
     7 
     8         #有'-'号的保留
     9         if symbol in x:
    10             x=x[1::]
    11             flag=True
    12 
    13         #从后往前读取字符串
    14         max_index=len(x)
    15         min_index=0
    16         for _ in range(max_index):
    17             if max_index>min_index:
    18                 new_x+=x[max_index-1]
    19                 max_index-=1
    20 
    21         x=new_x
    22         if flag:
    23             x='-%s' %(x)
    24 
    25         return int(x)

    方法二:
     1 class Solution(object):
     2     def reverse(self, x: int) -> int:
     3         self.x = x
     4         if '-' in str(self.x):
     5             strs =  str(self.x)[1:][::-1]
     6             for i in range(len(strs)):
     7                 if strs[i] != '0':
     8                     return  int(strs)
     9                 else:
    10                     strss = '-' + strs.lstrip('0')
    11                     return int(strss)
    12         else:
    13             strs =  str(self.x)[:][::-1]
    14             for i in range(len(strs)):
    15                 if strs[i] != '0':
    16                     return int(strs)
    17                 else:
    18                     return int(strs.lstrip('0'))
  • 相关阅读:
    Educational Codeforces Round 97 (Rated for Div. 2)
    Daizhenyang's Coin (sg函数)
    Just A String(KMP)
    Common Substrings POJ
    2020年HDU多校第六场 1010 Expectation(矩阵树)
    2020牛客暑期多校训练营(第八场)G题Game SET(折半搜索)
    矩阵分解在协同过滤推荐算法中的应用
    协同过滤推荐算法简述
    使用百度地图api可视化聚类结果
    信安实践——自建CA证书搭建https服务器
  • 原文地址:https://www.cnblogs.com/tianlinger/p/8658944.html
Copyright © 2011-2022 走看看