当前位置:网站首页>Interview algorithm questions
Interview algorithm questions
2022-07-22 07:06:00 【Secret fool】
One 、 Here's an unsorted array of integers nums , Please find the smallest positive integer that doesn't appear in it
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
m = 1
n = len(nums)
if n == 0:
return m
nums.sort()
for i in nums:
if i == m:
m += 1
return m
Two 、 How to use rand7 Generate rand10
def rand_10():
r1 = rand_7()
r2 = rand_7()
r = (r1 - 1) * 7 + r2
a = True
while a:
if 1 <= r <= 40:
num = (r-1)%10 + 1
a = False
return num
3、 ... and 、 Given an array of non negative integers , Your task is to count the number of triples that can form three sides of a triangle .
The condition for forming a triangle is that the sum of the two small sides is greater than the third side , Determine the idea after clarifying the conditions :
- Sort the given array , Make the small side on the left , The big side is on the right
- from n u m s [ 2 ] nums[2] nums[2] Start traversing array , i i i Point to the largest edge of this traversal range
- Position the left and right pointers , The left pointer initially points to n u m s [ 0 ] nums[0] nums[0], The right pointer initially points to n u m s [ i − 1 ] nums[i - 1] nums[i−1]
- If left + Right <= Dabian , l + 1 l + 1 l+1
- If left + Right > Dabian , be [ l , r ) [l ,r) [l,r) Take any number in the range as n u m s [ l ] nums[l] nums[l], Can achieve n u m s [ l ] + n u m s [ r ] > n u m s [ i ] nums[l] + nums[r] > nums[i] nums[l]+nums[r]>nums[i],res += r - l
class Solution:
def triangleNumber(self, nums: List[int]) -> int:
n = len(nums)
nums.sort()
num = 0
if n == 3:
if nums[0] + nums[1] > nums[2]:
return 1
else:
return 0
else:
for i in range(2, n):
l, r = 0, i-1
while l < r:
if nums[l] + nums[r] > nums[i]:
num += r - l
r -= 1
else:
l += 1
return num
Four 、 The closest sum of three
Given an include n Array of integers nums and A target value target. find nums Three integers in , Make their sum and target Nearest . Returns the sum of the three numbers . Assume that there is only one answer for each group of input .
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
n = len(nums)
nums.sort()
res = float('inf')
for i in range(n):
if i > 0 and nums[i] == nums[i-1]:
continue
else:
l, r = i+1, n-1
while l < r:
cur_sum = nums[i] + nums[l] + nums[r]
if cur_sum == target:
return target
elif cur_sum < target:
l += 1
else:
r -= 1
if abs(cur_sum - target) < abs(res - target):
res = cur_sum
return res
边栏推荐
- 输入一个URL到这个页面呈现出来,这个过程发生了什么?
- [MySQL and database] MySQL & database Chapter 11: process control structure
- 东方广益深夜辟谣,称入股锤子被立案调查消息不实!
- Container to container & container to host - interconnected via SSH protocol (multi node, cross host)
- 正则表达式
- Matlab GUI编程技巧(八):uitoolbar在图窗中创建工具栏
- Matlab GUI编程技巧(九):详解 uitable 函数显示表格及相关操作(创建表用户界面)
- After uniapp sets tabbar jump, other pages jump to the home page (sorting)
- 敬伟PS教程:基础篇A
- Foundation of Mathematics: Jensen inequality
猜你喜欢
随机推荐
Mysql基础命令
PS抠出logo
MODIS数据wget下载
启动服务常用命令
重写与重载
模板方法模式
EventLoop事件循环机制
[MySQL and database] MySQL & database Chapter 9: learning stored procedures
闻泰科技一季度营收同比猛增184.6%,净利润暴涨256.21%!
Educational Codeforces Round 100 (Rated for Div. 2) B. Find The Array 题解
Love running every day [noip2016 T4]
Matlab GUI编程技巧(十一):axes/geoaxes/polaraxes绘图创建 GUI 坐标区
请问开户对年龄有限制么?请问,手机开户股票开户安全吗?
C# out 关键字error CS0136 无法在此范围中声明该局部变量或参数,因为该名称在封闭局部范围中用于定义局部变量或参数
Strategy mode
IO model, multiplexing
零碎知识——统计相关
零碎知识——sql相关
跨服器,插入,查寻
Indexes