LeeCode-Single Number II

Given an array of integers, every element appears three times except for one. Find that single one.

 1 int singleNumber(int* nums, int numsSize)
 2 {
 3     if(numsSize==1)
 4         return nums[0];
 5     int i,j,k;
 6     for(i=1;i<numsSize;i++)
 7     {
 8         for(j=i-1;j>=0;j--)
 9         {
10             if(nums[i]>=nums[j])
11             {
12                 break;
13             }
14         }
15
16         if(j!=i-1)
17         {
18             int temp=nums[i];
19             for(k=i-1;k>j;k--)
20             {
21                 nums[k+1]=nums[k];
22             }
23             nums[k+1]=temp;
24         }
25     }
26
27     if(nums[0]!=nums[1])
28     {
29         return nums[0];
30     }
31     else
32     {
33         for(i=0;i<numsSize-1;i++)
34         {
35             if((nums[i]==nums[i+1])||(nums[i+1]==nums[i+2]))
36             {
37                 continue;
38             }
39             else
40             {
41                 return nums[i+1];
42             }
43         }
44     }
45 }
时间: 2024-10-18 05:57:20

LeeCode-Single Number II的相关文章

Single Number,Single Number II

Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium 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 imp

[LeetCode][JavaScript]Single Number II

Single Number II 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? https://leetcode.com/

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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

leetcode Single Number II python

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for eachint in nums: if

Single Number II ——位操作

题意: 给定两个32位的整数 N 和 M,以及表示比特位置的 i 与 j .编写一个方法,将 M 插入 N,使得 M 从 N 的第 j 位开始,到第 i 位结束.假定从 j 位到 i 位足以容纳M. 输入:N = 10000101000,M = 10011,i = 2, j = 6 输出:N = 10001001100 解题思路 :  根据题意,我们将问题解决分为三个步骤: (1)将 N 中从 j 到 i 之间的位清零: (2)对 M 进行移位操作,M << i (3)合并 M 与 N . 为

LeetCode 137:Single Number II

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? Single Number II 比Single Number要复杂的多,

Single Number和Single Number II

1 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? 解析: a ^ a = 0,a ^ 0 = a 所以对所

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

【LeetCode】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So