Coder Social home page Coder Social logo

画解算法 704-二分查找 about blog HOT 2 CLOSED

guanpengchn avatar guanpengchn commented on August 14, 2024
画解算法 704-二分查找

from blog.

Comments (2)

Adnios avatar Adnios commented on August 14, 2024

打卡C++,二分查找
class Solution { public: int search(vector<int>& nums, int target) { int left=0; int right=nums.size()-1; while(left<=right){ int mid=(left+right)/2; if(nums[mid]==target){ return mid; } else if(nums[mid]<target){ left=mid+1; } else{ right=mid-1; } } return -1; } };

from blog.

dljgs1 avatar dljgs1 commented on August 14, 2024

python:
Runtime: 36 ms, faster than 99.76% of Python3 online submissions for Binary Search.
Memory Usage: 14.1 MB, less than 40.08% of Python3 online submissions for Binary Search.

class Solution:
def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)
while left<right:
mid = int((left+right)/2)
if nums[mid] == target:
return mid
if nums[mid] < target:
left = mid+1
else:
right = mid
return -1

from blog.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.