From ae5cd15279e2515867d4e710004eb988984f2d1d Mon Sep 17 00:00:00 2001 From: jaredliw Date: Thu, 22 Jul 2021 23:12:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0py=EF=BC=8Cjs=E5=92=8Cc++?= =?UTF-8?q?=EF=BC=8C=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BA=8E=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../求次数问题/只出现一次的数.md | 284 ++++++++++++++++-- 1 file changed, 266 insertions(+), 18 deletions(-) diff --git a/animation-simulation/求次数问题/只出现一次的数.md b/animation-simulation/求次数问题/只出现一次的数.md index 0a20461..58133e4 100644 --- a/animation-simulation/求次数问题/只出现一次的数.md +++ b/animation-simulation/求次数问题/只出现一次的数.md @@ -1,3 +1,5 @@ +> +> > 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈 > > 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。 @@ -30,6 +32,8 @@ #### 题目代码 +Java Code: + ```java class Solution { public int singleNumber(int[] nums) { @@ -53,6 +57,64 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + //特殊情况 + if (nums.size() == 1) { + return nums[0]; + } + //HashMap + unordered_map map; + //将其存入哈希表中,含义为,若该元素不存在则存入表中,并计数为1,若已经存在获取次数并加1 + for (int x : nums) { + if (map.find(x) == map.end()) { + map.insert({x, 0}); + } + map[x] += 1; + } + //遍历出出现次数为1的情况 + for (int y : nums) { + if(map.at(y) == 1){ + return y; + } + } + return -1; + } +}; +``` + +JS Code: + +```javascript +var singleNumber = function(nums) { + //特殊情况 + if (nums.length === 1) { + return nums[0]; + } + //HashMap + map = {}; + //将其存入哈希表中,含义为,若该元素不存在则存入表中,并计数为1,若已经存在获取次数并加1 + for (let x of nums) { + if (!(x in map)) { + map[x] = 0; + } + map[x] += 1; + } + //遍历出出现次数为1的情况 + for (let key in map) { + if (map[key] === 1) { + return key; + } + } +}; +``` + +Python Code: + ```python class Solution: def singleNumber(self, nums: List[int]) -> int: @@ -104,13 +166,44 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + //排序 + sort(nums.begin(), nums.end()); + for (int i = 1; i < nums.size() - 1; i += 2){ + if (nums[i] != nums[i - 1]){ + return nums[i - 1]; + } + } + return nums[nums.size() - 1]; + } +}; +``` + +JS Code: + +```javascript +var singleNumber = function(nums) { + // 排序 + nums.sort(); + for (let i = 1; i < nums.length - 1; i += 2) { + if (nums[i] != nums[i - 1]) { + return nums[i - 1]; + } + } + return nums[nums.length - 1]; +}; +``` + Python Code: ```python class Solution: def singleNumber(self, nums: List[int]) -> int: - if len(nums) == 1: - return nums[0] # 排序 nums.sort() for i in range(1, len(nums), 2): @@ -136,18 +229,14 @@ Java Code: ```java class Solution { public int singleNumber(int[] nums) { - if (nums.length == 1){ - return nums[0]; - } HashSet set = new HashSet<>(); //循环遍历 for (int x : nums){ //已经存在,则去除 if(set.contains(x)){ set.remove(x); - } //否则存入 - else{ + } else { set.add(x); } } @@ -157,13 +246,53 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + unordered_set set; + //循环遍历 + for (int x : nums) { + //已经存在,则去除 + if (set.find(x) == set.end()) { + set.insert(x); + //否则存入 + } else { + set.erase(x); + } + } + //返回仅剩的一个元素 + return *set.begin(); + } +}; +``` + +JS Code: + +```javascript +var singleNumber = function(nums) { + let set = new Set(); + //循环遍历 + for (let x of nums) { + //已经存在,则去除 + if (set.has(x)) { + set.delete(x); + //否则存入 + } else { + set.add(x); + } + } + return set.values().next().value; +}; +``` + Python Code: ```python class Solution: def singleNumber(self, nums: List[int]) -> int: - if len(nums) == 1: - return nums[0] set_ = set() # 循环遍历 for x in nums: @@ -189,12 +318,11 @@ class Solution: #### 题目代码 +Java Code: + ```java class Solution { public int singleNumber(int[] nums) { - if (nums.length == 1) { - return nums[0]; - } Arrays.sort(nums); Stack stack = new Stack<>(); for (int x : nums){ @@ -214,6 +342,53 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + sort(nums.begin(), nums.end()); + stack stack; + for (int x: nums) { + if (stack.empty()) { + stack.push(x); + continue; + } + //不同时直接跳出 + if (stack.top() != x) { + break; + } + //相同时出栈 + stack.pop(); + } + return stack.top(); + } +}; +``` + +JS Code: + +```javascript +var singleNumber = function(nums) { + nums.sort(); + let stack = []; + for (let x of nums) { + if (!stack.length) { + stack.push(x); + continue; + } + //不同时直接跳出 + if (stack[0] !== x) { + break; + } + //相同时出栈 + stack.pop(); + } + return stack[0]; +}; +``` + Python Code: ```python @@ -229,8 +404,10 @@ class Solution: if not stack: stack.append(x) continue + # 不同时直接跳出 if stack[-1] != x: break + # 相同时出栈 stack.pop() return stack[-1] ``` @@ -249,12 +426,11 @@ class Solution: #### 题目代码 +Java Code: + ```java class Solution { public int singleNumber(int[] nums) { - if (nums.length == 1){ - return nums[0]; - } HashSet set = new HashSet<>(); int setsum = 0; int numsum = 0; @@ -273,6 +449,51 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + unordered_set set; + int setsum = 0; + int numsum = 0; + for (int x : nums) { + //所有元素的和 + numsum += x; + if (set.find(x) == set.end()) { + //HashSet内元素的和 + setsum += x; + } + set.insert(x); + } + //返回值 + return setsum * 2 - numsum; + } +}; +``` + +JS Code: + +```javascript +var singleNumber = function(nums) { + let set = new Set(); + let setsum = 0; + let numsum = 0; + for (let x of nums) { + //所有元素的和 + numsum += x; + if (!set.has(x)) { + setsum += x; + } + //HashSet内元素的和 + set.add(x); + } + //返回值 + return 2 * setsum - numsum; +}; +``` + Python Code: ```python @@ -289,7 +510,7 @@ class Solution: 这个方法主要是借助咱们的位运算符 ^ 按位异或,我们先来了解一下这个位运算符。 -> 按位异或(XOR)运算符“^”是双目运算符。 其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为1。相同时为0。 +> 按位异或(XOR)运算符“^”是双目运算符。 其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为1。相同时为0。 > 任何数和0异或,仍为本身:a⊕0 = a > 任何数和本身异或,为0:a⊕a = 0 @@ -320,6 +541,31 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int singleNumber(vector& nums) { + int num = 0; + //异或 + for (vector::iterator x = nums.begin(); x != nums.end(); x++) { + num ^= *x; + } + return num; + } +}; +``` + +JS Code: + +```javascript +var singleNumber = function(nums) { + //异或 + return nums.reduce((num, x) => num ^= x); +}; +``` + Python Code: ```python @@ -327,7 +573,9 @@ from functools import reduce class Solution: def singleNumber(self, nums: List[int]) -> int: - return reduce(lambda num, x: int(num) ^ int(x), nums) + # 异或 + return reduce(lambda num, x: num ^ x, nums, 0) ``` -本题一共介绍了6种解题方法,肯定还有别的方法,欢迎大家讨论。大家可以在做题的时候一题多解。这样能大大提高自己解题能力。下面我们来看一下这些方法如何应用到其他题目上。 \ No newline at end of file +本题一共介绍了6种解题方法,肯定还有别的方法,欢迎大家讨论。大家可以在做题的时候一题多解。这样能大大提高自己解题能力。下面我们来看一下这些方法如何应用到其他题目上。 +