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.
題目連結: Leetcode
給三個數字
問最少更改幾個
可使
Learn More →
Learn More →
依題目可知
將
因此可將 vector<bool>
來儲存其二進制
為避免不同數字轉換成二進制的長度不同
將用來儲存的vector
原始設為30個0
再依照數字二進制每個位元去更改
最後再比對每個位元
利用位元運算符 & (AND) 依序判斷三個的數字第一個位元
每判斷一個位元就將該數字右移(>>)1位並賦值回該數字
歡迎補充
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;
}
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;
}
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.
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up