Skip to content

Commit 57db01b

Browse files
authored
Update 416-partition-equal-subset-sum.js
1 parent 7edcc4b commit 57db01b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

416-partition-equal-subset-sum.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,23 @@ const canPartition = function(nums) {
9292

9393
return helper(nums, sum, 0)
9494
}
95+
96+
// another
97+
98+
/**
99+
* @param {number[]} nums
100+
* @return {boolean}
101+
*/
102+
const canPartition = function (nums) {
103+
const sumA = nums.reduce((acc, curr) => acc + curr, 0)
104+
if (sumA % 2) return false
105+
let row = 1n << BigInt(sumA / 2)
106+
for (const weight of nums) row = row | (row >> BigInt(weight))
107+
/*
108+
check the the column corresponding to my target by bitwise ANDing
109+
it with just 1,so if the first bit is 1,
110+
it will return true, otherwise false
111+
*/
112+
return row & 1n
113+
}
114+

0 commit comments

Comments
 (0)