leetCode解题报告5道题(一)

比较简单的几道题,不做详细的解释,只为之后可以回头看看自己之前的代码!!

虽然几道题都比较简单,但感觉自己写得不好,希望博文如果被哪位大神看到,可以留言下写下你的做法!


题目一:

Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

AC代码:

public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
		if (head == null)
			return null;
		if (m == n)
        	return head;
        ListNode pHead = new ListNode(0);
        pHead.next = head;
        ListNode preMNode = getListNode(pHead, m-1);
        ListNode MNode = getListNode(pHead, m);
        ListNode preNode = preMNode;
        
        int len = n-m;
        for (int i=0; i<=len; ++i){
        	ListNode MNodeNext = MNode.next;
        	MNode.next = preNode;
        	preNode = MNode;
        	MNode = MNodeNext;
        }
        
        preMNode.next.next = MNode;
        preMNode.next = preNode;
        
		return pHead.next;
    }
	public ListNode getListNode(ListNode startNode, int index){
		for (int i=0; i<index; ++i){
			startNode = startNode.next;
		}
		return startNode;
	}
	
	
}

题目二:

Binary Tree Inorder Traversal

 (考察的其实是栈的运用,非递归遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

AC代码:

public class Solution {
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> arrays = new ArrayList<Integer>();
        if (root == null)
            return arrays;
       
        
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        
        while (!stack.empty()){
            TreeNode topNode = stack.peek();
            if (topNode.left != null){
                stack.push(topNode.left);
                topNode.left = null;
                continue;
            }
            
            stack.pop();
            arrays.add(topNode.val);
            
            if (topNode.right != null){
                stack.push(topNode.right);
            }
            
        }
        return arrays;
    }
}

题目三:

Merge Two Sorted Lists (关于两个已经排好序的链表的合并)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

AC代码:

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
		ListNode list = new ListNode(0);
		ListNode newlist = list;
		if (l1 == null){
			list = l2;
			return list;
		}
		if (l2 == null){
			list = l1;
			return list;
		}
		while (l1 != null && l2 != null){
			if (l1.val < l2.val){
				list.next = l1;
				list = list.next;
				l1 = l1.next;
			}else{
				list.next = l2;
				list = list.next;
				l2 = l2.next;
			}
		}
		if (l1 != null){
			list.next = l1;
		}
		if (l2 != null){
			list.next = l2;
		}
        return newlist.next;
    }
}


题目四:

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.


AC代码:


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


题目五:

Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that yourreturned answers (both index1 and index2) are not zero-based.

You may assume that each input wouldhave exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2


由于这个题目中已经说了,只有一个解,所以我们不用考虑多个解的情况了。

思路:由于暴力的方法会TLE,所以我采用了把值当成key放入到map中,而下标作为value放入map中,map.put(numbers[i], i)。

这样预处理之后,再用个for循环,把target - numbers[i] 作为 key 去map里查找“元素”,如果找到的话,判断该“元素”的value域的值是否和当前的i相等(如果不判断会WA,比如3+3=6,返回index1 = index2 = i就错了),不相等就对应把两个下标值+1赋值给index1, index2。(题目要求: returned answers (both index1 and index2) are not zero-based.


public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        /*
        暴力法:会TLE哦!!!
        Integer[][] sum = new Integer[numbers.length+1][numbers.length+1];
        int index1 = 0,index2 = 0;
        for (int i=0; i<numbers.length; ++i){
            sum[0][i+1] = numbers[i];
        }
        for (int i=1; i<=numbers.length; ++i){
            for (int j=i+1; j<=numbers.length; ++j){
                sum[i][j] = sum[0][i] + sum[0][j];
                if (sum[i][j] == target){
                    index1 = i;
                    index2 = j;
                    break;
                }
            }
        }
        if (index1 == 0 && index2 == 0)
            return null;
        return new int[]{index1,index2};*/
        
        /*AC代码:*/
        int index1 = 0,index2 = 0;
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        for (int i=0; i<numbers.length; ++i){
            map.put(numbers[i],i);
        }
        for (int i=0; i<numbers.length; ++i){
            int key = target - numbers[i];
            
            if (map.containsKey(key)){
                int temp = map.get(key);
                if (temp != i){
                    index1 = i+1;
                    index2 = temp+1;
                    break;
                }
            }
        }
        if (index1 == 0 && index2 == 0)
            return null;
        return new int[]{index1,index2};
    }
}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页