136. Single Number leetcode做题报告

Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
class Solution {

public:

int singleNumber(vector<int>& nums) {

for(int i=1;i<nums.size();i++){

nums[0]^=nums[i];

}

return nums[0];

}

};

要求O(n)并且不用额外的变量。
非常巧妙的技巧。相同数取异或为0,所0和所有数的异或为本身,所以最后剩下的就是Single Number。
时间: 2024-08-10 11:32:19

136. Single Number leetcode做题报告的相关文章

136. Single Number - LeetCode

Question 136.?Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int singleNumber(int[] nums) { Map<Integer, Integer> countMap = new HashMap<>(); for (int i=0; i<nums.length; i++) { Integer count = countMap.get(nums

Single Number | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/single-number/ 题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without usin

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

136. Single Number C++ 答案

136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int singleNumber(vector<int>& nums) { int s = 0; for(int i = 0; i < nums.size(); i++) { s = s ^ nums[i]; } return s; } }; 参考 LeetCode Problems' So

94. Binary Tree Inorder Traversal 做题报告

题目链接: 94. Binary Tree Inorder Traversal 题目大意: 二叉树的中序遍历 做题报告: (1)该题涉及的算法,数据结构以及相关知识点 递归 (2)自己的解答思路+代码+分析时间和空间复杂度 递归思路 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) {

【59】136. Single Number

136. Single Number Description Submission Solutions Add to List Total Accepted: 191020 Total Submissions: 360448 Difficulty: Easy Contributors: Admin Given an array of integers, every element appears twice except for one. Find that single one. Note:Y

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

20. Valid Parentheses做题报告

题目链接: Valid Parentheses 题目大意: 判断字符串s的字符是否满足符号匹配 做题报告: (1)该题涉及的算法与数据结构 栈,哈希表 (2)自己的解答思路+代码+分析时间和空间复杂度 思路: 栈先入后出特点,若遇到左括号入栈,遇到右括号时将对应栈顶左括号出栈,则遍历完所有括号后 stack 仍然为空则表示满足符号匹配,输出true,否则输出false 代码: import java.util.Stack; class Solution { public boolean isVa

1047. Remove All Adjacent Duplicates In String做题报告

题目链接: Remove All Adjacent Duplicates In String 题目大意: 删除字符串中的所有相邻字符 做题报告: (1)该题涉及的算法与数据结构 栈 (2)自己的解答思路+代码+分析时间和空间复杂度 Input: "abbaca" Output: "ca"          思路:使用栈,对字符串遍历,进行入栈出栈操作.如果栈空或者遍历到的该字符与栈顶元素不同则入栈,否则(即遍历到的该字符与栈顶元素相同)出栈.最后,栈存的字符就是我们