-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0169-majority-element.js
More file actions
101 lines (82 loc) · 2.47 KB
/
Copy path0169-majority-element.js
File metadata and controls
101 lines (82 loc) · 2.47 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//Blog: https://www.allenliservice.online/leetcode-js-169-majority-element/
// <strong>Solution:</strong>
// 1. 如 nums 中只有一個元素時,回傳該元素的值。
// 2. 宣告一個 box 物件。
// 3. 進行 nums.length 長度的迴圈。
// 4. 運用物件{ } 的特性,如果 box[i] 中沒有對應的物件,則顯示undefined,而!undefined 為 false 轉為 true,執行對應物件次數為 { 'i': 1 }
// 5 - 1. 如有 box[i] 存在物件中,則顯示次數,而!1 的相反為 true 轉為 false,執行對應物件++ 為 { 'i': 1 + 1 }
// 5 - 2. 如果該物件的次數大於 nums陣列的長度 / 2,則返回該數值。
// <strong>Code:</strong>
var majorityElement = function (nums) {
if (nums.length === 1) return nums[0];
let box = {};
for (let i = 0; i < nums.length; i++) {
if (!box[nums[i]]) {
box[nums[i]] = 1;
} else {
box[nums[i]]++;
if (box[nums[i]] >= nums.length / 2) {
return nums[i];
}
}
}
};
/* <strong>Example 1</strong>
Input: nums = [3,2,3]
nums.length = 3
step.1
i = 0
box = {}
!box[nums[0]] => !box[3] => !undefined = true
box[nums[0]] = 1 //{'3': 1}
i = 1
box = {'3': 1}
!box[nums[1]] => !box[2] => !undefined = true
box[nums[1]] = 1//{ '2': 1, '3': 1 }
i = 2
box = {'2': 1, '3': 1}
!box[nums[2]] => !box[3] => !1 => false
box[nums[2]]++ => {'2': 1, '3': 2}
(box[nums[2]] >= nums.length / 2) //{'3'} 2 >= (3/2)=> 2 >= 1.5
return nums[2] //3 */
// <strong>Code 2: Boyer–Moore majority vote algorithm BigO(n)</strong>
var majorityElement = function (nums) {
if (nums.length === 1) return nums[0];
let target,
counter = 0;
for (const num of nums) {
if (counter === 0) {
target = num;
counter++;
} else if (target === num) {
counter++;
} else {
counter--;
}
}
return target;
};
// <strong>Code 3: Boyer–Moore majority vote algorithm BigO(n)</strong>
var majorityElement = function (nums) {
if (nums.length === 1) return nums[0];
let target = 0,
counter = 0;
for (const num of nums) {
if (counter === 0) {
target = num;
}
counter += num === target ? 1 : -1;
}
return target;
};
// <strong>Code 4: HashTable BigO(m + n)</strong>
var majorityElement = function (nums) {
if (nums.length === 1) return nums[0];
let hashTable = {};
for (let i = 0; i < nums.length; i++) {
hashTable[nums[i]] = (hashTable[nums[i]] || 0) + 1;
}
for (let num in hashTable) {
if (hashTable[num] >= nums.length / 2) return num;
}
};