Single Number III_260

原题的描述如下:

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

解题的思路如下:

1.咋一看到这个题目一定会似曾相似,大家一定遇到过一个数组中求得其中单独出现一次的一个数。无非就是把数组中的数全部异或处理就得到了该数组中出现了一次的数。

2.该题的解题思路借鉴这一思路,无非是按照一定的规则把包含两个出现一次的数A和B分成两个分别包含A和B的小数组,这样就可以利用1中的思想。

3.该规则应该如何来设计呢?这就需要借助位运算的思想。具体如下:1)把数组全部异或运算,得到的是res=A^B。找到res中的一个位点为1的位,如找到最右边的位1的位:

res=res&-res;然后依据该位来把大数组分成两个小数组,此时A&res和B&res一定不相同,所以可以分成两个不同的数组。这样就可以借助1中的思想了。具体代码如下:

public class Solution {
    public int[] singleNumber(int[] nums) {
        int length=nums.length;    
       int [] re=new int[2];
       int res=0;
       for(int i=0;i<length;i++)
       {
        res=res^nums[i];
       }
       res=res&-res;
       for(int j=0;j<length;j++)
        if((res&nums[j])==0)
           re[0]=re[0]^nums[j];
        else re[1]=re[1]^nums[j];
       
       return re;
    
}
}

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、付费专栏及课程。

余额充值