Leetcode-Java(二)

11. Container With Most Water

这道题不是说是两根线中间所有线中的最小值决定面积,而是仅仅根据左右两根线中的最小值决定面积,所以用两个指针,一个从左一个从右来遍历数组,每次更新最小值即可。

class Solution {
    public int maxArea(int[] height) {
        int maxHeight = 0;
        int left = 0; int right = height.length-1;
        while(left < right){
            maxHeight = Math.max(maxHeight,Math.min(height[left],height[right]) * (right-left));
            if(height[left] < height[right])
                left ++;
            else
                right --;
        }
        return maxHeight;
    }
}

14. Longest Common Prefix

从前往后两两判断,具体的可以看下图:

Leetcode-Java(二)_第1张图片
class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length==0) return "";
        String prefix = strs[0];
        for(int i=1;i

15. 3Sum

Leetcode-Java(二)_第2张图片

借鉴2个数求和的思路,但是这里要注意的是,要避免重复情况的出现。

class Solution {
    public List> threeSum(int[] nums) {
        Arrays.sort(nums);
        List> res = new LinkedList<>();
        for(int i = 0;i

16. 3Sum Closest

Leetcode-Java(二)_第3张图片

思路同15题

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        int minNumber = 0xfffffff;
        int res = 0;
        Arrays.sort(nums);
        for(int i=0;i target){
                    if(minNumber > Math.abs(sum-target)){
                        minNumber = Math.abs(sum-target);
                        res = sum;
                    }
                    right --;
                }
                else{
                    if(minNumber > Math.abs(sum-target)){
                        minNumber = Math.abs(sum-target);
                        res = sum;
                    }
                    left ++;
                }
            }  
        }
        return res;
    }
}

17. Letter Combinations of a Phone Number

Leetcode-Java(二)_第4张图片

使用先进先出的队列来存储我们的返回结果。关键是ans.peek().length()==i来循环得到所有上一轮的结果,并拼接上这一次的结果。

class Solution {
    public List letterCombinations(String digits) {
        String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        LinkedList ans = new LinkedList();
        if(digits.isEmpty()) return ans;
        ans.add("");
        for(int i=0;i

18. 4Sum

Leetcode-Java(二)_第5张图片

借鉴3个数求和的思路,但是这里要注意的是,要避免重复情况的出现,同时可以添加一些提前结束循环的条件。

class Solution {
    public List> fourSum(int[] nums, int target) {
        List> res = new LinkedList<>();
        if(nums.length<4)
            return res;
        Arrays.sort(nums);
        for(int i=0;i0 && nums[i]==nums[i-1])
                continue;
            if(4 * nums[i] > target)
                return res;
            for(int j=i+1;ji+1 && nums[j]==nums[j-1])
                    continue;
                if(nums[i] + 3 * nums[j] > target)
                    break;
                int sum = target - nums[i] - nums[j];
                int left = j+1;
                int right = nums.length-1;
                while(left < right){
                    if(nums[left]+nums[right]==sum){
                        res.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        while(left

19. Remove Nth Node From End of List

Leetcode-Java(二)_第6张图片

用一个指针先往前走n步,然后两个指针同时行动,当前面的指针走到最后的时候,后面的指针所指向的位置就是倒数第n个节点的位置。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null)
            return head;
        ListNode dummy = new ListNode(-999);
        dummy.next = head;
        ListNode p = dummy;
        ListNode q = dummy;
        int i = 0;
        while(p!=null && i

20. Valid Parentheses

经典的栈的问题

class Solution {
    public boolean isValid(String s) {
        Stack stack = new Stack();
    for (char c : s.toCharArray()) {
        if (c == '(')
            stack.push(')');
        else if (c == '{')
            stack.push('}');
        else if (c == '[')
            stack.push(']');
        else if (stack.isEmpty() || stack.pop() != c)
            return false;
    }
    return stack.isEmpty();
    }
}

你可能感兴趣的:(Leetcode-Java(二))