[思路]LeetCode Problems- 476. Number Complement
![圖片](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdJou-XkIZhFAhwdMVgXh7HhH2Bxu3DKTpJrGaNssz7r9NQfEeD76eNv8V9YXIo4VTxBDix34TUFDOaVcu79QlgdTjNAOAiORNxGbYmqkNjo1r-bgYvDgcjgAMepHXUriZ8EvopSxnGZlq/s1600/2017-07-30_113303.jpg)
這篇要講的題目是Number Complement,即為"補數"的意思.題目原文如下: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading zero bit in the integer’s binary representation. Example 1: Input: 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. Example 2: Input: 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.題目的ㄧㄠ 題目是要求某數二進位翻轉過後的結果是甚麼數字. 這題跟我先前解過的 [思路]LeetCode Problems- 461. Hamming Distance 有些相似, 都要求出十進位轉二進位, 所以一開始我的想法是: 1.十進位轉二進位 2.0跟1互換,1變0,0變1 3.在反求二進位轉十進位的數值 看起來還蠻合理的,但是在開始著手之後,就發覺整個過程變得很複雜.... 首先是在於,我在算十進位轉二進位的方法,主要是一開始先創設32位元的0元素陣列(考慮到int變數的位元範圍下), 再用除法除2時,把求得的餘數1或0放進去 例如對5做轉換求到的值,在陣列中被表...