Articles

  • 149 - Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

    Read More »

  • 3 - Longest Substring Without Repeating Characters

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

    Read More »

  • 128 - Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

    Read More »

  • 179 - Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number.

    Read More »

  • 378 - Kth Smallest Element in a Sorted Matrix

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.

    Read More »

  • k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target.

    Read More »

  • k Sum

    Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers where sum is target. Calculate how many solutions there are?

    Read More »

  • 208 - Implement Trie

    Implement a trie with insert, search, and startsWith methods.

    Read More »

  • Interleaving Positive and Negative Numbers

    Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.

    Read More »

  • 13 - Roman to Integer

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999.

    Read More »

  • 12 - Integer to Roman

    Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range from 1 to 3999.

    Read More »

  • 198 - House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

    Read More »

  • 134 - Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.

    Read More »

  • Convert Expression to Polish Notation

    Given an expression string array, return the Polish notation of this expression. (remove the parentheses)

    Read More »

  • Convert Expression to Reverse Polish Notation

    public class Solution {
        public ArrayList<String> convertToRPN(String[] expression) {
            ArrayList<String> result = new ArrayList<String>();
            if (expression == null || expression.length == 0) return result;        
            Stack<String> stack = new Stack<String>();
            for (String s : expression) {
                if (isOp(s)) {
                    if (s.equals("(")) {
                        stack.push(s);
                    } else if (s.equals(")")) {
                        while (!stack.isEmpty()) {
                            String p = stack.pop();
                            if (p.equals("(")) {
                                break;
                            }
                            result.add(p);
                        }
                    } else {
                        while (!stack.isEmpty() && order(s) <= order(stack.peek())) {// <= not < 因为如果s与stack.peek()相同order的话,我们要先处理栈里的,因为栈里的operator在表达式左边. 注意区分从左边开始处理和从右边开始处理 此处的<和<=
                            result.add(stack.pop());
                        }
                        stack.push(s);
                    }
                } else {
                    result.add(s);
                }
            }
            while (!stack.isEmpty()) {
                result.add(stack.pop());
            }
            return result;
        }
        public boolean isOp(String str) {
            if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("(") || str.equals(")")) {
                return true;
            } else {
                return false;
            }
        }
        public int order(String str) {
            if (str.equals("*") || str.equals("/")) {
                return 2;
            } else if (str.equals("+") || str.equals("-")) {
                return 1;//must have
            } else {
                return 0;
            }
        }
    }
    

    Read More »

  • 150 - Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation.

    Read More »

  • 29 - Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, return 2147483647

    Read More »

  • Digit Counts

    Count the number of k's between 0 and n. k can be 0 - 9.

    Read More »

  • Delete Digits

    Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer. Find the smallest integer after remove k digits.

    Read More »

  • 248 - Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) and an query list. For each query, give you an integer, return the number of element in the array that are smaller that the given integer.

    Read More »

  • 206 - Interval Sum

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result list.

    Read More »

  • 205 - Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the result list.

    Read More »

  • Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this node's interval. Implement a modify function with three parameter root, index and value to change the node's value with [start, end] = [index, index] to the new given value. Make sure after this change, every node in segment tree still has the max attribute with the correct value.

    Read More »

  • Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote the number of elements in the the array which value is between interval start and end. (The array may not fully filled by elements) Design a query method with three parameters root, start and end, find the number of elements in the in array's interval [start, end] by the given root of value SegmentTree.

    Read More »

  • Segment Tree Query

    For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attribute max to denote the maximum number in the interval of the array (index from start to end). Design a query method with three parameters root, start and end, find the maximum number in the interval [start, end] by the given root of segment tree.

    Read More »

  • Segmemt Tree Build II

    Implement a build method with a given array, so that we can create a corresponding segment tree with every node value represent the corresponding interval max value in the array, return the root of this segment tree.

    Read More »

  • Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end denote an segment / interval. start and end are both integers, they should be assigned in following rules:

    Read More »

  • 11 - Container With Most Water

    Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

    Read More »

  • Coins in a Line II

    There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins. Could you please decide the first player will win or lose?

    Read More »

  • Coins in a Line

    There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins. Could you please decide the first play will win or lose?

    Read More »