10个常见的算法题

本文展示了多个数组操作的JavaScript函数实现,包括区间汇总、无重复字符最长子串查找、全排列、子集生成、sku组合计算、数组随机打乱、二叉树翻转、合并以及判断对称性。同时,还包括了一个移动零元素的数组操作和字符串相加的问题解决方案。
摘要由CSDN通过智能技术生成

c1f85bca19e11cc31f5618b8e91b278a.png

1、汇总区间

输入:nums = [0,1,2,4,5,7]

输出:["0->2","4->5","7"]

var summaryRanges = function(nums) {
    const ret = [];
    let i = 0;
    const n = nums.length;
    while (i < n) {
        const low = i;
        i++;
        while (i < n && nums[i] === nums[i - 1] + 1) {
            i++;
        }
        const high = i - 1;
        const temp = ['' + nums[low]];
        if (low < high) {
            temp.push('->');
            temp.push('' + nums[high]);
        }
        ret.push(temp.join(''));
    }
    return ret;
};

2、无重复字符的最长子串

输入: s = "abcabcbb"

输出: 3

var lengthOfLongestSubstring = function (s) {
  const occ = new Set();
  const n = s.length;
  let rk = -1, ans = 0;
  for (let i = 0; i < n; ++i) {
    if (i != 0) {
      occ.delete(s[i - 1]);
    }
    while (rk + 1 < n && !occ.has(s[rk + 1])) {
      occ.add(s[rk + 1]);
      ++rk;
    }
    ans = Math.max(ans, rk - i + 1);
  }
  return ans;
};

2、全排列

输入:nums = [1,2,3]

输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

function permute(nums) {
  const ans = []
  const dfs = (arr, l, r) => {
    if (l === r) {
      ans.push(arr.slice());
      return;
    }
    for (let i = l; i <= r; i++) {
      [arr[l], arr[i]] = [arr[i], arr[l]];
      dfs(arr, l + 1, r);
      [arr[l], arr[i]] = [arr[i], arr[l]];
    }
  }
  dfs(nums, 0, nums.length - 1);
  return ans;
}

3、子集

输入:nums = [1,2,3]

输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

var subsets = function (nums) {
  const t = [];
  const ans = [];
  const n = nums.length;
  const dfs = (cur) => {
    if (cur === nums.length) {
      ans.push(t.slice());
      return;
    }
    t.push(nums[cur]);
    dfs(cur + 1, nums);
    t.pop(t.length - 1);
    dfs(cur + 1, nums);
  }
  dfs(0, nums);
  return ans;
};

4、sku计算

输入:nums = [[1, 2],["a", "b"], ["x", "y"]]

输出:[[1, "a", "x"], [1, "a", "y"], [1, "b", "x"], [1, "b", "y"], [2, "a", "x"], [2, "a", "y"], [2, "b", "x"], [2, "b", "y"]

function permuteArrays(arrays) {
  if (arrays.length === 0) return [[]];


  const currentArray = arrays[0];
  const remainingArrays = arrays.slice(1);
  const permutations = [];


  const permsWithoutCurrent = permuteArrays(remainingArrays);


  for (let i = 0; i < currentArray.length; i++) {
    for (let j = 0; j < permsWithoutCurrent.length; j++) {
      const permutation = [currentArray[i], ...permsWithoutCurrent[j]];
      permutations.push(permutation);
    }
  }


  return permutations;
}

5、随机打乱

function shuffle(arr) {
  for (let i = 0; i < arr.length - 1; i++) {
    const j = i + Math.floor(Math.random() * (arr.length - i));
    [arr[i], arr[j]] = [arr[j], arr[i]]
  }
  return arr
}

6、翻转二叉树

function reverse(node) {
  if (node != null) {
    let temp = node.left;
    node.left = node.right;
    node.right = temp;
    reverse(node.left);
    reverse(node.right);
  }
}

7、合并二叉树

function mergeTrees(t1, t2) {
  if (t1 == null)
    return t2;
  if (t2 == null)
    return t1;
  t1.value += t2.value;
  let left = mergeTrees(t1.left, t2.left);
  if (left) {
    t1.left = left
  }
  let right = mergeTrees(t1.right, t2.right);
  if (right) {
    t1.right = right
  }
  return t1;
}

8、判断对称二叉树

function isSymmetric (root) {
  return isMirror(root, root)
}


function isMirror (t1, t2) {
  if (t1 == null && t2 == null) return true;
  if (t1 == null || t2 == null) return false;
  return (t1.value === t2.value) && isMirror(t1.right, t2.left) && isMirror(t1.left, t2.right)
}

9、移动零

输入: nums = [0,1,0,3,12]

输出: [1,3,12,0,0]

var moveZeroes = function (nums) {
  let left = 0
  let right = 0
  let n = nums.length
  while (right < n) {
    if (nums[right]) {
      let temp = nums[left]
      nums[left] = nums[right]
      nums[right] = temp
      left++
    }
    right++
  }
  return nums
};

10、字符串相加

输入:num1 = "11", num2 = "123"

输出:"134"

var addStrings = function(num1, num2) {
    let i = num1.length - 1, j = num2.length - 1, add = 0;
    const ans = [];
    while (i >= 0 || j >= 0 || add != 0) {
        const x = i >= 0 ? num1.charAt(i) - '0' : 0;
        const y = j >= 0 ? num2.charAt(j) - '0' : 0;
        const result = x + y + add;
        ans.push(result % 10);
        add = Math.floor(result / 10);
        i -= 1;
        j -= 1;
    }
    return ans.reverse().join('');
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值