LeetCode solution (using JavaScript) [1]

(1) Rotate the string to the left [slice ()]

The left rotation operation of a string is to transfer several characters in front of the string to the end of the string. Please define a function to realize the function of string left rotation operation. For example, if you input the string "abcdefg" and the number 2, the function will return the result "cdefgab" obtained by rotating left by two bits.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>左旋转字符串</title>
<!--    字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
示例 1:

输入: s = "abcdefg", k = 2
输出: "cdefgab"
示例 2:

输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"
 
限制:1 <= k < s.length <= 10000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。-->
    <script>
        var reverseLeftWords = function(s, n) {
    
    
            return s.slice(n)+s.slice(0,n);
        };
        alert(reverseLeftWords("abcdef",2));
    </script>
<!--    arrayObject.slice(start,end) -->
</head>
<body>

</body>
</html>

Parsing
The key to the answer to this question is to use the slice method of the JS string.
The slice() method
slice() extracts a certain part of the string and returns the extracted part in a new string.
This method sets two parameters: start index (start position), end index (end position).
If an argument is negative, count from the end of the string.

This example crops the segment from position -12 to position -6 in the string:

example

var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);

The result of res is:

Banana

(2) Guess the number [loop]

Little A and Little B are playing guessing numbers. Little B randomly chooses one from 1, 2, 3 each time, and little A also chooses a guess from 1, 2, 3 each time. They played this game three times in total, please go back to Little A, how many times did you guess right?
The input guess array is the guess of Little A each time, and the answer array is the choice of Little B each time. The length of both guess and answer is equal to 3.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>猜数字</title>
<!--   
示例 1:
输入:guess = [1,2,3], answer = [1,2,3]
输出:3
解释:小A 每次都猜对了。

示例 2:
输入:guess = [2,2,3], answer = [3,2,1]
输出:1
解释:小A 只猜对了第二次。

限制:

guess的长度 = 3
answer的长度 = 3
guess的元素取值为 {
    
    1, 2, 3} 之一。
answer的元素取值为 {
    
    1, 2, 3} 之一。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/guess-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。-->
    <script>
        var game = function(guess, answer) {
    
    
            var j = 0;
            for(i=0;i<3;i++){
    
    
                if(guess[i]==answer[i])
                {
    
    
                    j++;
                }
            }
            return j;
        };
        alert(game([1,0,3],[1,2,3]));
    </script>
</head>
<body>

</body>
</html>

Analysis
There are two arrays of the same length in this question, compare how many of them are the same, and add one to the counter when they are the same.

(3) The difference between the product sums of integers [Math.floor]

Given an integer n, please help to calculate and return the difference between the integer "product of digits" and "sum of digits".

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--给你一个整数 n,请你帮忙计算并返回该整数「各位数字之积」与「各位数字之和」的差。
示例 1:

输入:n = 234
输出:15 
解释:
各位数之积 = 2 * 3 * 4 = 24 
各位数之和 = 2 + 3 + 4 = 9 
结果 = 24 - 9 = 15
示例 2:

输入:n = 4421
输出:21
解释: 
各位数之积 = 4 * 4 * 2 * 1 = 32 
各位数之和 = 4 + 4 + 2 + 1 = 11 
结果 = 32 - 11 = 21

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。-->
    <script>
        var subtractProductAndSum = function(n)
        {
    
    
            var mul =1;
            var add = 0;
                while(n>0){
    
    
                    num = n%10;
                    mul= num*mul;
                    add= num+add;
                    // n = n/10; 不能用这句 是因为这句的结果是有小数点的
                   /*
                   返回小于等于x的最大整数:
                   Math.floor(1.6);
                   以上实例将输出:1
                   */
                   n = Math.floor((n /= 10));
                }
                return mul-add;
        }
        alert(subtractProductAndSum(13));
    </script>
</head>
<body>

</body>
</html>

Parsing
The key to this question is the use of the Math.floor method
Math.floor()
Math.floor(x) returns the value x is rounded down to the nearest integer:

Example
Math.floor(2.7); // returns 2

(4) The number of operations to change the number to 0 [loop]

Given a non-negative integer num, please return the number of steps required to turn it into 0. If the current number is even, you need to divide it by 2; otherwise, subtract 1.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。

示例 1:

输入:num = 14
输出:6
解释:
步骤 1) 14 是偶数,除以 2 得到 7 。
步骤 27 是奇数,减 1 得到 6 。
步骤 36 是偶数,除以 2 得到 3 。
步骤 43 是奇数,减 1 得到 2 。
步骤 52 是偶数,除以 2 得到 1 。
步骤 61 是奇数,减 1 得到 0 。
示例 2:

输入:num = 8
输出:4
解释:
步骤 18 是偶数,除以 2 得到 4 。
步骤 24 是偶数,除以 2 得到 2 。
步骤 32 是偶数,除以 2 得到 1 。
步骤 41 是奇数,减 1 得到 0 。
示例 3:

输入:num = 123
输出:12
 
提示:

0 <= num <= 10^6

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。-->
    <script>
        var numberOfSteps  = function(num) {
    
    
            if(num==0){
    
    
                return 0;
            }
            else{
    
    
                for(i=0;num!=0;i++)
                {
    
    
                    if(num%2==0){
    
    
                    num = num/2;
                    }
                    else
                    {
    
    
                        num = num-1;
                    }
                }
                    return  i;
            }
        };
        alert(numberOfSteps(12));
    /*
除以2的次数就是二进制的位数,奇数的次数就是除最高位以外的1的个数。
例如:23 二进制即 10111,共5位,除最高位后面有3个1,所以总步数为8

大佬的代码:
var str = num.toString(2);
return str.length + str.replace(/0/g,"").length - 1;
//g 全局替换

作者:aleafworld
链接:https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/solution/wu-xu-xun-huan-2xing-dai-ma-gao-ding-by-aleafworld/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/
    </script>
</head>
<body>
</body>
</html>

Analysis
uses conditions and loop operations to calculate the operation of a number to zero. First, judge whether the number is zero. An operation is completed within a loop, so the number of loops is the number of operations.

(5) Gems and stones [indexOf]

Given a string J representing the type of gemstone in the stone, and a string S representing the stone you have. Each character in S represents a type of stone you own, and you want to know how many of the stones you own are gems.

Letters in J are not repeated, and all characters in J and S are letters. The letters are case sensitive, so "a" and "A" are different types of stones.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>宝石与石头</title>
    <!--示例 1:

输入: J = "aA", S = "aAAbbbb"
输出: 3
示例 2:

输入: J = "z", S = "ZZ"
输出: 0
注意:

SJ 最多含有50个字母。
 J 中的字符不重复。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jewels-and-stones
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。-->
    <script>
        var numJewelsInStones = function(J, S) {
    
    
            var n = 0;
            for(var i= 0;i<S.length;i++)
            {
    
    
                //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
                //如果没有找到匹配的字符串则返回 -1。
                if(J.indexOf(S[i])!=-1)
                {
    
    
                    n=n+1;
                }
            }
            return n;
        };
        alert(numJewelsInStones("a","axsx"));
    </script>
</head>
<body>
</body>
</html>

The parse
indexOf() method returns the position within the string of the first occurrence of a specified string value. Returns -1 if no matching string is found. So you only need to traverse the S string to see if there is S[i] in J, and add one as long as it exists, so that you can find several elements in J in S.

Guess you like

Origin blog.csdn.net/weixin_34727238/article/details/105891694