力拓题目

两数之和

 

题目描述

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

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

# for i in nums:
#             s = nums.index(i)
#             nums_2 = nums[s+1:]
#             for j in nums_2:
#                 s2 = nums.index(j)
#                 if i + j == target:
#                     if s == s2:
#                         s2 = nums_2.index(j)+s+1
#                     return(s,s2)

反转字符串

两数之和

 

题目描述

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

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

# for i in nums:
#             s = nums.index(i)
#             nums_2 = nums[s+1:]
#             for j in nums_2:
#                 s2 = nums.index(j)
#                 if i + j == target:
#                     if s == s2:
#                         s2 = nums_2.index(j)+s+1
#                     return(s,s2)

反转字符串

 

 

 

编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。

不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。

你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。

class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s = ["h","e","l","l","o"] char = s.reverse

 

 

 

猜数字大小

 

题目描述

我们正在玩一个猜数字游戏。 游戏规则如下: 我从 1n 选择一个数字。 你需要猜我选择了哪个数字。 每次你猜错了,我会告诉你这个数字是大了还是小了。 你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-110):

    start = 1
  mx = n
while start <= mx:
mid = start + (mx - start) // 2
z = guess(mid)
if z == 1:
start = mid + 1
elif z == -1:
mx = mid -1
else:
return mid

 

猜你喜欢

转载自www.cnblogs.com/zrx19960128/p/10854751.html