Together we write algorithms 05

Usually empty when a solution algorithm topic, (LeetCode provide a thousand multi-channel algorithm topic) can not only enhance logical thinking ability, you can also consolidate JavaScript basics. Subject to unified algorithm can be used to solve multiple languages, the idea is the same solution, we are learning the front, so just write javascript solution here.

1. The sum of two numbers

  1. Title Description

    /*
    给定一个整数数组 nums 和一个目标值 target,
    请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
    
    你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
    
    示例:
    
    给定 nums = [2, 7, 11, 15], target = 9
    
    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]
    
     */
    
  2. Answers and analysis

    Analysis of ideas:

    Identify and target worth two index number in the array num given. Important thought is still through the array elements.

    First cycle reading digital array num [i], again through the array elements, followed num [j], so that the num [i] + NUM [J] ==target, i.e. in the num [i] ==when target-num [j], returns the indices i and j elements (elements returns an array of i and j).

    Problem-solving code is as follows:

    function twosum(nums,target){
    	for(let i=0; i<nums.length; i++){
    		for(let j=1; j<nums.length;j++){
    			if (nums[i]===target-nums[j]) {
    				return [i,j]
    			}
    		}
    	}
    }
    console.log(twosum([2,7,11,15],9))
    

    Test Results:

    Question 1 results

2. palindrome numbers

  1. Title Description

    /*
    判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
    
    示例 1:
    输入: 121
    输出: true
    
    示例 2:
    输入: -121
    输出: false
    解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
    
    示例 3:
    输入: 10
    输出: false
    解释: 从右向左读, 为 01 。因此它不是一个回文数。
    */
    
  2. Answers and analysis

    Analytical thinking:

    Palindrome number is positive with backwards read and read all the same numbers, the same palindrome string, the first character of the string and the last character of the same, the second character and penultimate character ( length-1-i位) the same, and so on .

    method 1

    Array read cycle, the array elements by reverse sequentially assigned to a temporary variable, and then determining the positive sequence corresponding to the temporary variable element is equal, if not a palindrome number not equal, if both are equal, indicating a palindrome.

    First use a String object to an integer array into a string, and then split using a method to convert the string to a string array.

    split() A method for dividing a character string into a string array.

    grammar:

    stringObject.split(separator,howmany)

    code show as below:

    function palindrome(num){
        var arr= String(num).split('')
        for(let i=0;i<arr.length;i++){
            const temp = arr[arr.length-1-i]
            if(temp!==arr[i]){
                return false
            }
        }
        
        return true
    }
     //调用函数
     console.log(palindrome(101030101))
    

    Method 2

    When this method is a method of optimization, taking half the length of the array elements, the number of comparisons can be reduced. code show as below:

    function palindrome2(num){
        const arr = String(num).split('')
        for(let i=0;i<arr.length/2;i++){
            if(arr[i]!==arr[arr.length-i-1]){
                return false
            }
        }
        return true
    }
    // 调用函数
    console.log(palindrome2(101030101))
    

    Method 3

    This is the most simple method, a method using javascript, to use the split dividing element array, and then reversed using the reverse method of array elements, the join method to use a combination of the last elements of the array into a string. Then determines whether combined into a new string is equal to the original string. Realize functions require only one line of code. To facilitate understanding, here it is divided into two rows.

    Farmington usage grammar
    reverse() For the array elements in the reverse order arrayObject.reverse()
    join() For all the elements in the array into a string, the elements are separated by the specified delimiters (not split). arrayObject.join(separator)
    split() A string into a string array stringObject.split(separator,howmany)

    as follows:

    function palindrome3(num){
        const arr = String(num).split('')
        return String(num) == arr.reverse().join('')
    }
     console.log(palindrome3(101030101))
    

    Test results of the three methods:

    Topic 2 Test Results

800 go like the wind, and did not ask the return date

Published 111 original articles · won praise 120 · views 90000 +

Guess you like

Origin blog.csdn.net/w1418899532/article/details/94895629