19. Remove Nth Node From End of List 06/23/2017 Be A Better Programmer " Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one pass. " Solution: 12345678910111213141516171819202122232425262728293031/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { Map<Integer, ListNode> map = new HashMap<Integer, ListNode>(); ListNode t = new ListNode(0); t.next = head; int i = 1; while (t.next != null) { map.put(i, t.next); i++; t = t.next; } if (head.next == null) { return head.next; } else if (!map.containsKey(i - n - 1)) { head = map.get(i - n + 1); } else if (!map.containsKey(i - n + 1)) { map.get(i - n - 1).next = null; } else { map.get(i - n - 1).next = map.get(i - n + 1); } return head; }} 链表注意考虑既是头又是尾,头,尾,中间等情况
11. Container With Most Water 06/16/2017 Be A Better Programmer 1.brute (TLE) 1234567891011121314public class Solution { public int maxArea(int[] height) { int n=height.length; int maxarea=0; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ int s=Math.min(height[j],height[i])*(j-i); if(s>maxarea){ maxarea=s; } } } return maxarea; }}