Source: making power button (LeetCode) | | to brush the topic card for stars ✨ | give a ❤ ️ attention, ❤ ️ thumb up, ❤ ️ encourages the author

[Opened] Task 1: Brush the question punch card * 10

Nezha’s life creed: If you like what you learn, there will be strong motivation to support it.

Learn programming every day, so that you can get a step away from your dream. Thank you for not living up to every programmer who loves programming. No matter how strange the knowledge point is, work with me to calm down the wandering heart and keep going. Welcome to follow me vx:xiaoda0423, welcome to like, favorites and comments

Time: March 1 ~ March 13

  • Force buckle (LeetCode)- Sum of two numbers, valid parentheses, sum of two numbers | brush questions punch card – March 1
  • LeetCode – Merges two ordered lists, removes duplicates in sorted arrays,JavaScript Notes | Flash cards – March 2
  • Force Button (LeetCode)- Maximum suborder sum,JavaScript Data Structures and Algorithms (Arrays) | Brush questions punched -3 March
  • Say something about CSS | Tech Review – March 4
  • Force Buckle (LeetCode)- Stack, parenthesis generation | brush questions punch card – 5 March
  • It wasn’t that hard! Vue mall development | technical review – on March 6
  • Force buckle (LeetCode)- Plus one, queue | brush questions punch – 7 March
  • JavaScript data structure of the chain table | technical review – March 8
  • JavaScript Data Structures – Collections | Technical Review – March 9
  • LeetCode – Merge two ordered arrays, dictionary, hash table | swipe card – March 10
  • Force Buckle (LeetCode)- Symmetric binary Tree, Tree | Brush title punch card – March 11
  • Force buckle (LeetCode)-104. Maximum depth of binary tree, figure | brush problem clocking – March 12

preface

If this article is helpful to you, give ❤️ a follow, ❤️ like, ❤️ encourage the author, accept the challenge? Article public account launch, concern programmer Doraemon first time access to the latest article

❤ ️ cartridge ❤ ️ ~

28. Achieve strStr ()

1. Title Description

Implement the strStr() function.

Given a Haystack string and a Needle string, find the first position in the Haystack string where the Needle string appears (starting from 0). If none exists, -1 is returned.

Haystack = "hello", needle = "ll" Output: 2 Input: haystack = "aaAAA ", needle = "bba" Output: -1Copy the code

Description:

What value should we return when needle is an empty string? This is a good question to ask in an interview.

For this case, we should return 0 when needle is an empty string. This is consistent with the C language STRSTR () and the Java definition of indexOf().

Second, train of thought analysis

Returns 0 when two strings are equal

If haystack is an empty string, -1 is returned

Returns 0 if needle is an empty string

Needle length is greater than Haystack return -1

Loop through haystack, I records its pointer. Make equal judgment with needle first, when same, start loop needle, J records its needle. Res records the I pointer for equality, the result.

I and j plus one, keep going.

If they’re all equal afterwards, there’s no inequality in between. (Hello, ll) So when j and Needle are equal, we jump out of the loop.

If there is an intermediate discrepancy, then I returns the next position in the RES record and continues the loop, res and j reset. Continue comparing noodle’s first character.

Repeat the above steps. If j and len are equal, a match is made and the loop is broken. I is equal to the haystack length, that is, after the haystack loop is completed, j is not equal to Len, then it is not found, res is reset, and the loop is broken.

Answer code

var strStr = function(haystack, needle) {
    if(haystack===needle) return 0
    if(haystack==="") return -1
    if(needle==="") return 0
    if(needle.length>haystack.length) return -1
    var j=0,i=0,res=-1

    while(i>=0){
        var c=haystack[i]
        var len=needle.length
        if(j===len) break
        if(i==haystack.length){
            if(j<len){
                res=-1
            }
            break
        }

        if(c===needle[j]){
            j++
            if(res===-1) res=i
            i++
        }else{
            if(res>-1){
                i=res+1
                j=0
                res=-1
            }else{
                i++
            }
            
        }
    }
  
    return res
};
Copy the code

Four,

  1. Implement strStr() solution

Go back to my previous articles and you may get more!

  • A qualified junior front-end engineer needs to master module notes
  • Vue.js pen test questions Solve common business problems
  • [Primary] Personally share notes of Vue front-end development tutorial
  • A long summary of JavaScript to consolidate the front end foundation
  • ES6 comprehensive summary
  • Dada front-end personal Web share 92 JavaScript interview questions with additional answers
  • [Illustrated, like collection oh!] Re-study to reinforce your Vuejs knowledge
  • 【 Mind Mapping 】 Front-end development – consolidate your JavaScript knowledge
  • 14 – even liver 7 nights, summed up the computer network knowledge point! (66 items in total)
  • This was my first JavaScript primer
  • LocalStorage and sessionStorage localStorage
  • Drag and drop in HTML5
  • Challenge the front-end HTTP/ECMAScript
  • Must learn must learn – Audio and video
  • 170 Interview questions + answer Learn to organize (conscience making)
  • Front-end HTML5 interviewers and candidates ask and answer questions
  • Ne Zha is sweeping the sea
  • Tencent location service development applications
  • [Advanced] The interviewer asked me how Chrome renders (6000 words)
  • The interviewer started by asking me about Chrome’s underlying principles and HTTP protocol (swastika)
  • Staying up late summed up the “HTML5 Canvas”
  • This /call/apply/bind
  • The HTTP/HTTPS/HTTP2 / DNS/TCP/classic problem
  • Execute context/scope chain/closure/first-class citizen
  • Web page creation basics
  • Learn the summary of HTML5 finger front-end (suggested collection, illustrated)

❤️ follow + like + favorites + comments + forward ❤️, original is not easy, encourage the author to create better articles

Likes, favorites and comments

I’m Jeskson, thanks for your talent: likes, favorites and comments, and we’ll see you next time! ☞ Thank you for learning with me.

See you next time!

This article is constantly updated. You can search “Programmer Doraemon” on wechat to read it for the first time, and reply [information] there are materials of first-line big factories prepared by me, which have been included in this article www.dadaqianduan.cn/#/

Star: github.com/webVueBlog/…

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign