[Algo] Longest Descending Path 滑雪问题

2015-10-22
阅读 2 分钟
4.2k
给出一个矩阵,求矩阵中从某个点开始,最长的下降路径。路径可以走上下左右四个方向。求最长路径的长度。 {代码...} 其中一条最长路径是8 7 6 5 1

[Algo] Print Matrix Diagonal 对角打印

2015-10-22
阅读 1 分钟
2.3k
Print Matrix Diagonal Print the matrix in diagonal way. For example: {代码...} Print: {代码...} 双重循环 复杂度 时间 O(NM) 空间 O(1) 思路 总共需要打印的层数,是长度加宽度减去一。关键在于内层的row = i - j,而col = j。 代码 {代码...}

[Leetcode] Multiply String and Big Interger 大数乘法加法减法

2015-10-22
阅读 5 分钟
6.8k
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative.

[Algo] Anagram Substring Search 变形词子串

2015-10-22
阅读 2 分钟
2.7k
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] and its permutations (or anagrams) in txt[]. You may assume that n > m. {代码...}

[Leetcode] Nim Game 尼姆游戏

2015-10-21
阅读 1 分钟
8.3k
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones. Both of you are very clever and have ...

[Leetcode] Dungeon Game 地牢游戏

2015-10-21
阅读 3 分钟
4.4k
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess. T...

[Leetcod] Generate Parentheses 产生括号

2015-10-21
阅读 2 分钟
3.2k
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"

[Leetcode] Swap Nodes in Pairs Reverse Nodes in k-Group 成组反转链表

2015-10-21
阅读 4 分钟
2.9k
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

[Leetcode] Verify Preorder Sequence in Binary Search Tree 验证先序序列

2015-10-19
阅读 3 分钟
10.5k
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up: Could you do it using only constant space complexity?

[Leetcode] Length of Last Word 最后一个单词长度

2015-10-18
阅读 1 分钟
3.8k
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. For example, Given s = "Hello Wor...

[Leetcode] Count And Say 外观序列

2015-10-13
阅读 2 分钟
4.3k
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n, generate the nth sequence. Note: The sequence of integers ...

[Java] 浅析Google Guava Multimap

2015-10-12
阅读 5 分钟
5.8k
ArrayListMultiMap.java是以ArrayList为Collection的特定实现,这个类中没有太多的实际代码,主要是createCollection()方法中特定的产生一个ArrayList作为Collection。

[Algo] Find Intersection of Two Sets 找交集

2015-10-10
阅读 3 分钟
4.4k
Find Intersection of Two Sets 暴力法 复杂度 时间 O(NM) 空间 O(1) 思路 暴力解法,对于每个在集合1中的元素,我们遍历一遍集合2看看是否存在,如果存在则是Intersection。 代码 {代码...} 统一排序法 复杂度 时间 O((M+N)log(M+N)) 空间 O(M+N) 思路 将两个集合合并起来排序,这样如果排序后的数组中有两个相同的元素...

[Leetcode] Word Pattern 单词模式

2015-10-07
阅读 4 分钟
11.1k
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "abba", str = "dog cat cat dog" should return true. pattern = "abba", str = "dog cat cat fish" should return false. pattern = "aaaa", str = "dog cat cat dog" should return false. pattern = "abba", str = "d...

[Leetcode] Search Insert Position 搜索插入位置

2015-10-06
阅读 2 分钟
3.6k
这是最典型的二分搜索法了。如果使用我这个二分模板是可以直接适用的,在模板中,while循环的条件是min<=max,如果target和mid指向的值相等,则返回mid,否则根据情况min = mid + 1或者max = mid - 1。这样的好处是,如果找不到该数,max是比该数小的那个数的下标,而min是比该数大的那个数的下标。这题中,我们返回m...

[Lintcode] Longest Increasing Subsequence 最长上升序列

2015-10-05
阅读 4 分钟
24.6k
给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度。样例 给出[5,4,1,2,3],这个LIS是[1,2,3],返回 3 给出[4,2,4,5,3,7],这个LIS是[4,4,5,7],返回 4 挑战 要求时间复杂度为O(n^2) 或者O(nlogn) 说明 最长上升子序列的定义: 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的...

[Leetcode] Game of Life 生命游戏

2015-10-05
阅读 12 分钟
20.7k
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight ne...

[Leetcode] Rotate Image 旋转图片

2015-10-05
阅读 1 分钟
4.9k
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place?

[Leetcode] Count Complete Tree Nodes 统计完全树节点数

2015-10-04
阅读 4 分钟
8.1k
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes incl...

[Leetcode] Search for a Range 寻找区间

2015-10-04
阅读 2 分钟
3.6k
Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4].

[Leetcode] Gas Station 加油站

2015-10-04
阅读 2 分钟
5.6k
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 th...

[Leetcode] Spiral Matrix 螺旋矩阵

2015-10-04
阅读 6 分钟
9.1k
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: {代码...} You should return [1,2,3,6,9,8,7,4,5].

[Leetcode] Find the Duplicate Number 找到重复数字

2015-10-04
阅读 3 分钟
27.8k
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify the array (assume the array is read only). You mu...

[Leetcode] Convert Sorted Array/List to Binary Search Tree 建BST

2015-10-03
阅读 3 分钟
3.8k
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

[Leetcode] Validate Binary Search Tree 验证二叉搜索树

2015-10-03
阅读 2 分钟
4.1k
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and...

[Leetcode] Evaluate Reverse Polish Notation 计算逆波兰表达式

2015-10-03
阅读 2 分钟
4.4k
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: {代码...}

[Leetcode] Rotate List 旋转链表

2015-10-03
阅读 2 分钟
3k
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.

[Leetcode] Reorder List 链表重新排序

2015-10-03
阅读 2 分钟
3.9k
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}

[Leetcode] Candy 分糖果

2015-10-03
阅读 2 分钟
7.4k
There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. What is the minimum candi...

[Leetcode] Container With Most Water 最大盛水容器

2015-10-03
阅读 1 分钟
9.2k
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 containercontains the most wat...