LeetCode Top Interview Questions(101 - 145)

前言

每天刷一刷,5050發。

LeetCode Top Interview Questions(101 - 145)

101 Basic Calculator II

實現 + - * /計算

Example 1:

Input: "3+2*2"
Output: 7

Example 2:

Input: " 3/2 "
Output: 1

Example 3:

Input: " 3+5 / 2 "
Output: 5

Basic Calculator II

class Solution {
    public int calculate(String s) {
        if(s == null || s.isEmpty())return -1;
        int sum = 0, cur = 0, pre = 0;
        char sign = '+';
        char []chars = s.toCharArray();
        for(int i = 0; i <= chars.length; i ++) {
            if(i < chars.length && chars[i] == ' ')continue;

            if(i < chars.length && Character.isDigit(chars[i])) {
                cur = cur * 10 + (chars[i] - '0');
                continue;
            }
            if(sign == '+') {
                sum += cur;
                pre = cur;
            } else if(sign == '-') {
                sum -= cur;
                pre = -cur;
            } else if(sign == '*') {
                sum = sum - pre + pre * cur;
                pre = pre * cur;
            } else if(sign == '/') {
                sum = sum - pre + pre / cur;
                pre = pre / cur;
            }
            if(i < chars.length) {
                sign = chars[i];
            }
            cur = 0;
        }
        return sum;
    }
}

102 Kth Smallest Element in a BST

返回二叉搜索樹的第k小的數

Example 1:

Input: root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
   2
Output: 1

Kth Smallest Element in a BST

思路:使用非遞歸的中序排序,到第k個時返回

class Solution {
    public int kthSmallest(TreeNode root, int k) {
        if(root == null)return -1;
        
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.isEmpty()) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if(--k == 0) 
                return root.val;
            
            root = root.right;
        }
        return -1;
    }
}

103 Palindrome Linked List

判斷一個鏈表是不是迴文的

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

Palindrome Linked List

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) return true;

        ListNode pre = null;
        ListNode walker = head;
        ListNode runner = head;
        while (runner != null && runner.next != null) {
            runner = runner.next.next;

            //翻轉鏈表
            ListNode walkerNext = walker.next;
            walker.next = pre;
            pre = walker;
            walker = walkerNext;
        }
        //說明爲奇數個
        if (runner != null) {
            walker = walker.next;
        }
        while (pre != null) {
            if (walker.val != pre.val)
                return false;
            walker = walker.next;
            pre = pre.next;
        }
        return true;
    }
}

104 Lowest Common Ancestor of a Binary Tree

給定二叉樹,找到最低的共同祖先

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Lowest Common Ancestor of a Binary Tree

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //第一步:找到兩個節點 並返回
        if(root == null || p == root || q == root)return root;
        
        //如果當前節點不是要找的
        //進行左右子樹查找
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        
        //如果左右子樹都包含有目標節點,則當前節點就是交匯節點
        if(left != null && right != null)return root;  
        
        //如果左右子樹只找到一個,說明兩個節點在同一邊,則返回最先找到都節點即可
        //因爲是自上往下找的
        //符合第二例子,返回其中之一
        return left == null ? right : left;
    }
}

105 Delete Node in a Linked List

編寫一個函數以刪除單鏈接列表中的節點(尾部除外),僅授予對該節點的訪問權限。

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.

Delete Node in a Linked List

//第一個問題,刪除節點一定要有它的前置節點嗎?
//答案是必須的,但是題目沒提供,我們需要自己去創建出來
//只需要把上一個節點的值給到要刪除的節點的值,把上一個節點刪除即可
class Solution {
    public void deleteNode(ListNode node) {
        if(node.next == null)node = null;
        
        node.val = node.next.val;
        node.next = node.next.next;
    }
}

106 Product of Array Except Self

給定一個由n個整數組成的數組num,其中n> 1,則返回一個數組輸出,使得output [i]等於除nums [i]之外所有nums元素的乘積。

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

Product of Array Except Self

class Solution {
    public int[] productExceptSelf(int[] nums) {
        
        if(nums == null || nums.length == 0)return null;
        
        int res[] = new int[nums.length];
        
        //res = [1, 1 * 1, 1 * 1 * 2, 1 * 1 * 2 * 3]
        res[0] = 1;
        for(int i = 1; i < nums.length; i ++) {
            res[i] = res[i - 1] * nums[i - 1];
        }
        
        int tmp = 1;
        
        for(int i = nums.length - 1; i >= 0; i --) {
            res[i] = res[i] * tmp;
            tmp *= nums[i];
        }
        return res;
    }
}

107 Sliding Window Maximum

給定一個數組num,存在一個大小爲k的滑動窗口,該窗口從數組的最左邊移到最右邊。您只能在窗口中看到k個數字。每次滑動窗口向右移動一個位置。返回最大滑動窗口。

Example:

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7] 
Explanation: 

Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

Sliding Window Maximum

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums.length == 0)return new int[]{};
        int []res = new int[nums.length - k + 1];
        int index = 0;
        Deque<Integer> dq = new LinkedList<>();
        
        for(int i = 0; i < nums.length; i ++) {
            //存儲最大值和其次
            while(!dq.isEmpty() && nums[dq.peekLast()] < nums[i])
                dq.pollLast();
            
            dq.addLast(i);
            
            //彈出過期數據
            while(i - dq.peek() >= k)
                dq.pollFirst();
            
            if(i + 1 >= k) {
                res[index ++] = nums[dq.peek()];
            }
        }
        return res;
    }
}

108 Search a 2D Matrix II

編寫一種有效的算法,以在m x n矩陣中搜索值。該矩陣具有以下屬性:每行中的整數按從左到右的升序排列。每列中的整數按從上到下的升序排列。

Example:

Consider the following matrix:

[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

Search a 2D Matrix II

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length == 0)return false;
        int i = 0, j = matrix[0].length - 1;
        while(i < matrix.length && j >= 0) {
            if(matrix[i][j] == target) return true;
            else if(matrix[i][j] < target) i ++;
            else j--;
        }
        return false;
    }
}

109 Valid Anagram

給定兩個字符串s和t,編寫一個函數來確定t是否是s的字謎。

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

Valid Anagram

class Solution {
    public boolean isAnagram(String s, String t) {

        if (s.length() != t.length()) return false;
        int[] letters = new int[128];
        for (char ch : s.toCharArray()) {
            letters[(int) ch]++;
        }
        for (char ch : t.toCharArray()) {
            letters[(int) ch]--;
        }

        for (int i : letters) {
            if (i != 0)
                return false;
        }
        return true;
    }
}

110 Flatten 2D Vector(VIP)

Flatten 2D Vector


111 Meeting Rooms II(VIP)

Meeting Rooms II


112 Missing Number

給定一個數組,其中包含從0、1、2,…,n中提取的n個不同的數字,請找到該數組中缺少的一個。

Missing Number

class Solution {
    public int missingNumber(int[] nums) {
        int res = nums.length;
        for(int i = 0; i < nums.length; i ++){
            res ^= i ^ nums[i];
        }
        return res;
    }
}

113 Alien Dictionary(VIP)

Alien Dictionary


114 Find the Celebrity(VIP)

Find the Celebrity


115 Perfect Squares

給定一個正整數n,求和爲n的最小平方數(例如1、4、9、16,…)。

Example 1:

Input: n = 12
Output: 3 
Explanation: 12 = 4 + 4 + 4.

Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.

Perfect Squares

class Solution {
    public int numSquares(int n) {
        if(n < 2)return n;
        
        int []memo = new int[n + 1];
        Arrays.fill(memo, Integer.MAX_VALUE);
        memo[0] = 0;
        for(int i = 1; i <= n; i ++) {
            for(int j = 1; j * j <= i; j ++) {
                memo[i] = Math.min(memo[i], memo[i - j * j] + 1);
            }
        }
        return memo[n];
    }
}

116 Move Zeroes

給定一個數組num,編寫一個函數,將所有0移到它的末尾,同時保持非零元素的相對順序。

Example:

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

Move Zeroes

class Solution {
    public void moveZeroes(int[] nums) {
        if(nums.length == 0)return;
        int index = 0;
        
        for(int i = 0; i < nums.length; i ++) {
            if(nums[i] != 0) {
                nums[index ++] = nums[i];
            }
        }
        
        for(int i = index; i < nums.length; i ++) {
            nums[i] = 0;
        }
        
    }
}

117 Inorder Successor in BST(VIP)

Inorder Successor in BST


118 Find the Duplicate Number

給定一個包含n + 1個整數的數組,其中每個整數在1到n(含)之間,存在一個重複數。假定只有一個重複的數字,找到重複的一個。

Example:

Input: [1,3,4,2,2]
Output: 2

Find the Duplicate Number

class Solution {
    public int findDuplicate(int[] nums) {
        if(nums.length == 0)return -1;
        
        for(int i = 0; i < nums.length; i ++) {
            if(nums[i] - 1 == i)continue;
            
            int tmp = nums[i];
            if(nums[tmp - 1] == tmp)return tmp;
            nums[i] = nums[tmp - 1];
            nums[tmp - 1] = tmp;
            i --;
        }
        return -1;
    }
}

119 Game of Life

Game of Life


120 Find Median from Data Stream

中位數是有序整數列表中的中間值。如果列表的大小是偶數,則沒有中間值。因此,中位數是兩個中間值的平均值。

Example

[2,3,4], the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5

Find Median from Data Stream

class MedianFinder {

    /** initialize your data structure here. */
    PriorityQueue<Integer> min;
    PriorityQueue<Integer> max;
    
    public MedianFinder() {
        min = new PriorityQueue<>();
        max = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
    }
    
    public void addNum(int num) {
        if(min.isEmpty() || min.peek() <= num)
          min.add(num);
        else max.add(num);

        if(max.size() + 2 == min.size())
          max.add(min.poll());

        if(min.size() + 1== max.size())
          min.add(max.poll());
		}
    
    public double findMedian() {
        return min.size() == max.size() ?
            (min.peek() + max.peek()) / 2.0 : 1.0 * min.peek();
    }
}

121 Serialize and Deserialize Binary Tree

序列化和反序列化二叉樹

Example:

You may serialize the following tree:

    1
   / \
  2   3
     / \
    4   5

as "[1,2,3,null,null,4,5]"

Serialize and Deserialize Binary Tree

public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuffer sb = new StringBuffer();
        if(root == null) {
            sb.append("#,");
            return sb.toString();
        }
        sb.append(root.val + ",");
        sb.append(serialize(root.left));
        sb.append(serialize(root.right));
        return sb.toString();
    }
    int index = -1;
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String []res = data.split(",");
        TreeNode root = null;
        if(!res[++ index].equals("#")) {
            root = new TreeNode(Integer.parseInt(res[index]));
            root.left = deserialize(data);
            root.right = deserialize(data);
        }
        return root;                                    
    }
}

122 Longest Increasing Subsequence

給定一個未排序的整數數組,請找到最長遞增子序列的長度

Example:

Input: [10,9,2,5,3,7,101,18]
Output: 4 
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.

Longest Increasing Subsequence

class Solution {
    public int lengthOfLIS(int[] nums) {
        if(nums.length == 0)return 0;
        
        int memo[] = new int[nums.length];
        Arrays.fill(memo, 1);
        int res = 1;
        for(int i = 1; i < nums.length; i ++) {
            for(int j = 0; j < i; j ++) {
                if(nums[i] > nums[j]) {
                    memo[i] = Math.max(memo[i], memo[j] + 1);
                }
                    
            }
        }
        for(int i = 0; i < memo.length; i ++) {
            res = Math.max(res, memo[i]);
        }
        return res;
    }
}

123 Range Sum Query 2D - Mutable(VIP)

Range Sum Query 2D - Mutable


124 Count of Smaller Numbers After Self

您將得到一個整數數組nums,並且必須返回一個新的counts數組。counts數組具有以下屬性:counts [i]是nums [i]右側較小元素的數量。

Example:

Input: [5,2,6,1]
Output: [2,1,1,0] 
Explanation:
To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.

Count of Smaller Numbers After Self


125 Coin Change

您會得到不同面額的硬幣和總金額。編寫一個函數來計算組成該數量所需的最少數量的硬幣。如果這筆錢不能用硬幣的任何組合來彌補,請返回-1。

Example 1:

Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Coin Change

  • 記憶化搜索
public int coinChange(int[] coins, int amount) {
		if (coins.length == 0) return -1;

		int memo[] = new int[amount];

		return helper(coins, amount, memo);
}

private int helper(int[] coins, int amount, int[] memo) {
  	if (amount < 0) return -1;
  	if (amount == 0) return 0;
  	if (memo[amount - 1] != 0) return memo[amount - 1];
  	int min = Integer.MAX_VALUE;
  	int res = 0;
  	for (int i = 0; i < coins.length; i++) {
    	res = helper(coins, amount - coins[i], memo);

    	if (res >= 0)
      min = Math.min(min, res + 1);
  	}

  	memo[amount - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
  	return memo[amount - 1];
}
  • 動態規劃
public int coinChange(int[] coins, int amount) {
  	if (coins.length == 0) return -1;

  	int memo[] = new int[amount + 1];
  	Arrays.fill(memo, amount + 1);
  	memo[0] = 0;
  	for (int i = 1; i <= amount; i++) {
    		for (int c : coins) {
            if (i - c >= 0) {
                memo[i] = Math.min(memo[i], memo[i - c] + 1);
            }
    		}
  	}

  	return memo[amount] > amount ? -1 : memo[amount];
}

126 Wiggle Sort II

給定未排序的數組num,請對其重新排序,以使nums [0] <nums [1]> nums [2] <nums [3]…。

Example 1:

Input: nums = [1, 5, 1, 1, 6, 4]
Output: One possible answer is [1, 4, 1, 5, 1, 6].

Wiggle Sort II

class Solution {
  	public void wiggleSort(int[] nums) {
    		Arrays.sort(nums);
    		int[] arr = Arrays.copyOf(nums, nums.length);
    		int j = nums.length - 1;
    		for (int i = 1; i < nums.length; i += 2) {
      			nums[i] = arr[j];
      			j--;
        }
    		for (int i = 0; i < nums.length; i += 2) {
      			nums[i] = arr[j];
      			j--;
    		}
  	}
}

127 Power of Three

給定一個整數,編寫一個函數以確定它是否爲三的冪。

Power of Three

image-20200318135503573

public class Solution {
    public boolean isPowerOfThree(int n) {
        return (Math.log10(n) / Math.log10(3)) % 1 == 0;
    }
}

128 Odd Even Linked List

給定一個單鏈表,將所有奇數節點組合在一起,然後是偶數節點。請注意,這裏我們談論的是節點號,而不是節點中的值。您應該嘗試就地進行。該程序應在O(1)空間複雜度和O(節點)時間複雜度下運行。

Odd Even Linked List

class Solution {
    public ListNode oddEvenList(ListNode head) {
        if(head == null)return null;
        if(head.next == null)return head;
        
        ListNode odd = head;
        ListNode even = head.next;
        ListNode evenHead = even;
        
        while(even != null && even.next != null){
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}

129 Longest Increasing Path in a Matrix

給定一個整數矩陣,找到最長增加路徑的長度。從每個單元格,您可以向四個方向移動:左,右,上或下。您不能對角移動或移動到邊界之外(即不允許環繞)。

Longest Increasing Path in a Matrix

class Solution {
    int m,n;
    boolean[][] visited;//存儲是否訪問
    int[][] memo;//存儲從這個節點開始的最大路徑
    public int longestIncreasingPath(int[][] matrix) {
        if(matrix.length == 0)return 0;
        m = matrix.length;
        n = matrix[0].length;
        visited = new boolean[m][n];
        memo = new int[m][n];
        int res = 0;
        for(int i = 0; i < m; i ++) {
            for(int j = 0; j < n; j ++) {
                //如果這個位置已經計算過了,直接比較
                if(memo[i][j] != 0) {
                    res = Math.max(memo[i][j], res);
                } else {
                    //尋找最大值
                    res = Math.max(res, findPath(matrix, i, j));
                }
            }
        }
        return res;
    }
    
    private int findPath(int[][] matrix, int x, int y) {
        if(visited[x][y] == true)return 0;
        
        if(memo[x][y] != 0)return memo[x][y];
        //如果都不能走都情況就是1
        int res = 1;
        //上下左右走
        visited[x][y] = true;
        if(x < m - 1 && matrix[x][y] < matrix[x + 1][y])
            res = Math.max(res, findPath(matrix, x + 1, y) + 1);
        if(x > 0 && matrix[x][y] < matrix[x - 1][y])
            res = Math.max(res, findPath(matrix, x - 1, y) + 1);
        if(y < n - 1 && matrix[x][y] < matrix[x][y + 1])
            res = Math.max(res, findPath(matrix, x, y + 1) + 1);
        if(y > 0 && matrix[x][y] < matrix[x][y - 1])
            res = Math.max(res, findPath(matrix, x, y - 1) + 1);
        visited[x][y] = false;
        
        //回溯時記錄走過節點都最大值
        memo[x][y] = res;
        return res;
    }
}

130 Increasing Triplet Subsequence

給定一個未排序的數組,返回數組中是否存在長度爲3的遞增子序列。

Example 1:

Input: [1,2,3,4,5]
Output: true

Increasing Triplet Subsequence

class Solution {
    public boolean increasingTriplet(int[] nums) {
        if (nums.length < 3) return false;
        int i = Integer.MAX_VALUE, j = i, k = i;
        boolean flagI = false, flagJ = flagI, flagK = flagI;//表示三個位置自滿足的情況
        //由於位置從小到大賦值,所以滿足  0 ≤ i的位置 < j的位置 < k的位置 ≤ n-1
        for (int q = 0; q < nums.length; q++) {
            //i 只要滿足位置不是倒數1,2即可
            if (nums[q] < i && nums.length - q > 2) {
                i = nums[q];
                flagI = true;
            }
            //j 需要滿足大於 i 的值 且位置不是倒數 1
            else if (nums[q] > i && nums[q] < j && nums.length - q > 1) {
                j = nums[q];
                flagJ = true;
            }
            //k 需要滿足大於 j 且小於 INTEGER.MAX_INTEGER
            else if (nums[q] > j && nums[q] < k) {
                k = nums[q];
                flagK = true;
              	break;
            }
        }
        return flagI && flagJ && flagK ? i < j && j < k : false;
    }
}

131 Longest Substring with At Most K Distinct Characters(VIP)

Longest Substring with At Most K Distinct Characters


132 Flatten Nested List Iterator

給定一個嵌套的整數列表,請實現一個迭代器以使其扁平化。每個元素可以是整數,也可以是列表-其元素也可以是整數或其他列表。

Flatten Nested List Iterator

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
public class NestedIterator implements Iterator<Integer> {

    ArrayList<Integer> list = new ArrayList<>();
    int index = 0;

    public NestedIterator(List<NestedInteger> nestedList) {
        init(nestedList);
    }

    public void init(List<NestedInteger> nestedList) {
        for (NestedInteger n : nestedList) {
            addNestedInteger(n);
        }
    }

    public void addNestedInteger(NestedInteger n) {
        if (n.isInteger()) {
            list.add(n.getInteger());
            return;
        }
        init(n.getList());
    }

    @Override
    public Integer next() {
        return list.get(index++);
    }

    @Override
    public boolean hasNext() {
        return index < list.size() ? true : false;
    }
}

133 Reverse String

反轉字符串

Reverse String

class Solution {
    public void reverseString(char[] s) {
        for (var i = 0; i < s.Length / 2; i++) {
            var tmp = s[i];
            s[i] = s[s.Length - i - 1];
            s[s.Length - i - 1] = tmp;
        }
    }
}

134 Top K Frequent Elements

給定一個非空的整數數組,返回k個最頻繁的元素。

思路:使用最大堆

Top K Frequent Elements

class Solution {
    public List<Integer> topKFrequent(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int count = map.getOrDefault(nums[i], 0);
            map.put(nums[i], count + 1);
        }
        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                return map.get(b) - map.get(a);
            }
        });
        for (int key : map.keySet()) {
            pq.add(key);
        }
        LinkedList<Integer> list = new LinkedList<>();
        while (k-- > 0) {
            list.add(pq.remove());
        }
        return list;
    }
}

135 Design Tic-Tac-Toe(VIP)

Design Tic-Tac-Toe


136 Intersection of Two Arrays II

給定兩個數組,編寫一個函數來計算它們的交集。

Intersection of Two Arrays II

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int[] result = new int[Math.min(nums1.length, nums2.length)];
        for (int i = 0; i < nums1.length; i++) {
            int count = map.getOrDefault(nums1[i], 0);
            map.put(nums1[i], count + 1);
        }
        int j = 0;
        for (int i = 0; i < nums2.length; i++) {
            if (map.get(nums2[i]) != null && map.get(nums2[i]) > 0) {
                map.put(nums2[i], map.get(nums2[i]) - 1);
                result[j++] = nums2[i];
            }
        }
        return Arrays.copyOfRange(result, 0, j);
    }
}

137 Sum of Two Integers

計算兩個整數a和b的總和,但不允許使用+和-運算符。

Sum of Two Integers

class Solution {
    public int getSum(int a, int b) {
        if(b == 0)return a;
        int carry = a & b;
        return getSum(a ^ b, carry << 1);
    }
}

138 Kth Smallest Element in a Sorted Matrix

給定一個n x n矩陣,其中每個行和列都按升序排序,請找到矩陣中第k個最小的元素。請注意,它是排序順序中第k個最小的元素,而不是第k個不同的元素。

Example:

matrix = [
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.

Kth Smallest Element in a Sorted Matrix

class Solution {
    private int count(int[][] a, int target) {
        int i = a.length - 1, j = 0;
        int c = 0;
        while (i >= 0 && j < a.length) {
            if (a[i][j] <= target) {
                c += i + 1;
                j++;
            } else i--;
        }
        return c;
    }

    public int kthSmallest(int[][] matrix, int k) {
        int l = matrix[0][0], h = matrix[matrix.length - 1][matrix.length - 1];
        while (l < h) {
            int mid = l + ((h - l) >> 1);
            if (count(matrix, mid) < k) l = mid + 1;
            else h = mid;
        }
        return h;
    }
}

139 Insert Delete GetRandom O(1)

設計一個平均0(1)次支持所有後續操作的數據結構。

插入(val):如果項目val尚未存在,則將其插入到集合中。

移除(val):從集合中移除項目val(如果存在)。

getRandom:從當前元素集中返回一個隨機元素。每個元素必須具有相同的返回概率。

Insert Delete GetRandom O(1)

class RandomizedSet {

    /** Initialize your data structure here. */
    HashMap<Integer,Integer> map;
    ArrayList<Integer> list;
    public RandomizedSet() {
        map = new HashMap<>();
        list = new ArrayList<>();
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    //插入使用HashMap  O(1)
    public boolean insert(int val) {
        if(map.containsKey(val))return false;
        map.put(val,list.size());
        list.add(val);
        return true;
    }
    //刪除的時候判斷是否是最後一個,如果不是最後一個
    //把最後一個元素和要刪除的元素下標對換位置
    //然後刪除List的最後一個 O(1)
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        if(!map.containsKey(val))return false;
        if(map.get(val) < list.size()){
            int index = map.get(val);
            list.set(index,list.get(list.size() -  1));
            map.put(list.get(list.size() -  1),index);
        }
        map.remove(val);
        list.remove(list.size() - 1);
        return true;
    }
    //隨機生成數用list取是O(1)
    /** Get a random element from the set. */
    public int getRandom() {
        Random random = new Random();
        return list.get(random.nextInt(list.size()));
    }
}

140 Shuffle an Array

洗牌的一組數字不重複。

Example:

//初始化集合爲1、2和3的數組。
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

//洗牌數組[1,2,3]並返回其結果。[1,2,3]的任何置換必須同樣可能被返回。
solution.shuffle();

//將數組重置回其原始配置[1,2,3]。
solution.reset();

//返回數組[1,2,3]的隨機洗牌。
solution.shuffle();

Shuffle an Array

思路:主要問題時如何返回一個隨機的數組

class Solution {
    private int[] array;
    private int[] original;

    Random rand = new Random();

    private int randRange(int min, int max) {
        return rand.nextInt(max - min) + min;
    }

    private void swapAt(int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public Solution(int[] nums) {
        array = nums;
        original = nums.clone();
    }
    
    public int[] reset() {
        array = original;
        original = original.clone();
        return original;
    }
    
    public int[] shuffle() {
        for (int i = 0; i < array.length; i++) {
            //該位置的數和數組後的數位置隨機交換
            //因爲從頭開始,所有我們可以認爲前面的數已經是隨機過了的
            swapAt(i, randRange(i, array.length));
        }
        return array;
    }
}

141 First Unique Character in a String

找到字符串第一個不重複的字符,如果不存在則返回-1.注意:返回的是字符的位置

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

First Unique Character in a String

class Solution {
    public int firstUniqChar(String s) {
        int let[] = new int[26];
        for(int i = 0; i < s.length(); i ++){
            let[s.charAt(i) - 'a']++;
        }
        for(int i = 0; i < s.length(); i ++){
            if(let[s.charAt(i) - 'a'] == 1)return i;
        }
        return -1;
    }
}

142 Longest Substring with At Least K Repeating Characters

查找給定字符串的最長子字符串T的長度(僅由小寫字母組成),使T中的每個字符出現不少於k次。

Example 1:

Input:
s = "aaabb", k = 3

Output:
3

The longest substring is "aaa", as 'a' is repeated 3 times.

Example 2:

Input:
s = "ababbc", k = 2

Output:
5

The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.

Longest Substring with At Least K Repeating Characters

class Solution {
    public int longestSubstring(String s, int k) {
        if(s.length() < k || k == 0)return 0;
        
        int []arr = new int[26];
        for(int i = 0; i < s.length(); i ++){
            arr[s.charAt(i) - 'a'] ++;
        }
        
        boolean flag = true;
        for(int i = 0; i < s.length(); i ++)
            if(arr[s.charAt(i) - 'a'] < k){
                flag = false;
                break;
            }
        if(flag)return s.length();
        
        
        int idx = 0;int start = 0;int res = 0;
        while(idx < s.length()){
            if(arr[s.charAt(idx) - 'a'] < k){
                res = Math.max(res,longestSubstring(s.substring(start,idx),k));
                start = idx + 1;            
            }
            idx ++;
        }
                
        return Math.max(res,longestSubstring(s.substring(start,idx),k));
    }
}

143 Fizz Buzz

Fizz Buzz

編寫一個程序,輸出從1到n的數字的字符串表示形式。但是對於三的倍數,它應該輸出“ Fizz”而不是數字,對於五的倍數,它應該輸出“嗡嗡聲”。對於三和五的倍數的數字,輸出“ FizzBuzz”。

class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> list = new ArrayList<>();
        if(n == 0) return list;
        for(int i = 1; i <= n; i ++){
            if(i % 3 == 0 && i % 5 == 0)
                list.add("FizzBuzz");
            else if(i % 3 == 0)
                list.add("Fizz");
            else if(i % 5 == 0)
                list.add("Buzz");
            else
                list.add(i + "");
        }
        return list;
    }
}

144 4Sum II

給定四個具有整數值的列表A,B,C,D,計算有多少個元組(i,j,k,l),使得A [i] + B [j] + C [k] + D [l]

Example:

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]

Output:
2

Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

4Sum II

class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        int count = 0;
        Map<Integer, Integer> map = new HashMap<>(A.length*A.length);
        for (int a: A) {
            for (int b: B) { 
                map.put(a+b, map.getOrDefault(a+b, 0) + 1);
            }
        }
        for (int c: C) {
            for (int d: D) { 
                if (map.containsKey(-c-d))
                    count += map.get(-c-d);
            }
        }
        return count;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章