leetcode(1-5)

1. 兩數之和

Difficulty: 簡單

給定一個整數數組 nums 和一個目標值 target,請你在該數組中找出和爲目標值的那 兩個 整數,並返回他們的數組下標。

你可以假設每種輸入只會對應一個答案。但是,數組中同一個元素不能使用兩遍。

示例:

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

因爲 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

解題關鍵點 :HashMap、先找後存

Solution

Language: Java

//HashMap 邊存邊找
import java.util.HashMap;
class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for(int i=0; i<nums.length; i++){
            if(map.containsKey(target-nums[i])){
                res[0] = map.get(target-nums[i]);
                res[1] = i;
                return res;
            }
            map.put(nums[i],i);
        }
        return res;
    }
}

2. 兩數相加

Difficulty: 中等

給出兩個 非空 的鏈表用來表示兩個非負的整數。其中,它們各自的位數是按照 逆序 的方式存儲的,並且它們的每個節點只能存儲 一位 數字。

如果,我們將這兩個數相加起來,則會返回一個新的鏈表來表示它們的和。

您可以假設除了數字 0 之外,這兩個數都不會以 0 開頭。

示例:

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

解題關鍵點 :按位加,注意進位

Solution

Language: Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int sum=0, temp=0, x1, x2;
        ListNode head = null, t = null;
        while(l1!=null || l2!=null || temp!=0){
            if(l1==null && l2==null){
                sum = temp%10;
                temp = temp/10;
            }
            else if(l1 == null){
                sum = (l2.val+temp)%10;
                temp = (l2.val+temp)/10;
                l2 = l2.next;
            }
            else if(l2 == null){
                sum = (l1.val+temp)%10;
                temp = (l1.val+temp)/10;
                l1 = l1.next;
            }
            else{
                sum = (l1.val+l2.val+temp)%10;
                temp = (l1.val+l2.val+temp)/10;
                l1 = l1.next;
                l2 = l2.next;
            }
            ListNode n = new ListNode(sum);
            if(head == null){
                head = n;
                t = head;
            }
            else{
                t.next = n;
                t = t.next;
            }
        }
        return head;
    }
}

3. 無重複字符的最長子串

Difficulty: 中等

給定一個字符串,請你找出其中不含有重複字符的 **最長子串 **的長度。

示例 1:

輸入: "abcabcbb"
輸出: 3 
解釋: 因爲無重複字符的最長子串是 "abc",所以其長度爲 3。

示例 2:

輸入: "bbbbb"
輸出: 1
解釋: 因爲無重複字符的最長子串是 "b",所以其長度爲 1。

示例 3:

輸入: "pwwkew"
輸出: 3
解釋: 因爲無重複字符的最長子串是 "wke",所以其長度爲 3。
     請注意,你的答案必須是 子串 的長度,"pwke" 是一個子序列,不是子串。

解題關鍵點 :ArrayList存儲無重複的子串

Solution

Language: Java

class Solution {
    public int lengthOfLongestSubstring(String s) {

        if(s==null || s.length()<=0) return 0;

        int max = 0, count = 0, len = s.length();
        ArrayList<Character> list = new ArrayList<>();
        for(int i=0; i<len; i++){
            char c = s.charAt(i);
            int index = list.indexOf(c);
            if(index == -1){
                list.add(c);
                count++;
            }
            else{
                for(int j=0; j<=index; j++){
                    list.remove(0);
                }
                list.add(c);
                max = max>count? max: count;
                count = list.size();
            }
        }
        max = max>count? max: count;
        return max;
    }
}

4. 尋找兩個正序數組的中位數

Difficulty: 困難

給定兩個大小爲 m 和 n 的正序(從小到大)數組 nums1nums2

請你找出這兩個正序數組的中位數,並且要求算法的時間複雜度爲 O(log(m + n))。

你可以假設 nums1nums2 不會同時爲空。

示例 1:

nums1 = [1, 3]
nums2 = [2]

則中位數是 2.0

示例 2:

nums1 = [1, 2]
nums2 = [3, 4]

則中位數是 (2 + 3)/2 = 2.5

題目要求的複雜度,我還沒想出來。。。。。

Solution

Language: Java

5. 最長迴文子串

Difficulty: 中等

給定一個字符串 s,找到 s 中最長的迴文子串。你可以假設 s 的最大長度爲 1000。

示例 1:

輸入: "babad"
輸出: "bab"
注意: "aba" 也是一個有效答案。

示例 2:

輸入: "cbbd"
輸出: "bb"

解題關鍵點:中心拓展法

Solution

Language: Java

//中心拓展法
//第一種迴文:baab
//第二種迴文:bab
class Solution {
    public String longestPalindrome(String s) {
        String res = "";
        int count = 0, len = s.length(), low,high;
        for(int i=0; i<len; i++){
            int temp = 0;
            //如果是第一種迴文
            if(i<len-1 && s.charAt(i)==s.charAt(i+1)){
                low = i;
                high = i+1; 
                while(low>=0 && high<=len-1 && s.charAt(low)==s.charAt(high)){
                    temp += 2;
                    low--;
                    high++;
                }
                if(count < temp){
                    res = s.substring(low+1, high);
                    count = temp;
                }
            }
            //不管是否是第一種迴文,都要去判斷是否滿足第二種迴文
            low = i-1;
            high = i+1;
            temp = 1;
            while(low>=0 && high<=len-1 && s.charAt(low)==s.charAt(high)){
                temp += 2;
                low--;
                high++;
            }
            if(count < temp){
                res = s.substring(low+1, high);
                count = temp;
            }
        }
        return res;
    }

    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章