1684838553 / arithmeticQuestions

程序员的算法趣题
2 stars 0 forks source link

考试(入门) #14

Open 1684838553 opened 1 year ago

1684838553 commented 1 year ago

考试题(力扣)

  1. 485
  2. 1598
  3. 819
  4. 234
  5. 706
  6. 617
  7. 1004
  8. 227
  9. 21
  10. 1614
  11. 1221
  12. 1030
  13. 1137
  14. 783
1684838553 commented 1 year ago

https://leetcode.cn/problems/max-consecutive-ones/

/** 
 * @param {number[]} nums
 * @return {number}
 */
var findMaxConsecutiveOnes = function(nums) {
    let max = 0
    let total = 0
    for(let i =0;i<nums.length;i++){
        if(nums[i] === 1){
            total += 1
        }else{
            max = Math.max(max,total)
            total = 0
        }
    }
    max = Math.max(max,total)
    return max
};
1684838553 commented 1 year ago

https://leetcode.cn/problems/crawler-log-folder/

/**
 * @param {string[]} logs
 * @return {number}
 */
var minOperations = function(logs) {
    let step = 0
    for(let i=0;i<logs.length;i++){
        if(logs[i] === '../'){
            if(step === 0){
                step === 0
            }else{
                step -= 1
            }
        } else if(logs[i] === './'){
            continue;
        } else {
            step += 1
        }
    }
    return step
};
1684838553 commented 1 year ago

https://leetcode.cn/problems/design-hashmap/

var MyHashMap = function() {
    this.map = new Map() 
};

/** 
 * @param {number} key 
 * @param {number} value
 * @return {void}
 */
MyHashMap.prototype.put = function(key, value) {
    this.map.set(key, value);
};

/** 
 * @param {number} key
 * @return {number}
 */
MyHashMap.prototype.get = function(key) {
   const value = this.map.get(key)
   return typeof value === 'number'?  value : -1
};

/** 
 * @param {number} key
 * @return {void}
 */
MyHashMap.prototype.remove = function(key) {
    this.map.delete(key)
};

/**
 * Your MyHashMap object will be instantiated and called as such:
 * var obj = new MyHashMap()
 * obj.put(key,value)
 * var param_2 = obj.get(key)
 * obj.remove(key)
 */
1684838553 commented 1 year ago

https://leetcode.cn/problems/merge-two-binary-trees/

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function(root1, root2) {
   if(root2 && root1){
       root1.val += root2.val
       root1.left =  mergeTrees(root1.left, root2.left)
       root1.right = mergeTrees(root1.right, root2.right)
   }
   return root1 || root2
};
1684838553 commented 1 year ago

https://leetcode.cn/problems/maximum-nesting-depth-of-the-parentheses/

/**
 * @param {string} s
 * @return {number}
 */
var maxDepth = function(s) {
    const stack = []
    let max = 0
    for(let i =0;i<s.length;i++){
        if(s[i] === '('){
            stack.push(s[i])
        } else if(s[i] === ')'){
            stack.pop()
        }
        max = Math.max(max, stack.length)
    }
    return max
};