Leetcode: SingleNumber I & II & III 136/137/260

SingleNumber I:

题目链接:https://leetcode-cn.com/problems/single-number/

题意:

  给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

  说明:

  你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

  示例 1:

  输入: [2,2,1]
  输出: 1

  示例 2:

  输入: [4,1,2,1,2]
  输出: 4

分析:

  利用异或(xor)运算,两个相同的数异或为0

代码如下:

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         int ans = 0;
 5         for(int i = 0; i < n; ++i) {
 6             ans ^= A[i];
 7         }
 8         return ans;
 9     }
10 };

SingleNumber II:

题目链接:https://leetcode-cn.com/problems/single-number-ii/

题意:

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

示例 1:

  输入: [2,2,3,2]
  输出: 3

示例 2:

  输入: [0,1,0,1,0,1,99]
  输出: 99

分析:

TODO

代码如下:

TODO

SingleNumber III:

题目链接:https://leetcode-cn.com/problems/single-number-iii/

题意:

给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。

示例 :

输入: [1,2,1,3,2,5]
输出: [3,5]

注意:

    1. 结果输出的顺序并不重要,对于上面的例子, [5, 3] 也是正确答案。
    2. 你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?

分析:

TODO

代码如下:

TODO

原文地址:https://www.cnblogs.com/zaq19970105/p/10733364.html

时间: 2024-08-02 12:06:16

Leetcode: SingleNumber I & II & III 136/137/260的相关文章

Leetccode 136 137 260 SingleNumber I II III

Leetccode 136 SingleNumber I 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? 1.自己想到了先排序,再遍历数

136.137.260. Single Number &amp;&amp; 位运算

136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) 再说一下异或的性质,满足交换律和结合律 因此: 对于任意一个数n n ^ 0 = n n ^ n = 0 对于这道题来说,所有数依次异或剩下的就是那个数了 1 class Solution(object): 2 def singleNumber(self, nums): 3 ans = 0 4 for i

[LeetCode] Contains Duplicate(II,III)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 解题思路 用一个set保存数组中的值,如

Leetcode 137. Single Number I/II/III

Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XOR的特性, X^0 = X, X^X = 0, 并且XOR满足交换律. 1 class Solution(object): 2 def singleNumber(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I 题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益. 分析:动态规划法.从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求. 代码:时间O(n),空间O(1). Best Time to Buy and Sell Stock II 题目:用一

LeetCode: Word Break II 解题报告

Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat",

买卖股票的最佳时机I II III IV

I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票). III 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易. 样例 给出一个样例数组

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2]