1.题目意思:在数组中,只有一个数字只出现了一次 其他的都出现了两次。找出那个只出现一次的数字。

 //利用位运算 异或   两个相同的数字异或为0
public int singleNumber(int[] nums) {
int res = 0 ;
for (int i = 0; i < nums.length; i++) {
res ^=nums[i];
}
return res;
}

2.题目意思:数组中,每个数字都出现三次  除了只有一个数字只出现了一次。找出那个只出现一次的数字。

 // 方法1:排序解
public static int singleNumber(int[] nums) {
Arrays.sort(nums);
int res = 0;
for (int i = 0; i < nums.length; i += 3) {
if (i == nums.length - 1) {
res = nums[i];
break; //continue 可以
}
if (nums[i] == nums[i + 1]) {
continue;
}else {
res = nums[i];
break;
}
}
return res;
}
 //位运算解---没看懂---哪位大佬知道可以在下面告知我--万分感谢!!!!
public static int singleNumber1(int[] nums) {
int one = 0, two = 0;
for (int i : nums) {
one = (one ^ i) & ~two;// ~补位运算
two = (two ^ i) & ~one;
}
return one;
} //还有一种思路 : 就是用set集合存储,然后求和,然后乘3,减去原数组的和,在除以2---就是我想得到的数;

最新文章

  1. 循环写入Insert 与 SqlBulkcopy
  2. html5的小知识点小集合
  3. PHPCMS V9教程之快速入门
  4. 编写高质量JS代码的68个有效方法(九)
  5. XML文件操作学习(一)
  6. SharePoint咨询师之路:设计之前的那些事一:容量
  7. Live555中RTP包的打包与发送过程分析
  8. Spring Data JPA教程, 第八部分:Adding Functionality to a Repository (未翻译)
  9. openssl生成rsa公私钥
  10. js和HTML结合(补充知识:如何防止文件缓存的js代码)
  11. 【C#4.0图解教程】笔记(第19章~第25章)
  12. Windows phone 8 安装在 VMWare上错误的各种解决方案
  13. chrome开发工具指南(十)
  14. python3.5新增函数isclose的使用
  15. 02_HTML5+CSS详解第四天
  16. Hibernate--对象关系
  17. 实验6 shell程序设计一(1)
  18. 卢卡斯定理——应用hdu4349
  19. [math][mathematica] archlinux 下 mathematica 的安装 (科学计算软件 mathematica/matlab/sagemath)
  20. Qt布局管理: 停靠窗口QDockWidget类(纯代码实现)

热门文章

  1. MySQL-Tool:Navicate 安装
  2. js方式的页面跳转
  3. 真正认识 realloc 的工作方式(转载)
  4. bzoj 3503: [Cqoi2014]和谐矩阵【高斯消元】
  5. Activiti6.0教程 Service用途剖析 (二)
  6. 一步一步学习GTK
  7. Use Power bi Mobile Show SSRS 2016 Mobile Report;使用 Power BI Mobile 查阅ssrs2016 mobile report
  8. java的构造方法 this 重载
  9. C#关于html颜色值的转化 ColorTranslator
  10. js连续赋值,你理解了吗