2 Star 0 Fork 0

taotechip/CodeForInterview

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Q22.md 787 Bytes
一键复制 编辑 原始数据 按行查看 历史
Outliwer 提交于 7年前 . 剑指offer部分

数组中只出现一次的数字

  • 把这两个数分开到两个数组中即可
//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
public class Solution {
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
        int sum = 0;
        for (int i = 0; i < array.length; i ++){
            sum ^= array[i];
        }
        int index = 0;
        for (index = 0; index < 32; index ++){
            if ( (sum&(1 << index)) != 0){
                break;
            }
        }
        for (int i = 0; i < array.length; i ++){
            if ( (array[i] & (1 << index)) != 0){
                num1[0] ^= array[i];
            } else {
                num2[0] ^= array[i];
            }
        }
    }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/taotechip/code-for-interview.git
git@gitee.com:taotechip/code-for-interview.git
taotechip
code-for-interview
CodeForInterview
master

搜索帮助