Preface: a lot of people say, do algorithm problem is useless, actually use less than. I did not have a girlfriend before the algorithm problem, after doing the girlfriend has been snoring next to me, now know what to do. Hey hey. Come on!! Look at me looking like this can find a girlfriend, what are you waiting for!

001. The sum of two numbers

The title

Train of thought

Set up a map for storage, then start to iterate through the nums, using the elements as keys and the index as values, and store them in the map. When iterating over a map where a key (target-element) exists, return the value of the key and the index of the current element

code

var twoSum = function(nums, target) {
    // Set a map for storage
    const map = new Map(a)for(let i = 0; i < nums.length ; i++) {
        // Calculate the difference between target and the current element
        const chazhi = target - nums[i]
        if (map.has(chazhi)) {
            // If there is a key difference in the map, return it directly
            return [map.get(chazhi), i]
        } else {
            // If it does not exist, it will be stored in the map
            map.set(nums[i], i)
        }
    }
};
Copy the code




007. Integer reversal

The title

Train of thought

Take the absolute value of the number, then continue to take the remainder of 10 and put it in front of it. Finally, put the positive and negative into the judgment of the return value (note the interval).

code

var reverse = function(x) {
    // Determine whether the number is positive or negative
    const flag = x < 0 ? -1 : 1
    // old: take the absolute value of this number so that it can be calculated as a positive number
    let old = Math.abs(x)
    // new: used to store new numbers
    let now = 0
    while (old > 0) {
        // old takes mod 10 every time and adds now times 10
        now = now * 10 + old % 10
        // Then divide old by 10 and take floor to prepare for the next operation
        old = Math.floor(old / 10)}// Positive and negative operations
    now = flag * now
    // Determine whether the interval is exceeded
    return now < Math.pow(-2.31) || now > Math.pow(2.31) - 1 ? 0 : now
};
Copy the code




009. A palindrome

The title

Train of thought

Return false if the start and end Pointers are different, and true if they are all equal

code

var isPalindrome = function(x) {
    // First convert the number to a string for easy traversal
    let numStr = x.toString()
    // Set the first pointer
    let start = 0, end = numStr.length - 1
    while(start < end) {
        // Return false as soon as the first digit is detected
        if(numStr[start] ! == numStr[end])return false
        // Otherwise the double pointer moves to the center
        start++
        end--
    }
    // Return true if they are the same
    return true
};
Copy the code




014. Maximum common prefix

The title

Train of thought

Ideas:

code

var longestCommonPrefix = function (strs) {
    // Return an empty string if STRS length is 0
    if(! strs.length)return ' '
    // longPrefix: stores the longest prefix
    let longPrefix = ' '
    let i = 0
    // Iterate based on the first word
    while (strs[0][i]) {
        // Each letter of the word
        let first = strs[0][i]
        if (strs.some(item= >item[i] ! == first)) {// Iterates through STRS and returns the current longPrefix as long as the i-th letter of a word is different from the i-th letter of the first word
            return longPrefix
        } else {
            // Otherwise, longPrefix continues splicing
            longPrefix += first
        }
        i++
    }
    return longPrefix
};
Copy the code




020. Valid parentheses

The title

Train of thought

If the open bracket is a key and the close bracket is a value, return false. If the open bracket is a key and the close bracket is a value, return false

code

var isValid = function(s) {
    Return false if s is odd in length and cannot be closed
    if (s.length % 2! = =0) return false
    // Set a map store with open parenthesis as key and close parenthesis as value
    map.set('('.') ')
    map.set('['.'] ')
    map.set('{'.'} ')
    const map = new Map(a)// Set an array to traverse the stack
    const res = []
    for (let i = 0; i <= s.length; i++) {
        let item = s[i]
        if (map.has(s[i])) {
            // Open parenthesis, push directly into the array
            res.push(item)
        } else {
            // Return false if the close parenthesis is different from the preceding element
            if(map.get(res.pop()) ! == item) {return false}}}return true
};
Copy the code

Ah, everyone can pay attention to the body, do not learn too obsessed with oh, keep the body can be good after looking for a girlfriend, accompany girlfriend oh!