Home LeetCode. 16. 3Sum Closest
Post
Cancel

LeetCode. 16. 3Sum Closest

[Link] https://leetcode.com/problems/3sum-closest/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function radixSort(array) {
  let max;
  let digitArr = new Array(10);
  for (let i = 0; i < digitArr.length; i++) {
    digitArr[i] = [];
  }
  for (let i = 0; i < array.length; i++) {
    if (i === 0 || max < array[i]) {
      max = array[i];
    }
  }
  let maxLog = Math.log10(max) + 1;
  for (let digit = 1; digit <= maxLog; digit++) {
    let digit10 = Math.pow(10, digit);
    let tempArr = [];
    for (let i = 0; i < array.length; i++) {
      if (digit === 1) {
        digitArr[array[i] % digit10].push(array[i]);
      } else {
        tempDigit = Math.floor(((array[i] % digit10) * 10) / digit10);
        digitArr[tempDigit].push(array[i]);
      }
    }

    for (let i = 0; i < digitArr.length; i++) {
      while (digitArr[i].length !== 0) {
        tempArr.push(digitArr[i][0]);
        digitArr[i].shift();
      }
    }
    array = [...tempArr];
  }
  return array;
}
This post is licensed under CC BY 4.0 by the author.