码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode刷题

时间:2020-01-26 19:02:20      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:leecode   刷题   too   ring   boolean   ext   repeat   java   exception   

1.两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
package leecode;

import java.util.HashMap;

import com.sun.org.apache.regexp.internal.recompile;

import tool.Tookit;

public class TowSum {
    // 方法1:暴力法,两层for循环
    /*public static int[] getTowSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }*/

    // 方法2:使用HashMap快速查找
    public static int[] getTowSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int competent = target - nums[i];
            if (map.containsKey(competent)) {
                return new int[] { map.get(competent), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args) {
        int[] nums = { 2, 7, 11, 15 };
        int target = 9;
        int[] res = getTowSum(nums, target);
        Tookit.printArray(res);
    }

}

技术图片

 

2.两数字相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

官方题解

package leecode;

class ListNode {
    int val;
    ListNode next;

    public ListNode(int x) {
        this.val = x;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.val).append("->");

        ListNode node = this;
        while (node.next != null) {
            node = node.next;
            sb.append(node.val);

            if (node.next != null) {
                sb.append("->");
            }
        }
        return sb.toString();
    }
}

public class AddTowNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);// 头节点
        ListNode p1 = l1, p2 = l2;
        ListNode current = dummyHead;
        int carry = 0;
        while (p1 != null || p2 != null) {
            int x1 = (p1 != null) ? p1.val : 0;
            int x2 = (p2 != null) ? p2.val : 0;
            int sum = carry + x1 + x2;
            carry = sum / 10;
            current.next = new ListNode(sum % 10);
            current = current.next;
            if (p1 != null)
                p1 = p1.next;
            if (p2 != null)
                p2 = p2.next;
        }
        if (carry > 0) {
            current.next = new ListNode(carry);
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        /*        int[] num1 = new int[] { 2, 4, 3 };
        int[] num2 = new int[] { 5, 6, 4 };
        
        ListNode l1 = new ListNode(0);
        ListNode node = l1;
        for (int i = 0; i < num1.length; i++) {
            node.val = num1[i];
            if (i < num1.length - 1) {
                node.next = new ListNode(0);
                node = node.next;
            }
        }
        System.out.println(l1);
        
        ListNode n2 = new ListNode(0);
        node = n2;
        for (int i = 0; i < num2.length; i++) {
            node.val = num2[i];
            if (i < num2.length - 1) {
                node.next = new ListNode(0);
                node = node.next;
            }
        }
        System.out.println(n2);*/
        
        ListNode l1 = new ListNode(2);
        ListNode node2 = new ListNode(4);
        ListNode node3 = new ListNode(3);
        l1.next = node2;
        node2.next = node3;
        System.out.println(l1);

        ListNode l2 = new ListNode(5);
        ListNode node5 = new ListNode(6);
        ListNode node6 = new ListNode(4);
        l2.next = node5;
        node5.next = node6;
        System.out.println(l2);
        
        ListNode res = new AddTowNumbers().addTwoNumbers(l1, l2);
        System.out.println(res);
        
    }

}

技术图片

 

3. 无重复字符的最长子串

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
     请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

官方题解

package leecode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class T03_LengthOfLongestSubstring {

    /**
     * 方法1:暴力法
     *//*
        public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {// s的所有子串
                if (allUnique(s, i, j)) {
                    ans = Math.max(ans, j - i);
                }
            }
        }
        return ans;
        }
        
        // 检查子串中是否包含相同的元素
        public boolean allUnique(String s, int start, int end) {
        Set<Character> set = new HashSet<>();
        for (int i = start; i < end; i++) {
            Character ch = s.charAt(i);
            if (set.contains(ch))
                return false;
            set.add(ch);
        }
        return true;
        }
        */
    /**
     * 方法2
     *//*
        public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))) {
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            } else {
                set.remove(s.charAt(i++));
            }
        }
        return ans;
        }*/
    /**
     * 方法3
     */
    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0, i = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int j = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(map.get(s.charAt(j)), i);
            }
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j + 1);
        }
        return ans;
    }

    public static void main(String[] args) {
        T03_LengthOfLongestSubstring s = new T03_LengthOfLongestSubstring();
        System.out.println(s.lengthOfLongestSubstring("bbbbb"));
        System.out.println(s.lengthOfLongestSubstring("abcabcbb"));
        System.out.println(s.lengthOfLongestSubstring("pwwkew"));
    }
}

技术图片

 

LeetCode刷题

标签:leecode   刷题   too   ring   boolean   ext   repeat   java   exception   

原文地址:https://www.cnblogs.com/xinmomoyan/p/12234513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!