From afa70a68f0023736c0e0211feeabd5979ca59594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BD=B3=E5=B8=86?= <3151342895@qq.com> Date: Tue, 16 Aug 2022 02:26:46 +0800 Subject: [PATCH] i1 --- .../README.md" | 26 +++++++++++++++++++ .../Solution.cpp" | 22 ++++++++++++++++ .../README.md" | 23 ++++++++++++++++ .../Solution.cpp" | 18 +++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/Solution.cpp" create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.cpp" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" index d4ae5f07cc459..45f241e48f02a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" @@ -126,6 +126,32 @@ public class Solution { } } ``` +### **C++** + +```cpp +class Solution { +public: + vector singleNumbers(vector& nums) { + int res = 0; + for(int x : nums) { + res ^= x; + } + int div = 1; + while(!(div & res)) { + div <<= 1; + } + int a = 0,b = 0; + for(int x : nums) { + if(x & div) { + a ^= x; + } else { + b ^= x; + } + } + return vector {a,b}; + } +}; +``` ### **...** diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/Solution.cpp" new file mode 100644 index 0000000000000..6125ae1bb0f30 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/Solution.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + vector singleNumbers(vector& nums) { + int res = 0; + for(int x : nums) { + res ^= x; + } + int div = 1; + while(!(div & res)) { + div <<= 1; + } + int a = 0,b = 0; + for(int x : nums) { + if(x & div) { + a ^= x; + } else { + b ^= x; + } + } + return vector {a,b}; + } +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" index b23ad1d70c499..2edfbb23b254f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" @@ -131,6 +131,29 @@ public class Solution { } ``` +### **C++** + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + int bits[32] = {0}; + for(int i = 0; i < nums.size(); i++) { + for(int j = 0; j < 32; j++) { + if(nums[i] & (1 << j)) { + bits[j]++; + } + } + } + int ans = 0; + for(int i = 0; i < 32; i++) { + ans += (1 << i) * (bits[i] % 3); + } + return ans; + } +}; +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.cpp" new file mode 100644 index 0000000000000..dd1beead03011 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + int singleNumber(vector& nums) { + int bits[32] = {0}; + for(int i = 0; i < nums.size(); i++) { + for(int j = 0; j < 32; j++) { + if(nums[i] & (1 << j)) { + bits[j]++; + } + } + } + int ans = 0; + for(int i = 0; i < 32; i++) { + ans += (1 << i) * (bits[i] % 3); + } + return ans; + } +}; \ No newline at end of file