• Leetcode No.167 two sum II – input array is sorted (c + + implementation)


    1. Title 1.1 English title Given an array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Return the indices of the two numbers (1-indexed) as an integer array answer of size 2, where 1 <= answer[0] < answer[1] <= numbers.length. […]

  • Algorithm | dichotomy


    In data structure books, the precondition of order is often added when introducing dichotomy. The definition of order is either from large to small or from small to large. So, is order really a necessary condition for using dichotomy in solving all problems?The answer is: No As long as the elimination logic on the left […]

  • Python’s case of finding the square root by dichotomy


    I’ll stop talking nonsense. Let’s look at the code directly~ ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def sq2(x,e):   e = E error range   low= 0   high = max(x,1.0)Treatment greater than0less than1Number of   guess = (low + high) / 2.0   ctr = 1      while […]

  • Interview handwritten code list


    Anti chattering function const debounce = (fn, wait) => { let _ = ” return (…arg) => { clearTimeout(_) _ = setTimeout(() => { fn.apply(this, arg) }, wait) } } input.addEventListener(“input”, debounce(ajax, 1000)); Throttling function const throttle = (fn, s) => { let a = +new Date(), _ return (…arg) => { let b = +new Date() – a if (b > s) { _ = setTimeout(() => { fn(arg); _ = ” }, s) a = +new Date() } } } input.addEventListener(“input”, throttle(ajax, 1000)); Deep copy (solve circular reference) function clone(target, map = new WeakMap()) { if (typeof target === ‘object’) { let cloneTarget = Array.isArray(target) ? [] : […]

  • Display component and container component (by Dan Abramov, developer of Redux)


    This article is translated fromPresentational and Container ComponentsThe author of the article isDan Abramov, he is also the author of Redux and create react app.In the actual development process of using react + Redux technology stack, a very good understanding of the concept of container component and display component is the basis of developing an […]

  • [dichotomy algorithm] PHP implementation of binary search algorithm


    $a = array(1,2,33,24,15,16,7,8); function find2($a,$findValue){ /** * [url= home.php?mod=space&uid=170990 ]@Name [/ url] dichotomy search algorithm * [url= home.php?mod=space&uid=686208 ]@Author [/ url] * [url= home.php?mod=space&uid=952169 ]@Param [/ url] $a: array passed, $findvlaue: value to find * [url= home.php?mod=space&uid=155549 ]@Return [/ url] true found, false not found */ sort($a); $heigh = count((array)$a)-1; $low = 0; while ($heigh>=$low) […]

  • Leetcode 35 search insertion location


    Leedcode35 search insertion location The main idea of solving the problem is that the target array is ordered array, and the efficiency of searching by dichotomy is higher //Title: //Given a sort array and a target value, find the target value in the array, And returns its index. If the target value does not exist […]

  • The longest common prefix


    Address: https://leetcode-cn.com/probl… Title Description Analysis topic The most intuitive feeling about this problem is: First string foundtarget Perform a horizontal scan by column My code is as follows: /** * @param {string[]} strs * @return {string} */ var longestCommonPrefix = function(strs) { if (!strs || !strs.length) { return ” } let target = strs[0] for […]

  • Binary search (C language)


    Dichotomy is an efficient search method, which is suitable forIt’s in orderArray Basic thinking Search and judge from the middle number of the array. If it is not the number to be searched, compare the size. Then search and judge from the middle on one side of the two sides separated from the middle, and […]

  • How to find out whether an ordered array contains a value in PHP (binary search)


    Problem: for an ordered array, how to judge whether a given value exists in the array.   Idea: the simplest way to judge whether there is an array is to directly loop the array and compare each value. But for an ordered array, this writing does not make good use of the “ordered” feature. So […]