[2016/06/26] LeetCode / Java - Day 04 -

349. Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.
[思路] hhhhhh看我暴力而不经大脑的解法……居然还只有8ms(我以为起码要个20ms的。。)然后还beat了25%的人呢……!(你到底在得意个啥)。说实在的,看了Discuss Board,我对Stream解法不是很懂。我打算二刷(真的会有二刷么……)的时候好好理解一下。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
            	List<Integer> l = new ArrayList<Integer>();
    	Arrays.sort(nums1);Arrays.sort(nums2);
    	int i=0,j=0;
    	while(i<nums1.length && j<nums2.length)
    	{	
    		int temp = nums1[i];    	
    		if(temp==nums2[j])
    		{
    			if(!l.contains(temp))
    				l.add(temp);
    			j++;i++;
    		}
    		else if (temp>nums2[j])
    			j++;
    		else if(temp<nums2[j])
    			i++;    			
    	}
    	int[] num =new  int[l.size()];
    	for(i=0;i<l.size();i++)
    		num[i] = l.get(i);
		return num;       
    }
}

237. Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

[思路] 刚看到题觉得很简单(虽然的确就是很简单),不就是个链表删节点的问题嘛。后来仔细一看发现它只给当前节点,而且ListNode这个类没有previous诶(本来就没有!)。本来是想直接把node.previous.next = node.next; 就可以了~ 但是显然这是不行的→ → 然后就按照正常思路来就行,把下一个节点覆盖当前节点,然后把下一个节点删掉。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void deleteNode(ListNode node) {
    	if(node.next!=null)
    	{	
    		node.val = node.next.val;
    		node.next = node.next.next;
    	}	
    	else node = null;
    }
}

100. Same Tree

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.

[思路] 虽然烦一些,不过一点一点来就好啦,该判断的就判断。(我昨天发过了,但是网不好没发出去???这草稿也是一段时间之前的好像。。)

public boolean isSameTree(TreeNode p, TreeNode q) {
    	if(p==null && q==null) return true;
    	else if(p==null || q==null) return false;
        if(p.val != q.val) return false;
        boolean flagl = false, flagr = false;
        if(p.left!=null && q.left!=null){
        	flagl = isSameTree(p.left, q.left);
        }else if(p.left==null && q.left==null){
        	flagl = true;
        }
        if(p.right!=null && q.right!=null){
        	flagr = isSameTree(p.right, q.right);
        }else if(p.right==null && q.right==null){
        	flagr = true;
        }
        return flagl & flagr;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值