[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:

  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?

思路:在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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

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

思路:我的思路很简单,所以效率不高,只beat了15.几%的人。。

遍历数组,如果当前数是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   9
to
     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>

0 0