136_Single Number

题目

Given a non-empty 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?

Example 1:

Input: [2,2,1]
Output: 1
Example 2:

Input: [4,1,2,1,2]
Output: 4

解法一(没达到最好要求,额外用了空间,但有通用性):
第一个感觉,可以使用哈希表,每次查找是否有相同的用o(1)的时间复杂度就可以,共查询n次, 加上第一个建立哈希表, 建立哈希表可以认为是1的时间复杂度 也就是o(n)的时间复杂度,空间复杂度o(n)
这个方法可以解single num i ii iii(修改下返回参数即可) 还是有通用性的~

class Solution {
public:
    int singleNumber(vector<int>& nums) {
    	int nRes = -1;
        unordered_multiset<int> numdict(nums.begin(), nums.end());
        for(auto iter=nums.begin(); iter != nums.end(); iter++)
        {
        	if(1 == numdict.count(*iter))
            {
            	nRes=*iter;
                break;
            }
        }
        return nRes;
    }
};

注意:这里使用unordered_multiset 含有可能非唯一对象的集合;不能用unordered_set,它是非唯一对象的集合,2,2,1 放进set,就变成了2,1了,这里需要注意

解法二:
使用异或的特性,相同为0,不同为1; 0异或任何数都是任何数;异或满足交换律
我们可以使用按位异或,假设给了 a,b,b,a,c五个数; abbac = (aa)(bb)c = 00c = c; 异或到最后的结果,也就是要求的数;

class Solution {
public:
    int singleNumber(vector<int>& nums) {
    	int nRes = 0;
		for(auto iter=nums.begin(); iter != nums.end(); iter++)
        {
        	nRes^=*iter;
        }
        return nRes;
    }
};
void eeprom_buffer_write(uint8_t* p_buffer, uint8_t write_address, uint16_t number_of_byte) { uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0; address = write_address % I2C_PAGE_SIZE; count = I2C_PAGE_SIZE - address; number_of_page = number_of_byte / I2C_PAGE_SIZE; number_of_single = number_of_byte % I2C_PAGE_SIZE; //write_address相对于当前页的偏移量 //count表示该页中还剩余多少可写的空间 //number_of_page表示要写入的整个页数(即不包括最后一页) //number_of_single表示要写入的最后一页的字节数 if(0 == address){ while(number_of_page--){ eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE); eeprom_wait_standby_state(); write_address += I2C_PAGE_SIZE; p_buffer += I2C_PAGE_SIZE; } if(0 != number_of_single){ eeprom_page_write(p_buffer, write_address, number_of_single); eeprom_wait_standby_state(); } }else{ /* if write_address is not I2C_PAGE_SIZE aligned / // if(number_of_byte < count){ eeprom_page_write(p_buffer, write_address, number_of_byte); eeprom_wait_standby_state(); }else{ number_of_byte -= count; number_of_page = number_of_byte / I2C_PAGE_SIZE; number_of_single = number_of_byte % I2C_PAGE_SIZE; if(0 != count){ eeprom_page_write(p_buffer, write_address, count); eeprom_wait_standby_state(); write_address += count; p_buffer += count; } / write page / while(number_of_page--){ eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE); eeprom_wait_standby_state(); write_address += I2C_PAGE_SIZE; p_buffer += I2C_PAGE_SIZE; } / write single */ if(0 != number_of_single){ eeprom_page_write(p_buffer, write_address, number_of_single); eeprom_wait_standby_state(); } } } }详细解释这段的代码的每一个语句
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值