File tree Expand file tree Collapse file tree
javascript/LeetCode/Array Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * 3314. Construct the Minimum Bitwise Array I
3+ *
4+ * prime number = 1 & itself.
5+ * ans[i] 與 ans[i] + 1 的位元或運算等於 nums[i],即 ans[i] OR (ans[i] + 1) == nums[i]。
6+ *
7+ * @param {number[] } nums
8+ * @return {number[] }
9+ */
10+ var minBitwiseArray = function ( nums ) {
11+ let ans = new Array ( nums . length ) ;
12+
13+ for ( let i = 0 ; i < nums . length ; ++ i ) {
14+ let maybe = - 1 ;
15+ for ( let j = 1 ; j < nums [ i ] ; ++ j ) {
16+ if ( ( j | ( j + 1 ) ) === nums [ i ] ) {
17+ maybe = j ;
18+ break ;
19+ }
20+ }
21+ ans [ i ] = maybe ;
22+ }
23+ return ans ;
24+ } ;
25+ let nums = [ 2 , 3 , 5 , 7 ] ;
26+ /**
27+ * [-1,1,4,3]
28+ Explanation:
29+ For i = 0, as there is no value for ans[0] that satisfies ans[0] OR (ans[0] + 1) = 2, so ans[0] = -1.
30+ For i = 1, the smallest ans[1] that satisfies ans[1] OR (ans[1] + 1) = 3 is 1, because 1 OR (1 + 1) = 3.
31+ For i = 2, the smallest ans[2] that satisfies ans[2] OR (ans[2] + 1) = 5 is 4, because 4 OR (4 + 1) = 5.
32+ For i = 3, the smallest ans[3] that satisfies ans[3] OR (ans[3] + 1) = 7 is 3, because 3 OR (3 + 1) = 7.
33+ */
34+ console . log ( minBitwiseArray ( nums ) ) ;
You can’t perform that action at this time.
0 commit comments