[LeetCode&Python] Problem 704. Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
Example 1:
Input:nums= [-1,0,3,5,9,12],target= 9
Output: 4
Explanation: 9 exists innumsand its index is 4
Example 2:
Input:nums= [-1,0,3,5,9,12],target= 2
Output: -1
Explanation: 2 does not exist innumsso return -1
Note:
- You may assume that all elements in
numsare unique. nwill be in the range[1, 10000].- The value of each element in
numswill be in the range[-9999, 9999].
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
n=len(nums)
low=0
high=n-1
mid=(low+high)//2 while low!=mid:
if nums[mid]==target:
return mid
elif nums[mid]>target:
high=mid
mid=(high+low)//2
elif nums[mid]<target:
low=mid
mid=(high+low)//2
if nums[mid]==target:
return mid
elif nums[high]==target:
return high
else:
return -1
[LeetCode&Python] Problem 704. Binary Search的更多相关文章
- [LeetCode&Python] Problem 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode&Python] Problem 401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode&Python] Problem 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- [LeetCode&Python] Problem 693. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [LeetCode&Python] Problem 868. Binary Gap
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- 【Leetcode_easy】704. Binary Search
problem 704. Binary Search solution: class Solution { public: int search(vector<int>& nums ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
随机推荐
- [record]WebLogic域之创建-文本界面
WebLogic域的创建方式主要有图形界面.文本方式(字符界面).脚本方式(WLST脚本)等,本文用文本方式来创建域. 一.启动Configuration Wizard •config.cmd/con ...
- 【还是回来了】博客搬家--https://cangbean.github.io
还是弄了个自己的地址:https://cangbean.github.io 想记录下,防止万一以后迁移不好弄 想练习markdown写作 折腾而已 以后不再博客园记录东西了,但是还是会回来看看的
- ng-packagr 不能全部打包文件
1.没有在public_api.ts中导出 export * from './src/app/ngprime/components/tooltip/tooltip.module'; export * ...
- Java读取txt文件和写入txt文件
package com.nickwong.code; import java.io.*; /** * Created by Nickwong on 31/07/2018. * 根据1-8楼的建议,优化 ...
- Firebug: 已拦截跨源请求:同源策略禁止读取位于XXX的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-
第一种,就是在被请求的程序中添加HTTP头,即CORS跨域(跨域资源共享,Cross-Origin Resource Sharing) 如: Response.Headers.Add("Ac ...
- 我的第一个Angular2应用
1需要具备的基本前端基础:HTML.CSS.JavaScript.为了实现对项目包的管理,推荐使用npm NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题:官网先下 ...
- JS 函数参数 及 函数数组
<script> function a(){ alert("a"); } function b(){ alert("b"); } var arr = ...
- 洛谷P1073 最优贸易
题面要求的是一个差值,即走过一条路径能找到的路径上最大值-最小值. 那么相当于跑一遍最长路和一遍最短路,当然不是概念上的最长路最短路,这里把dis[v]的松弛改成用路径上传递来的最大/最小值维护,而不 ...
- 电脑小白和ta的小白电脑——MySQL数据库
数据库我选择了MySQL,因为据说MySQL是最流行的关系型数据库管理系统,在WEB应用方面 MySQL 是最好的RDBMS之一了,而且,免费呀! MySQL数据库开发环境的配置 (一)下载MySQL ...
- css书写规范以及如何写出赏心悦目的代码
css书写规范: 1. 编码统一为utf-8;2. 协作开发及分工: i根据各个模块, 同时根据页面相似程序, 事先写好大体框架文件,同时根据页面相似程序,事先写好大体框架文件.共用css文件base ...