LeetCode esay集錦

1. Tow Sum
2. Add Two Numbers
3. Longest Substring Without Repeating Characters

Two Sum
https://leetcode.com/problems/two-sum/
解法1:2層for循環,時間複雜度O(n*n),很容易就想到。

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int tmp1, tmp2;
        for(int i = 0; i < nums.length; i++) {
            tmp1 = nums[i];
            for (int j = i + 1;  j < nums.length; j++) {
                tmp2 = nums[j];
                if (tmp1 + tmp2 == target) {
                    //結果
                    int[] array = new int[2];
                    array[0] = i;
                    array[1] = j;
                    return array;
                }
            }
        }
        return null;
    }
}

答完題看討論還有種O(n)的解法, 利用了HashMap數據結構特性。 受到的啓發是HashMap的key也可以存值, 挑選合適的數據結構性能更佳。
解法2:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap();
        int[] result = new int[2];
        for(int i = 0; i < nums.length; i++) {
            Integer index1 = map.get(target-nums[i]);
            if (index1 != null) {
                //index1在前是因爲它在數組的前面
                result[0] = index1;
                result[1] = i;
                return result;
            }
            map.put(nums[i], i);
        }
        return null;
    }
}

Add Two Numbers
https://leetcode.com/problems/add-two-numbers/
思路1:將輸入鏈表轉換成數字後求和, 然後再創建鏈表。 缺陷:int佔4個字節、long佔8個字節, 有可能越界。 所以該方式不可行。

思路2:2個鏈表相同位置的指數相同, 例如第一個值是個位,第二個值是十位、第三個值是百位。所以2個鏈表合併成一個鏈表就是從低位到高位做加法。 我們要做的就是逐個相加,但要注意加法的進數。 第一次提交在while循環沒加tmp不等於0的條件, 導致輸入[5] [5]–[0 1]的case失敗。。。 練習算法其實也是練習邏輯的嚴謹性。

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int tmp = 0; //向高位進的數值
        int curValue = 0; //當前位的數值
        ListNode left = l1;
        ListNode right = l2;
        ListNode headNode = null;  //緩存鏈表頭節點
        ListNode lastNode = null;  //緩存鏈接末尾節點
        while(left != null || right != null || tmp != 0) {
            int curValue1 = left==null? 0:left.val;
            int curValue2 = right==null? 0:right.val;
            int tmpResult = curValue1 + curValue2 + tmp;
            tmp = tmpResult / 10;  
            curValue = tmpResult % 10;
            ListNode curNode = new ListNode(curValue);
            if (headNode == null) {
                headNode = curNode;
                lastNode = curNode;
            } else {
                lastNode.next = curNode; //新節點的next
                lastNode = curNode;
            }
            
            if (left != null) {
              left = left.next;
            }
            if (right != null) {
              right = right.next;
            }
        }
        return headNode;
    }
}

Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/
思路:比較容易想到,使用set數據結構2次循環, 遇到重複字符時跳出當前循環, 記錄最大長度。時間複雜度O(n*n)

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int longest = 0;  //最大長度
        HashSet<Character> hashSet = new HashSet(); //緩存無重複字符
        
        for (int i = 0; i < s.length(); i++) {
            hashSet.clear();
            hashSet.add(s.charAt(i)); //添加第一個字母
            for (int j = i+1; j < s.length(); j++) {
                if (hashSet.contains(s.charAt(j))) {
                    //有重複字母
                    if (longest < hashSet.size()) {
                        longest = hashSet.size();
                    }
                    break;  //退出當前層級循環
                } else {
                    //無重複字母時添加到set中
                    hashSet.add(s.charAt(j));
                }
            }
            if (longest < hashSet.size()) {
                longest = hashSet.size();
            }
        }
        return longest;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章