leetcode day3

Same Tree【100】

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

思路:之前做的二叉树的问题基本都会用到递归,这道题也是,判断两个树是否相等

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        boolean result = false;
        if(p==null&&q==null) result =  true;
        else if((p==null&&q!=null)||(p!=null&&q==null)) result =  false;
        else{
            if(p.val==q.val) result =  isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
         }
        return result;
    }
}

 

 

 

Move Zeroes【283】

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]

把数组中的0全部移至末尾,并且不改变原有顺序,并且不能重新分配空间,在原数组上移动。

 

思路: 开始打算用冒泡解决,事实证明可以,就是运行太慢,垫底了都。

思路一:肯定是要把非零部位都往前移,用一个变量记录0的个数,通过比较0的位数和 非零部位的位置可以得出规律,就是每个非零部位的移动距离都取决于他前面有几个0,然后最终的位置就是初始位置减去在此之前0的个数。然后数组的后面后几位全赋值为0.

思路二: 用一个变量记录非0数字出现的次数,在for循环内,当遇到非0数字的时候,重新覆盖数组。

 

//思路二
public void moveZeroes(int[] nums) {
    int p = 0;// Index of none-zero number
    for (int i : nums) 
        if (i != 0) 
          nums[p++] = i;
    while (p < nums.length) nums[p++] = 0;
}
//思路一
public void moveZeroes(int[] nums) { 

int count = 0 , size = nums.length;

for (int i = 0; i< size; i++) {
    if ( nums[i] == 0) { count ++;}
     if ( nums[i] != 0) { nums[i - count] = nums[i];}
}
for (int i = 0; i < count; i++ ) {
    nums[size - count  + i] = 0;
}
}
//like bubble sort,类似冒泡排序
public class Solution {
    public void moveZeroes(int[] nums) {
    int temp ;
    for(int i = 0 ; i<nums.length-1;i++){
       for(int j = i+1; j<nums.length;j++){
          if(nums[i]==0&&nums[j]!=0){
               temp = nums[i];
               nums[i] = nums[j];
               nums[j] = temp;
           }
        }
    }
    }
}

 

 

【226】Invert Binary Tree

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1


思路:习惯想到递归,还有使用队列来遍历交换结点的

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

//使用递归
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        TreeNode temp;
        if(root!=null){
            temp = root.left;
            root.left = root.right;
            root.right = temp; 
            if(root.left!=null){
                 invertTree(root.left);
            }
            if(root.right!=null){
                 invertTree(root.right);
            }
        }
      return root;
    }
}

 

//使用队列来遍历交换结点
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
            if(node.left != null) queue.offer(node.left);
            if(node.right != null) queue.offer(node.right);
        }
        return root;
    }
}

 

 

【217】Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

 

思路:采取set,逐个添加,如果set的大小和数组长度一样,则没有重复的。这种时间运行最慢,再次基础上改进,如果判断set能否add成功元素直接返回结果,则时间运行提高20%.更快的是先将元素排序,使用arrays.sort(nums),然后for循环判断有没有元素相等的,这种有提高了20%

public boolean containsDuplicate1(int[] nums) {//先排序再判断
    Arrays.sort(nums);
    for (int i=0; i<nums.length-1; i++)
        if (nums[i]==nums[i+1])
            return true;
    return false;
}
public boolean containsDuplicate2(int[] nums) {
    HashSet<Integer> mySet = new HashSet<Integer>();
    for (int i=0; i<nums.length; i++) {
        if (mySet.contains(nums[i]))
            return true;
        mySet.add(nums[i]);
    }
    return false;
}

public boolean containsDuplicate(int[] nums) {
    Set<Integer> mySet = new HashSet<Integer> ();
    for (int num: nums)
        if (!mySet.add(num))
            return true;
    return false;
}

 




posted @ 2015-11-11 11:10  伟大的臭猪猪  阅读(232)  评论(0编辑  收藏  举报