169 competition

And N unique integer zero

N an integer you, please return any array of a different composition of the integer number n, and the n is the number 0 and the sum.

 

Example 1:

Input: n = 5
Output: [--7, -1,1,3,4]
Explanation: These arrays is correct [-5, -1,1,2,3], [- 3-1,2, 2,4].
Example 2:

Input: n = 3
Output: [- 1,0,1]
Example 3:

Input: n = 1
Output: [0]
 

prompt:

1 <= n <= 1000

 

/**
 * @param {number} n
 * @return {number[]}
 */
var sumZero = function(n) {
    let res = [];
    let start = 0;
    let p=0;
    while(n>1){
        start+=1;
        res.push(start, -start);
        n-=2;
    }
    if(n===1){
       res.push(0); 
    }
    return res;
};
/**
 * @param {number} n
 * @return {number[]}
 */
var sumZero = function(n) {
    const result = [];
    if (n % 2 === 1) result.push(0);
    for (let i = 1; result.length < n; i++) {
        result.push(i);
        result.push(-i);
    }
    return result;
};

This writing is written, but the first one I wrote above, that is a little funny, it's clear what you can cycle, I guess it is out of circulation did not think the judgment conditions result.length <n, so the whole of the while loop ,Okay.

All elements of two binary search tree

You root1 and root2 which two binary search tree.

Please return a list containing all integers sorted in ascending order of two trees. .

 

Example 1:

 

Input: root1 = [2,1,4], root2 = [1,0,3]
Output: [0,1,1,2,3,4]
Example 2:

Input: root1 = [0, -10,10] , root2 = [5,1,7,0,2]
Output: [- 10,0,0,1,2,5,7,10]
Example 3:

Input: root1 = [], root2 = [5,1,7,0,2]
Output: [0,1,2,5,7]
Example 4:

Input: root1 = [0, -10,10] , root2 = []
Output: [- 10,0,10]
Example 5:

 

Input: root1 = [1, null, 8], root2 = [8,1]
Output: [1,1,8,8]
 

prompt:

Each tree has a maximum of 5000 nodes.
Value for each node between [-10 ^ 5 ^ 5 10].

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {number[]}
 */
const dfs = (root, arr)=>{
    if(!root) return;
    arr.push(root.val)
    if(root.left){
        dfs(root.left, arr)
    }
    if(root.right){
        dfs(root.right, arr)
    }
}
const toList = (arr)=>{
    // let s = null;
    // let p = null;
    // s = p;
    // arr.forEach(a=>{
    //     let newObj = {
    //         val: a,
    //         next: null
    //     }
    //     p = newObj;
    //     p = p.next
    // })
    //  return s;
    let s = null;
    let p = null;
    arr.forEach((a, i)=>{
        let newObj = {
            val: a,
            next: null
        } 
        // P = newobj; 
        IF (I === 0 ) { 
            P = newobj; 
            S = P 
        } 
        p.next = newobj; 
        P = p.next 
    }) 
     return S; 
    
} 
var getAllElements = function (the root1, root2) { 
    the let ARR = []; 
    DFS (the root1, ARR); 
    DFS (root2, ARR); 
    // arr.sort () 0 is not the default sort of 
    arr.sort ((A, B) => A- B)
    return ARR; 
};


Explanation: There are problems described this problem, saying that list, I think the list. Turn the layer list, beginning not turn right, turn the previous codes before turn right. Lead to take some time. And this sort function, I used the default sort (), this is not enough, two more figures appear elements does not work, it will be sorted according to the size of the same bits.

 

 

Jumper III

There is a non-negative integer array arr, you start at the beginning of the most marked at the start of the array. When you are located at the index i, you can skip i + arr [i] or i - arr [i].

Please judge whether they can jump to the corresponding elements of the value 0 at any index.

Note that no matter what the circumstances, you can not jump outside of the array.

 

Example 1:

Input: arr = [4,2,3,0,3,1,2], start = 5
Output: true
interpretation:
arrive index value 0 are the following three possibilities:
subscript 5 -> subscript 4 - > subscript 1 -> 3 subscript
subscript 5 -> subscript 6 -> subscript 4 -> subscript 1 -> subscript 3
example 2:

Input: arr = [4,2,3,0,3,1,2], start = 0
Output: true
interpretation:
arrive index value 0 are the following three possibilities:
subscript 0 -> subscript 4 - > subscript 1 -> subscript 3
example 3:

Input: arr = [3,0,2,1,2], start = 2
Output: false
interpretation: 1 can not reach the index value of zero.
 

prompt:

1 <= arr.length <= 5 * 10^4
0 <= arr[i] < arr.length
0 <= start < arr.length

/**
 * @param {number[]} arr
 * @param {number} start
 * @return {boolean}
 */
var canReach = function(arr, start) {
   let flag = false;
    let getZero = (arr, start, map=new Map())=>{
        if(arr[start] === 0){
            flag = true;
            return;
        }
        if(map.has(start)) return;
        map.set(start, 1);
        let plus = start+arr[start];
        let minus = start-arr[start];
        let len = arr.length;
        if(plus>=0 && plus <len ){
            // map.set(plus, 1);
            getZero(arr, plus, map)
            // map.set(plus, 1);
        }
        if( minus >= 0 && minus < len) {
             // map.set(minus, 1)
            getZero(arr, minus, map);
            // map.set(minus, 1)
        }
    }
    getZero(arr, start);
    return flag
};

This occurs when the duplicate index start, several places in the whole wrong judgment on where noted above, or is a stack overflow, or is only recursive once withdrew. Finally an idea into the correct position.

/**
 * @param {number[]} arr
 * @param {number} start
 * @return {boolean}
 */
var canReach = function(arr, start) {
    let l = 0;
    const r = [ start ];
    const a = new Array(arr.length).fill(0);
    a[start] = 1;
    while (l < r.length) {
        if (!arr[r[l]]) return true;
        if (r[l] - arr[r[l]] >= 0 && !a[r[l] - arr[r[l]]]) {
            r.push(r[l] - arr[r[l]]);
            a[r[l] - arr[r[l]]] = 1;
        }
        
        if (r[l] + arr[r[l]] < arr.length && !a[r[l] + arr[r[l]]]) {
            r.push(r[l] + arr[r[l]]);
            a[r[l] + arr[r[l]]] = 1;
        }
        l++;
    }
    return false;
};
/**
 * @param {number[]} arr
 * @param {number} start
 * @return {boolean}
 */
var canReach = function(arr, start) {
    if (arr[start] === 0) {
        return true
    }
    const n = arr.length
    let a = [start]
    let s = new Set()
    let i = 0
    while (i < a.length) {
        const x = a[i++]
        const w = x - arr[x]
        if (w >= 0 && !s.has(w)) {
            if (arr[w] === 0) {
                return true
            }
            s.add(w)
            a.push(w)
        }
        const y = x + arr[x]
        if (y < n && !s.has(y)) {
            if (arr[y] === 0) {
                return true
            }
            s.add(y)
            a.push(y)
        }
    }
    return false
};

 

I count problem

 

Give you an equation, with the left  words said that with the right  result representation.

Whether you need to check the following rules solvable equation:

  • Each character will be decoded into a digit (0--9).
  • Each pair of different characters must be mapped to different numbers.
  • Each  words[i] and  result will not be decoded into a number of leading zeros.
  • Left digital sum ( words) is equal to the right number result( ). 

If the equation is solvable, returns  True, otherwise it returns  False.

 

Example 1:

输入:words = ["SEND","MORE"], result = "MONEY"
输出:true
解释:映射 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2'
所以 "SEND" + "MORE" = "MONEY" ,  9567 + 1085 = 10652

Example 2:

输入:words = ["SIX","SEVEN","SEVEN"], result = "TWENTY"
输出:true
解释:映射 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4
所以 "SIX" + "SEVEN" + "SEVEN" = "TWENTY" ,  650 + 68782 + 68782 = 138214

Example 3:

输入:words = ["THIS","IS","TOO"], result = "FUNNY"
输出:true

Example 4:

Input: words = [ "LEET", "CODE"], result = "POINT" 
Output: false

 

prompt:

  • 2 <= words.length <= 5
  • 1 <= words[i].length, results.length <= 7
  • words[i], result Contains only uppercase letters
  • The number of different characters used in the expression for the maximum 10
/**
 * @param {string[]} words
 * @param {string} result
 * @return {boolean}
 */
var isSolvable = function(words, result) {
    let map = new Map();
    let arr = [0,1,2,3,4,5,6,7,8,9];
    let arr1 = [1,2,3,4,5,6,7,8,9];
    words.forEach((a)=>{
        let arr = a.split('');
        arr.forEach((b, i)=>{
            if(i===0){
               if(!map.has(b)){
                map.set(b, arr1)
                } 
            } else {
                if(!map.has(b)){
                map.set(b, arr)
                } 
            }
            
        })
    })
    result.split('').forEach((b, i)=>{
        if(i===0){
               if(!map.has(b)){
                map.set(b, arr1)
                } 
            } else {
                if(!map.has(b)){
                map.set(b, arr)
                } 
            } 
    })
    
    words.forEach((a) => { 
        The let ARR = a.split ( '' ); 
        arr.forEach ((B, I) => { 
            as map.get (B) .forEach (C => {
                   // Here only variable is fitted circulating level sets, no solution, or fight string   
            }) 
            
        }) 
    }) 
    
    
};
/**
 * @param {string[]} words
 * @param {string} result
 * @return {boolean}
 */
var isSolvable = function(words, result) {
    const a = {};
    const l = [];
    const min = [];
    for (let i = 0; i < words.length; i++) {
        for (let j = 0; j < words[i].length; j++) {
            if (!a[words[i][j]]) {
                l.push(words[i][j]);
                min.push(0);
                a[words[i][j]] = l.length;
            }
            if (!j) min[a[words[i][j]] - 1] = 1;
        }
    }
    for (let i = 0; i < result.length; i++) {
        if (!a[result[i]]) {
            l.push(result[i]);
            min.push(0);
            a[result[i]] = l.length;
        }
        if (!i) min[a[result[i]] - 1] = 1;
    }
    const getL = new Array(10).fill(0);
    const v = new Array(l.length).fill(0);
    var dfs = function(d) {
        if (d === l.length) {
            let sum1 = 0;
            for (let i = 0; i < words.length; i++) {
                let sum = 0;
                for (let j = 0; j < words[i].length; j++) {
                    const ttt = v[a[words[i][j]] - 1];
                    if (!j && !ttt) return false;
                    sum = sum * 10 + ttt;
                }
                sum1 += sum;
            }
            let sum2 = 0;
            for (let i = 0; i < result.length; i++) {
                const ttt = v[a[result[i]] - 1];
                if (!i && !ttt) return false;
                sum2 = sum2 * 10 + ttt;
            }
            if (sum1 === sum2) return true;
        }
        for (let i = min[d]; i < 10; i++) {
            if (!getL[i]) {
                getL[i] = true;
                v[d] = i;
                if (dfs(d + 1)) return true;
                getL[i] = false;
            }
        }
        return false;
    }
    return dfs(0);
};
/**
 * @param {string[]} words
 * @param {string} result
 * @return {boolean}
 */
var isSolvable = function (words, result) {
    let letters = new Set()
    let leading = new Set()
    let left = {}
    let right = {}
    let p = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000]
    for (let word of words) {
        let n = word.length
        for (let i = 0; i < n; i++) {
            const c = word[n - 1 - i]
            letters.add(c)
            if (c in left) {
                left[c] += p[i]
            } else {
                left[c] = p[i]
            }
        }
        leading.add(word[0])
    }
    let n = result.length
    for (let i = 0; i < n; i++) {
        const c = result[n - 1 - i]
        letters.add(c)
        if (c in right) {
            right[c] += p[i]
        } else {
            right[c] = p[i]
        }
    }
    leading.add(result[0])
    let a = Array.from(letters)
    let b = new Array(10)
    let d = {}
    b.fill(true)
    return dfs(a, a.length - 1, d, b, leading, left, right)
};

const dfs = (a, i, d, b, leading, left, right) => {
    if (i >= 0) {
        for (let x = leading.has(a[i]) ? 1 : 0; x < 10; x++) {
            if (b[x]) {
                b[x] = false
                d[a[i]] = x
                if (dfs(a, i - 1, d, b, leading, left, right)) {
                    return true
                }
                b[x] = true
            }
        }
        return false
    } else {
        let diff = 0
        for (c in left) {
            diff += d[c] * left[c]
        }
        for (c in right) {
            diff -= d[c] * right[c]
        }
        return diff === 0
    }
}

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/all-elements-in-two-binary-search-trees
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/zhangzs000/p/12114671.html