Try   HackMD
Dark Theme License

The MIT License (MIT)

Copyright © 2022-2023 Lumynous

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1318. Minimum Flips to Make a OR b Equal to c

題目連結: Leetcode

題目概要

給三個數字

abc
問最少更改幾個
a
b
的二進位制位元
可使
ab
=
c

為位元 or 運算
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

範例
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

想法

作法一 vector比對

依題目可知

a
b
c
的範圍為
1abc109

109
轉換成二進制後為30位

因此可將

a
b
c
都轉換成vector<bool>來儲存其二進制
為避免不同數字轉換成二進制的長度不同
將用來儲存的vector原始設為30個0
再依照數字二進制每個位元去更改

最後再比對每個位元

i 是否符合
aibi
=
ci

作法二 位元運算

利用位元運算符 & (AND) 依序判斷三個的數字第一個位元
每判斷一個位元就將該數字右移(>>)1位並賦值回該數字

參考解法

歡迎補充

CPP 法一
vector<bool> binary(int n){ vector<bool> vec(30, 0); int c = 0; while (n != 1){ vec[c++] = n%2; n /= 2; } vec[c] = 1; return vec; } int minFlips(int a, int b, int c) { vector<bool> vec_a = binary(a); vector<bool> vec_b = binary(b); vector<bool> vec_c = binary(c); int ans = 0; for (int i=0; i<30; i++){ if ((vec_a[i] or vec_b[i]) != vec_c[i]){ if (vec_c[i]) ans++; else{ if (vec_a[i] == vec_b[i]) ans += 2; else ans += 1; } } } return ans; }
CPP 法二
int minFlips(int a, int b, int c){ int ans = 0; while (a != 0 or b != 0 or c != 0){ if (c & 1 == 1){ if (!((a & 1) or (b & 1))) ans ++; // a_i = 0 and b_i = 0 => !false // a_i = 1 and b_i = 0 => !true }else{ ans += (a & 1) + (b & 1); } a >>= 1; b >>= 1; c >>= 1; } return ans; }

Dark Theme License

The MIT License (MIT)

Copyright © 2022 Luminous-Coder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.