当前位置:网站首页>负数+0+正数
负数+0+正数
2022-06-11 00:23:00 【sueong】
一堆负数+一堆0+一堆正数
找最后一个出现的负数和第一个出现的正数的下标 下标从1开始
果然没运行看不出哪里错了 555
def findnum(nums):
res = []
for i in range(len(nums)):
# 如果全是0 边界
if nums[0] == 0 and nums[-1] == 0:
return 0, 0
# 记录0的下标
elif nums[i] == 0:
# 因为数字从下标1开始
res.append(i + 1)
elif nums[i] > 0:# 找到正数跳出循环
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))
二分 找第一个0和最后一个0的位置 然后他们前面各一位就是要找的下标
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
# 第一个0,l>right = mid -1 l=mid
# 下标从1开始
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
# 最后一个0,l = mid+1 r=mid
# 下标从1开始
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))
边栏推荐
- 多兴趣召回模型实践|得物技术
- Introduction to the application process of Shenzhen China Patent Award, with a subsidy of 1million yuan
- Direct insert sort and shell sort
- ava. Lang.noclassdeffounderror: org/apache/velocity/context/context solution
- Yunna Qingyuan fixed assets management and barcode inventory system
- Introduction to the application process of China Patent Award, with a subsidy of 1million yuan
- Inventory management and strategy mode
- Beijing Tongzhou District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
- The file "setup" does not exist. What should I do?
- Support standard for cultivation of high-tech enterprises in Changping District, Beijing, with a subsidy of 100000 yuan
猜你喜欢

Why is the digital transformation of small and medium-sized enterprises so difficult?

Pd虚拟机安装系统提示 “网络初始化失败 操作失败 ”的解决方案

Yanrong looks at how to realize the optimal storage solution of data Lake in a hybrid cloud environment

網絡基礎(1)-----認識網絡

多兴趣召回模型实践|得物技术

记录打包GoogleChrome浏览器插件

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

Application of object storage S3 in distributed file system

用data和proc怎么写出这个,不用sql

The file "setup" does not exist. What should I do?
随机推荐
百万级访问量—高并发问题的解决历程
Clean up the broken artifacts data (.lastUpdated files) and reload the project.问题解决办法
2022年高考加油 那年我的高考如此兴奋
中间件_Redis_05_Redis的持久化
87. (leaflet house) leaflet military plotting - straight arrow modification
Web3生态去中心化金融平台——Sealem Finance
Basic introduction of graph and depth first traversal and breadth first traversal
Bad RequestThis combination of host and port requires TLS.
SSH远程登陆配置sshd_config文件详解
Oracle relational tables with XY field values are synchronized to PG database and converted to geometric fields
Bubble sort and quick sort
Time dependent - format, operation, comparison, conversion
table_ exists_ Action=append and table_ exists_ action=truncate
Viewpager and dot of bottom wireless loop
How to write this with data and proc without SQL
项目_基于网络爬虫的疫情数据可视化分析
云呐|庆远固定资产管理及条码盘点系统
Introduction to support standards for specialized, special and new manufacturing enterprises in Chaoyang District, Beijing, with a subsidy of 1million yuan
After a/b machine is connected normally, B machine suddenly restarts. Ask a what is the TCP status at this time? How to eliminate this state in the server program?
IRS应用发布之十五:应用安全自测指南