Insist on brushing every week!
38. Appearance sequence
Given a positive integer n, prints the NTH term of the appearance sequence.
An “appearance sequence” is a sequence of integers, starting with the number 1, and each item in the sequence is a description of the previous item.
countAndSay(1) = “1”
CountAndSay (n) is a description of countAndSay(n-1), which is then converted to another numeric string. ,
I have been struggling with this problem for a long time. The first idea is to start with the first item 1, list all the items based on the input number N, and find the item returned.
var countAndSay = function(n) { let str = '1'; let arr = str.split(''); // Array of strings let list = ['1']; // let result = ''; let j; for(j = 0; j<n; J++){// let num = 1; // Table count result = "; for(let i = 0; i<arr.length; I++) {/ / n the appearance of the length of the array if this is a resume (arr [I + 1] && arr = = arr [I] [I + 1]) {num++; if(arr[i+2] && arr[i] == arr[i+2]) { continue; } }else if(arr[i] == arr[i-1]) { num = 1 continue; }else { num = 1 } result = result + num + arr[i]; if(i === arr.length-1) { } } arr = result.split('') list.push(result); } return list[j-1] };Copy the code
58. Length of the last word
You are given a string S, consisting of several words separated by Spaces. Returns the length of the last word in the string. If the last word does not exist, return 0.
First, it can be divided into pure blank string, blank end or word end. Use the re to match the string, and then return the length.
var lengthOfLastWord = function(s) { let arr = s.split('') let reg = null; let result = '' if(s.replace(/\s/g,'').length === 0) { return 0 } if(arr[arr.length-1] === ' ') { reg = /\w+\s+$/; let regResult = s.match(reg)[0] result = regResult.replace(/\s/g,'').length }else { reg = /\w+$/; result = s.match(reg)? s.match(reg)[0].length:0; } return result; };Copy the code
66. Plus one
Given a non-negative integer represented by a non-empty array of integers, add one to that number. The highest digit is stored at the beginning of an array, where each element stores only a single digit. You can assume that this integer does not start with zero, except for the integer 0.
Find if the array is 9 from the back, and then jump out of the loop when a non-9 number is added by one, or add one digit to the array header
var plusOne = function(digits) { let addFlg = false; for(let i = digits.length-1; i>=0; i--) { if(i === 0 && digits[i] === 9) { digits[i] = 0; addFlg = true }else if(digits[i] === 9) { digits[i] = 0; }else{ digits[i] = digits[i]+1; break; } } if(addFlg){ digits.unshift(1) } return digits };Copy the code
88. Merge two ordered arrays
Merge nums2 into nums1 to make nums1 an ordered array. Initialize nums1 and nums2 to m and n, respectively. You can assume that nums1 has a space size equal to m + n so that it has enough space to hold elements from Nums2.
Replace the array with m subscripts and then sort it
var merge = function(nums1, m, nums2, n) { let j = 0; for(let i = m; i<m+n; i++) { nums1.splice(i,1,nums2[j]); j++ } return nums1.sort((a,b) => a-b) };Copy the code