Giter VIP home page Giter VIP logo

llancelot / leetcode Goto Github PK

View Code? Open in Web Editor NEW
161.0 161.0 38.0 1.38 MB

✍️ My LeetCode solutions, ideas and templates sharing. (我的LeetCode题解,思路以及各专题的解题模板分享。分专题归纳,见tag)

Home Page: https://leetcode.com/lancelot_/

Python 13.34% Java 84.15% C++ 2.51%
data-structures-and-algorithms interview-questions leetcode leetcode-solutions top-100-liked-questions top-interview-questions

leetcode's Introduction

Hi there 👋

  • 🔭 I’m currently working on ... Java, Python, Flask, Spring Boot, MongoDB, MySQL
  • 🌱 I’m currently learning ... Javascript, Node.JS, Docker and AWS
  • 👯 I’m looking to collaborate on any back-end projects!
  • 💬 Please feel free to contact me through email

Welcome Open Source Love visitor Count

Adit's GitHub Stats

leetcode's People

Contributors

llancelot avatar ytp327 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

leetcode's Issues

77. Combination

组合问题

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Example:

Input: n = 4, k = 2
Output:
[
 [2,4],
 [3,4],
 [2,3],
 [1,2],
 [1,3],
 [1,4],
]
class Solution(object):
    def combine(self, n, k):
        # init first combination
        nums = list(range(1, k + 1)) + [n + 1]
        
        output, j = [], 0
        
        while j < k:
            # add current combination
            output.append(nums[:k])
            # increase first nums[j] by one
            # if nums[j] + 1 != nums[j + 1]
            j = 0
            while j < k and nums[j + 1] == nums[j] + 1:
                nums[j] = j + 1
                j += 1
            nums[j] += 1

        return output

93. Restore IP Addresses

https://leetcode.com/problems/restore-ip-addresses/

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

A valid IP address consists of exactly four integers (each integer is between 0 and 255) separated by single points.

Example:

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]

class Solution(object):
    def restoreIpAddresses(self, s):
        
        def dfs(res, s, curr, field):
            if field == 4 and s == "":
                res.append(curr[1:])
            elif field == 4 or s == "":
                return 
            else:
                # add 1 digit into curr, then dfs
                dfs(res, s[1:], curr + "." + s[0:1], field + 1)
                
                # add 2 digits into curr, then dfs
                if s[0] != "0" and len(s) > 1:
                    dfs(res, s[2:], curr + "." + s[0:2], field + 1)
                    
                    # add 3 digits into curr, then dfs
                    if len(s) > 2 and int(s[0:3]) <= 255:
                        dfs(res, s[3:], curr + "." + s[0:3], field + 1)
         
        res=[]
        if not s or len(s) > 12:
            return res
        
        dfs(res, s, "", 0)
        return res

1192. Critical Connections in a Network

There are n servers numbered from 0 to n-1 connected by undirected server-to-server connections forming a network where connections[i] = [a, b] represents a connection between servers a and b. Any server can reach any other server directly or indirectly through the network.

A critical connection is a connection that, if removed, will make some server unable to reach some other server.

Return all critical connections in the network in any order.

Example 1:

img

Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
Output: [[1,3]]
Explanation: [[3,1]] is also accepted.

105. Construct Binary Tree from Preorder and Inorder Traversal

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

// root->left>right, preorder = [3,9,20,15,7]
// left->root->right, inorder = [9,3,15,20,7]
class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        HashMap<Integer, Integer> map = new HashMap<>();
        
        // HashMap to store indicies(9,0),(3,1),(15,2)...
        for(int i=0;i<inorder.length;i++)
            map.put(inorder[i], i);
        
        return build(preorder, inorder, 0, 0, inorder.length-1, map);
    }
    
    private TreeNode build(int[] preorder, int[] inorder,
                           int preBegin, int inBegin, int inEnd, HashMap<Integer, Integer> map){
        // return case
        if (preBegin >= preorder.length || inBegin > inEnd)
            return null;
        TreeNode root = new TreeNode(preorder[preBegin]);
        // root_index: get root num index from inorder(map)
        int root_index = map.get(preorder[preBegin]);
        // build the tree
        root.left = build(preorder, inorder, preBegin+1, inBegin, root_index-1, map);
        root.right = build(preorder, inorder, preBegin + root_index-inBegin+1, root_index+1, inEnd, map);
        
        return root;
      }
}

362. Design Hit Counter

Design a hit counter which counts the number of hits received in the past 5 minutes.

Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1.

It is possible that several hits arrive roughly at the same time.

Example:

HitCounter counter = new HitCounter();

// hit at timestamp 1.
counter.hit(1);

// hit at timestamp 2.
counter.hit(2);

// hit at timestamp 3.
counter.hit(3);

// get hits at timestamp 4, should return 3.
counter.getHits(4);

// hit at timestamp 300.
counter.hit(300);

// get hits at timestamp 300, should return 4.
counter.getHits(300);

// get hits at timestamp 301, should return 3.
counter.getHits(301);

767. Reorganize String

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.

If possible, output any possible result. If not possible, return the empty string.

Example 1:

Input: S = "aab"
Output: "aba"

Example 2:

Input: S = "aaab"
Output: ""

思路:

  • 这道题的解法有多种,Heap, PriorityQueue, Greedy 等等。我的解法是,先把出现次数最多的字符ch找到,再把ch全部放在result中的第0, 2, 4, 6...等位置上,保证剩下的待插入字符可以安排在第1, 3, 5 ... 的位置。index 初始化为0,步长为2,即 index += 2。
  • 插入字符的过程可以理解成:先将偶数下标的位置填满,再把剩下的奇数填满。用index记录插入字符在result中的下标,类似循环队列,当index超过result的size时,将index = 1,保证可以填满整个result.
  • 关于记录每个字符出现的次数,这里用int[] hash数组(长度26,对应每个英文字符)来统计。

代码:

class Solution {
    public String reorganizeString(String S) {
        int[] hash = new int[26]; // hash.length = 26
        for(int i = 0; i < S.length(); i++) {
            hash[S.charAt(i) - 'a']++;
        }
        int max = 0, letter = 0;
        for (int i = 0; i < hash.length; i++) {
            if (hash[i] > max) {
                max = hash[i];
                letter = i;
            }
        }
        if (max > (S.length() + 1) / 2) {
            return "";
        }
        char[] res = new char[S.length()];
        int idx = 0;

        // 先把最高频字母安排在0,2,4,6....等位置上
        while (hash[letter] > 0) {
            res[idx] = (char) (letter + 'a');
            idx += 2;
            hash[letter]--; 
        }

        // 对剩下的字母,分别对其安排在1,3,5,7的位置上
        for (int i = 0; i < hash.length; i++) {
            while (hash[i] > 0) {
                if (idx >= res.length) { // idx重置
                    idx = 1;
                }
                res[idx] = (char) (i + 'a');
                idx += 2;
                hash[i]--;
            }
        }
        return String.valueOf(res);
    }
}

300. Longest Increasing Subsequence

题目
Given an unsorted array of integers, find the length of 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.

二分查找 (nlogn)

  • 推荐写法 Java版:
class Solution {
    public int lengthOfLIS(int[] nums) {
        int n = nums.length;
        int[] q = new int[n + 1];
        int len = 0;
        for (int i = 0; i < n; i++) {
            int left = 0, right = len;
            while (left < right) {
                int mid = left + right + 1 >> 1;
                if (q[mid] >= nums[i]) right = mid - 1;
                else left = mid;
            }
            len = Math.max(len, right + 1);
            q[right + 1] = nums[i];
        }
        return len;
    }
  • 推荐写法 Python版:
class Solution(object):
    def lengthOfLIS(self, arr):
        """
        :type nums: List[int]
        :rtype: int
        """
        tails = [0] * len(arr)
        size = 0
        for x in arr:
            left, right = 0, size
            while left != right:
                mid = (left + right) / 2
                if tails[mid] < x:
                    left = mid + 1
                else:
                    right = mid
            tails[left] = x
            
            size = max(size, left + 1)
        return size

或者

class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        from bisect import bisect_left
        dp = []
        for num in nums:
            i = bisect_left(dp, num)
            if i == len(dp):
                dp.append(num)
            else:
                dp[i] = num
        
        return len(dp)

动态规划 (n^2),不推荐

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

4. Median of Two Sorted Arrays

参考:https://www.youtube.com/watch?v=ScCg9v921ns

image

Binary Search

class Solution {
    public double findMedianSortedArrays(int[] A, int[] B) {
        int lenA = A.length, lenB = B.length;
        if (lenA > lenB) { return findMedianSortedArrays(B,A); }
        if (lenA == 0)
            return ((double)B[(lenB - 1)/2] + (double)B[lenB/2])/2;
        int len = lenA + lenB;
        int A_startK = 0, A_endK = lenA;
        int cutA, cutB;
        while (A_startK <= A_endK) {
            cutA = (A_endK + A_startK) / 2;
            cutB = (len + 1)/2 - cutA;
            double L1 = (cutA == 0) ? Integer.MIN_VALUE : A[cutA-1];
            double L2 = (cutB == 0) ? Integer.MIN_VALUE : B[cutB-1];
            double R1 = (cutA == lenA) ? Integer.MAX_VALUE : A[cutA];
            double R2 = (cutB == lenB) ? Integer.MAX_VALUE : B[cutB];
            if (L1 > R2) {
                A_endK = cutA - 1;
            }
            else if (L2 > R1) {
                A_startK = cutA + 1;
            }
            else {
                if (len % 2 == 0) {
                    return (Math.max(L1, L2) + Math.min(R1, R2)) / 2;
                } 
                else {
                    return Math.max(L1, L2);
                }
            }
        }
        return -1;
    }
}

116. Populating Next Right Pointers in Each Node

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

Example 1:

img

Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

代码

"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, left, right, next):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""
class Solution(object):
    def connect(self, root):
        """
        :type root: Node
        :rtype: Node
        """
        if not root or not root.left:
            return root
        root.left.next = root.right
        if root.next:
            root.right.next = root.next.left
        self.connect(root.left)
        self.connect(root.right)
        return root        
class Solution(object):
    def connect(self, root):
        """
        :type root: Node
        :rtype: Node
        """
        if not root:
            return None
        queue = deque()
        queue.append(root)
        
        while queue:
            preNode = Node()
            queuelen = len(queue)
            for _ in range(queuelen):
                cur = queue.popleft()
                if preNode:
                    preNode.next = cur
                preNode = cur

                if cur.left:
                    queue.append(cur.left)
                if cur.right:
                    queue.append(cur.right)
        
        return root

207. Course Schedule

Description

There are a total of numCourses courses you have to take, labeled from 0 to numCourses-1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

Example 1:

Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take. 
             To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take. 
             To take course 1 you should have finished course 0, and to take course 0 you should
             also have finished course 1. So it is impossible.

Constraints:

  • The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  • You may assume that there are no duplicate edges in the input prerequisites.
  • 1 <= numCourses <= 10^5

DFS

class Solution {    
    public boolean canFinish(int N, int[][] preqs) {        
        ArrayList[] G = new ArrayList[N];
        for (int i = 0; i < N; i++) {
            G[i] = new ArrayList();       
        }
        
        for (int i = 0; i < preqs.length; i++) {
            G[preqs[i][1]].add(preqs[i][0]);    // G[1]=[0, 2, ...]把1的先修课加到集合里
        }
        
        boolean[] visited = new boolean[N];
        
        for (int course_idx = 0; course_idx < N; course_idx++) {
            if (!dfs(G, visited, course_idx)) {
                return false;
            }
        }
    
        return true;
    }
    
    private boolean dfs(ArrayList[] G, boolean[] visited, int course) {
        if (visited[course] == true)
            return false;
        
        visited[course] = true;
        
        for (int i = 0; i < G[course].size(); i++) {
            if (!dfs(G, visited, (int)G[course].get(i)))
                return false;
        }
        
        visited[course] = false;
        return true;
    }
}

315. Count of Smaller Numbers After Self

题目

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of 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.

这道题与307. Range Sum Query – Mutable 类似,可以用 Fenwick Tree (Binary Indexed Tree) 这种特殊的数据结构来处理。具体参考 Fenwick Tree

image

首先定义 Fenwick Tree 的类:

  • int query(int i); 方法用来查找第i个节点的prefix sum,求 prefix sum 的过程是沿着一条路径(如图),每次 i -= i & -i; 并将 prefix sum 累加。
  • void update(int i, int delta); 方法用来动态更新第i个节点的值sums[i],以及受它影响的其他节点的值(如图),至于是哪些节点(哪些 index)受影响,操作跟query相反,每次i += i & -i;并更改sums[i].
class FenwickTree {    
    private int[] sums;    
    
    public FenwickTree(int n) {
      sums = new int[n + 1];
    }
 
    public void update(int i, int delta) {    
      while (i < sums.length) {
          sums[i] += delta;
          i += lowbit(i); //             i += i & -i;
      }
    }
 
    public int query(int i) {       
      int sum = 0;
      while (i > 0) {
          sum += sums[i];
          i -= lowbit(i); //             i += i & -i;
      }
      return sum;
    }    
}

然后,根据题意,我们要求解 [5, 2, 6, 1] 中,比自己要小的后续数字个数 (smaller numbers after self),第一个数是5,后面的2、1比5小,所以有2个;第二个数是2,后面的1比2小,有1个;以此类推。

基本思路:

  • 先将原数组排序,用HashMap记录排序后数字的相对排名(rank,数越大排名越大),根据 rank 我们可以知道两点:1. 有多少个unique numbers, 2. 根据 rank 个数(也就是不重复数字的个数)创建一个辅助数组,该数组下标代表数组对应的排名,也就是说,假如先访问的数字比较小,对应的下标在 rank[] 中就靠前,那么我们query这个数组的范围(0 到 i)就小,再update。如果后面访问的数字越来越大,对应的下标在 rank[] 中就靠后,那么我们只需要找排名比它要小的数字(rank范围在0到i)的总数即可,这个总数可以看成 rank[] 的前缀和 prefix sum。所以核心思路就是要先记录较小数字出现的次数,并保证记录的时候要在辅助数组(rank数组)中靠前,那么问题就转化为求一个数组前i个元素的前缀和了,凡是求 prefix sum的问题,也都能用 fenwick tree 解决了。

完整代码:

class FenwickTree {    
    int[] sums;
    
    public FenwickTree(int n) {
        // constructor
        sums = new int[n + 1];
    }
    
    public void update(int i, int delta) {
        while (i < sums.length) {
            sums[i] += delta;
            i += i & -i; // 等号右边取lowbit(i),最低位二进制数
        }
    }
    
    public int query(int i) {
        int sum = 0;
        while (i > 0) {
            sum += sums[i];
            i -= i & -i;
        }
        return sum;
    }
}

class Solution {
    public List<Integer> countSmaller(int[] nums) {
        int[] sorted = Arrays.copyOf(nums, nums.length);
        Arrays.sort(sorted);
        Map<Integer, Integer> ranks = new HashMap<>(); // map<num, rank>
        int rank = 0;
        for (int i = 0; i < sorted.length; i++) {
            if (i == 0 || sorted[i] != sorted[i - 1])
                ranks.put(sorted[i], ++rank);
        }
        
        FenwickTree tree = new FenwickTree(ranks.size());
        List<Integer> ans = new ArrayList<Integer>();
        for (int i = nums.length - 1; i >= 0; --i ) { // 从后往前,保证先记录后面数字的rank[i]
            int prefix_sum = tree.query(ranks.get(nums[i]) - 1);
            ans.add(prefix_sum);
            tree.update(ranks.get(nums[i]), 1);
        }
        
        Collections.reverse(ans); // 因为是从后往前访问,最后记得反转ans
        return ans;
    }
}

787. Cheapest Flights Within K Stops

https://leetcode.com/problems/cheapest-flights-within-k-stops/

Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph looks like this:

image

代码

  • c++
class Solution
{
public:
    
    int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K)
    {        
        vector<vector<int>> dp(K+2, vector<int>(n, INT_MAX));
        
        //dp[i][j] = Dist to reach j using atmost i edges from src
        
        for(int i = 0; i <= K+1; i++)
        {
            dp[i][src] = 0; // Dist to reach src always zero
        }
        
        for(int i = 1; i <= K+1; i++)
        {
            for(auto &f: flights)
            {
                //Using indegree
                int u = f[0];
                int v = f[1];
                int w = f[2];
                
                if(dp[i-1][u] != INT_MAX)
                    dp[i][v] = min(dp[i][v], dp[i-1][u] + w);
            }
        }
        
        return (dp[K+1][dst] == INT_MAX)? -1: dp[K+1][dst];
    }
};
  • Python
        dp = [[float('inf') for _ in range(n)] for _ in range(K+2)]
        for i in range(K+2):
            dp[i][src] = 0
        
        for i in range(1, K+2):
            for f in flights:
                u, v, w = f[0], f[1], f[2]
                # print(u, v, w)
                if dp[i-1][u] != float('inf'):
                    dp[i][v] = min(dp[i][v], dp[i-1][u]+w)
        
        return dp[K+1][dst] if dp[K+1][dst] != float('inf') else -1

65. Valid Number (正则表达式)

Validate if a given string can be interpreted as a decimal number.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3 " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

  • Numbers 0-9
  • Exponent - "e"
  • Positive/negative sign - "+"/"-"
  • Decimal point - "."

Of course, the context of these characters also matters in the input.

代码

class Solution:
    def isNumber(self, s: str) -> bool:
        return re.match(r'^\s*[+|\-]?(\.\d+|\d+(\.\d*)?)(e[\+|\-]?\d+)?\s*$', s)

801. Minimum Swaps To Make Sequences Increasing

https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/

题目

We have two integer sequences A and B of the same non-zero length.

We are allowed to swap elements A[i] and B[i]. Note that both elements are in the same index position in their respective sequences.

At the end of some number of swaps, A and B are both strictly increasing. (A sequence is strictly increasing if and only if A[0] < A[1] < A[2] < ... < A[A.length - 1].)

Given A and B, return the minimum number of swaps to make both sequences strictly increasing. It is guaranteed that the given input always makes it possible.

Example:
Input: A = [1,3,5,4], B = [1,2,3,7]
Output: 1
Explanation:
Swap A[3] and B[3]. Then the sequences are:
A = [1, 3, 5, 7] and B = [1, 2, 3, 4]
which are both strictly increasing.
Note:

A, B are arrays with the same length, and that length will be in the range [1, 1000].
A[i], B[i] are integer values in the range [0, 2000].

代码

class Solution {
    public int minSwap(int[] A, int[] B) {
        int n = A.length;
        int[] keep = new int[n];
        int[] swap = new int[n];
        Arrays.fill(keep, Integer.MAX_VALUE);
        Arrays.fill(swap, Integer.MAX_VALUE);
        keep[0] = 0;
        swap[0] = 1;
        for (int i = 1; i < n; i++) {
            if (A[i] > A[i-1] && B[i] > B[i-1]) {
                // Good case, no swapping needed.
                keep[i] = keep[i-1];

                // Swapped A[i-1] / B[i-1], swap A[i], B[i] as well
                swap[i] = swap[i-1] + 1;
            }
            
            if (A[i] > B[i-1] && B[i] > A[i-1]) {
                // A[i-1] / B[i-1] weren't swapped.
                keep[i] = Math.min(keep[i], swap[i-1]);
                
                // Swapped A[i-1] / B[i-1], no swap needed for A[i] / B[i]
                swap[i] = Math.min(swap[i], keep[i-1] + 1);
            }
        }
        
        return Math.min(keep[n-1], swap[n-1]);
    }
}

3. Longest Substring Without Repeating Characters

题目: 无重复字符的最长子串

Given a string, find the length of the longest substring without repeating characters.
Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

双指针思路

  • 双指针方法,left = 0, right = 0,max = 0,定义一个长度为128的 boolean[] used 数组,来表示字符串中哪些字符(ASCII 为 0~127)被使用过。
  • 用 s.charAt(right) 表示当前字符,right 从头遍历到尾。
  • 若当前字符不在 used 中,则将该字符在 used 中标记为 true,并且 right ++
  • 若当前字符在 used 中 (已经在前面出现过),
    首先,将max值更新 max = Math.max(max, right - left) 然后,我们需要处理 left,并且要一直移动 left 直到 left 对应的字符与当前字符重复为止,比如 “abcdbe”,因为“abcd” 中每个字符均在扫描时使用了一次,所以这个阶段都是 right 指针在移动,left 指针依旧处于 0 的位置,接着 right 来到字符 ‘b’ ,发现 b 已被使用,我们需要做的是让 left 来到第一个 'b' 的位置,即让 left = 1。此时,left 和 right 都已经指向了相同的字符‘b’,下一步我们的目的是要把 left 和 right 同时右移一位,以保证 [left, right) 区间不再有重复现象。
  • 循环 right 一直向后,直到结束循环,最终再更新一次 max 的值

Code

// Two Pointer
class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0)    return 0;
        int n = s.length();
        int left = 0, right = 0;
        int max = 0;
        boolean[] used = new boolean[128];
        
        while (right < n) {
            if (used[s.charAt(right)] == false) {
                used[s.charAt(right)] = true;
                right++;
            }
            else {
                max = Math.max(max, right-left);
                while (left < right && s.charAt(left)!= s.charAt(right)) {
                    used[s.charAt(left)] = false;
                    left++;
                }
                // find same char
                left++;
                right++;
            }
        }
        // lastly, update max
        max = Math.max(max, right - left);
        return max;
    }
}

排列&组合问题

46. Permutation

Given a collection of distinct integers, return all possible permutations.

Example:

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

代码

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        total = len(nums)
        res = []
        permute = deque()
        permute.append([])
        
        for cur in nums:
            permute_len = len(permute)
            for _ in range(permute_len):
                last_permute = permute.popleft()
                for pos in range(len(last_permute) + 1):
                    # insert current number to each position
                    new_permute = list(last_permute)
                    new_permute.insert(pos, cur)
                    if len(new_permute) == total:
                        res.append(new_permute)
                    else:
                        permute.append(new_permute)
        
        return res

78. Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

代码

class Solution(object):
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        subsets = []
        subsets.append([])
        
        for cur in nums:
            sublen = len(subsets)
            for i in range(sublen):
                each = list(subsets[i])
                each.append(cur)
                subsets.append(each)
            
        return subsets

784. Letter Case Permutation

Examples:
Input: S = "a1b2"
Output: ["a1b2", "a1B2", "A1b2", "A1B2"]

Input: S = "3z4"
Output: ["3z4", "3Z4"]

Input: S = "12345"
Output: ["12345"]

代码

class Solution(object):
    def letterCasePermutation(self, string):
        """
        :type S: str
        :rtype: List[str]
        """
        permute = []
        permute.append(string)
        
        for i in range(len(string)):
            
            if string[i].isalpha():
                
                permute_len = len(permute)
                
                for j in range(permute_len):
                    
                    each = list(permute[j])
                    each[i] = each[i].swapcase()
                    permute.append("".join(each))
                    
        return permute

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.