当前位置:网站首页>Negative number +0+ positive number
Negative number +0+ positive number
2022-06-11 01:38:00 【sueong】
A pile of negative numbers + a pile 0+ A pile of positive numbers
Find the subscript of the last negative number and the first positive number Subscript from 1 Start
Sure enough, it didn't work. I can't see what went wrong 555
def findnum(nums):
res = []
for i in range(len(nums)):
# If it's all 0 The border
if nums[0] == 0 and nums[-1] == 0:
return 0, 0
# Record 0 The subscript
elif nums[i] == 0:
# Because the numbers are subscript 1 Start
res.append(i + 1)
elif nums[i] > 0:# Find a positive number and jump out of the loop
break
print(res)
return res[0] - 1, res[-1] + 1
nums = [-3, -4, -1, 0, 0, 0, 0, 1, 2, 3, 6]
print(findnum(nums))
nums = [0, 0, 0, 0, 1, 2, 3, 6]
print(findnum(nums))
nums = [0, 0, 0, 0]
print(findnum(nums))
Two points Find the first one 0 And the last 0 The location of Then each of them is the subscript they are looking for
class Solution():
def find_left(self, nums):
l = 0
r = len(nums) - 1
while l <= r:
mid = (l + r) // 2
if nums[mid] == 0:
r = mid - 1
elif nums[mid] > 0:
r = mid - 1
else:
l = mid + 1
# first 0,l>right = mid -1 l=mid
# Subscript from 1 Start
return l + 1
def find_right(self, nums):
l = 0
r = len(nums) - 1
while l <= r:
mid = (l + r) // 2
if nums[mid] == 0:
l = mid + 1
elif nums[mid] < 0:
l = mid + 1
else:
r = mid - 1
# the last one 0,l = mid+1 r=mid
# Subscript from 1 Start
return r + 1
def find_num(self, nums):
if not nums or nums[0] == 0 and nums[-1] == 0:
return 0, 0
ll = self.find_left(nums)
rr = self.find_right(nums)
return ll - 1, rr + 1
nums = [-3, -4, -1, 0, 0, 0, 0, 1, 2, 3, 6]
s = Solution()
print(s.find_num(nums))
nums = [0, 0, 0, 0, 1, 2, 3, 6]
print(s.find_num(nums))
nums = [0, 0, 0, 0]
print(s.find_num(nums))
边栏推荐
- IRS application release 15: application security self test guide
- Middleware_ Redis_ 05_ Persistence of redis
- Multi interest recall model practice | acquisition technology
- 2022年高考加油 那年我的高考如此兴奋
- MultipartFile和File互转工具类
- 中国专利奖奖金多少,补贴100万
- Yanrong looks at how to realize the optimal storage solution of data Lake in a hybrid cloud environment
- Brief description of custom annotations
- Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
- 深圳市南山区专精特新企业申报条件,补贴10-50万
猜你喜欢

记录打包GoogleChrome浏览器插件

Conda安装Pytorch后numpy出现问题

項目_基於網絡爬蟲的疫情數據可視化分析

node和express实现mySql模糊搜索

项目_基于网络爬虫的疫情数据可视化分析

1.3 ROS 无人机简介
![[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design](/img/63/3193186820215b9babc3d00e1ef20b.jpg)
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design

Yunna PDA wireless fixed assets inventory management system

PX4装机教程(六)垂起固定翼(倾转)

2.0、ROS与PX4通信详解
随机推荐
记录打包GoogleChrome浏览器插件
Projet Visualisation et analyse des données sur les épidémies basées sur le Web crawler
Introduction to the application process of Shenzhen China Patent Award, with a subsidy of 1million yuan
Role of handlermethodargumentresolver + use case
threejs:两点坐标绘制贝赛尔曲线遇到的坑
Beijing Mentougou District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
[Li mu] how to read papers [intensive reading of papers]
項目_基於網絡爬蟲的疫情數據可視化分析
Yunna provincial administrative unit fixed assets management system
How to write this with data and proc without SQL
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
Current limiting and download interface request number control
云呐|省级行政单位固定资产管理系统
数字ic设计自学ing
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
2.1 ros+px4 simulation - Fixed Point flight control
Web3 ecological decentralized financial platform sealem Finance
What is the C-end and what is the b-end? Let me tell you
Simple select sort and heap sort
Uninstall mavros