[2016/06/25] LeetCode / Java - Day 3 -
来源:互联网 发布:无人机源码 编辑:程序博客网 时间:2024/05/29 04:56
260. Single Number III
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:
- The order of the result is not important. So in the above example,
[5, 3]
is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
思路:在vote最高的回答者下面,基本上都是“brilliant!!”"How could you come up with this???!!!!"之类的赞美哈哈哈。我当然是想不出来的:)但是光看代码,估计还是会存在一些理解上的问题。解释一下好了~
根据昨天做到的这种题目的思路,通过xor的bit操作可以直接找出那一个的single number,但如果是两个的话要怎么解决呢?
The reason why XOR works is, to separate the 2 unique numbers, their bits will differ in atleast one position, which would be the 1's in XOR. We take the rightmost such 1 from the XOR. This 1 must have come from either numbers, to identify which, we XOR all numbers into 2 sets, one set which had that bit set and one which didn't. In the end, the duplicate numbers will cancel themselves out leaving only the unique numbers in each set。
解释一下上面这段话,两个single number的话,他们的binary representation肯定至少有一位是不同的,而bit = xor & ~(xor-1)这个表达式,就可以计算出它们的二进制表示的从右往左的第一位不同,即rightmost 1. 通过num和bit相与计算,便可以将所有的数分为两组,存在这一位bit的数和不存在这一位bit的数(而那两个single number就被分到不同的组去了)。而这两组数中,分别全部xor后就会剩下那两个single number。
public class Solution { public int[] singleNumber(int[] nums) { int result[] = new int[2]; int xor=0; for(int num:nums){ xor ^=num; } int bit = xor & ~(xor-1); int num1=0; int num2=0; for(int num:nums){ if((num & bit)>0) num1 ^=num; else num2 ^=num; } result[0] = num1; result[1] = num2; return result; }}
283.Move Zeroes
Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
遍历数组,如果当前数是0,那么就把它和往后数最近的一个非零元素交换位置。如果后面没有非零元素,那么就结束遍历。
public class Solution { public void moveZeroes(int[] nums) { //和最近的一个非零元素交换位置 for(int i=0;i<nums.length;i++){ if(nums[i]==0) { int j=i+1; while(j<nums.length && nums[j]==0 ){ j++; } if(j==nums.length) break; else{ nums[i]=nums[j]; nums[j]=0; } } } } }
226. Invert Binary Tree
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1
思路:啊又是二叉树!这个我想到的还是用递归。就是不断地交换左右两个子树。
<span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#333333;">public TreeNode invertTree(TreeNode root) { if(root == null) return null; if(root.left!=null){ root.left = invertTree(root.left); } if(root.right!=null){ root.right = invertTree(root.right); } TreeNode temp=root.left; root.left=root.right; root.right=temp; return root; }</span>
- [2016/06/25] LeetCode / Java - Day 3 -
- [2016/06/28] LeetCode / Java - Day 06 -
- [2016/06/24] LeetCode / Java - Day 02 -
- [2016/06/26] LeetCode / Java - Day 04 -
- [2016/06/27] LeetCode / Java - Day 05 -
- [2016/06/29] LeetCode / Java - Day 07 -
- [2016/06/30] LeetCode / Java - Day 08 -
- [2016/07/06] LeetCode / Java - Day 13 -
- [2016/07/01] LeetCode / Java - Day 09 -
- [2016/07/02] LeetCode / Java - Day 10 -
- [2016/07/03] LeetCode / Java - Day 11 -
- [2016/07/05] LeetCode / Java - Day 12 -
- java高新技术day-3
- Java Day 3
- JAVA DAY DAY UP
- java自学日记 day 06
- JAVA SE — Day 06
- Java study 06day-静态代码块
- Css文本框样式
- Chrome中字体设置小于12px后无法显示解决办法
- J2EE开发:struts2 文件上传(单个文件与多个文件)
- iOS工程中创建库工程
- Guava Futures异步回调机制源码解析
- [2016/06/25] LeetCode / Java - Day 3 -
- ReactJS学习系列课程(React mixin的使用)
- C++异常对象的传递
- Hadoop开发---经验总结2
- STL_Iterator
- 注册、忘记密码 界面集成语音验证 -2016.06.25
- leveldb代码阅读知识积累3
- J2EE开发:struts2 文件下载
- Mybatis的一个通用的枚举处理器