[Leetcode] Number of Islands 岛屿个数

2015-09-16
阅读 6 分钟
11.2k
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example 1: {代码...} Answer: 1 Example 2...

[Leetcode] Count and Say 数个数

2015-09-16
阅读 2 分钟
3.3k
Count consecutive digits and say it. For example, return 132341 if input is 1112224. There are three 1s, three 2s and one 4.

[Lintcode] Nth to Last Node in List 链表倒数第N个节点

2015-09-16
阅读 1 分钟
2.7k
Find the nth to last element of a singly linked list. The minimum number of nodes in list is n.

[Leetcode] Remove Duplicates from Sorted Array 移除有序数组中的重复元素

2015-09-16
阅读 2 分钟
6.8k
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2], Your function should return length ...

[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度

2015-09-15
阅读 4 分钟
4.1k
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

[Leetcode] Min Stack 最小栈

2015-09-15
阅读 2 分钟
5.2k
Design a stack that supports push, pop, top, and retrieving theminimum element in constant time.push(x) -- Push element x onto stack. pop() -- Removes the element ontop of the stack. top() -- Get the top element. getMin() -- Retrievethe minimum element in the stack.

[Leetcode] Set Matrix Zeroes 矩阵置零

2015-09-15
阅读 2 分钟
4.8k
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solutio...

[Leetcode] Combination Sum 组合数之和

2015-09-14
阅读 5 分钟
8.5k
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a combination...

[Leetcode] Combinations 组合数

2015-09-14
阅读 2 分钟
4.5k
Given two integers n and k, return all possible ombinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: {代码...}

[Leetcode] LRU Cache 最近使用缓存

2015-09-14
阅读 3 分钟
6k
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set(key, value) - Set or insert the value if the k...

[Leetcode] Gray Code 格雷码

2015-09-13
阅读 2 分钟
7k
The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = 2, return [0,1,3,2]. Its g...

[Leetcode] Edit Distance 最小编辑距离

2015-09-13
阅读 2 分钟
12.4k
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Replace a character

[Leetcode] Clone Graph 复制图

2015-09-13
阅读 2 分钟
4.1k
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neighbor of the node. As an example, consider the se...

[Leetcode] Intersection of Two Linked Lists 链表交点

2015-09-13
阅读 1 分钟
4.4k
先算出两个链表各自的长度,然后从较长的链表先遍历,遍历到较长链表剩余长度和较短链表一样时,用两个指针同时遍历两个链表。这样如果链表有交点的话,两个指针已经一定会相遇。

[Leetcode] Group Anagrams 变形词

2015-09-13
阅读 2 分钟
8.4k
Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: {代码...}

[Leetcode] Majority Element 众数

2015-09-13
阅读 3 分钟
6k
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array.

[Leetcode] Trapping Rain Water 积水问题

2015-09-13
阅读 2 分钟
7.2k
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 u...

[Leetcode] 3Sum 4Sum 3Sum Closet 多数和

2015-09-13
阅读 6 分钟
8.5k
3Sum其实可以转化成一个2Sum的题,我们先从数组中选一个数,并将目标数减去这个数,得到一个新目标数。然后再在剩下的数中找一对和是这个新目标数的数,其实就转化为2Sum了。为了避免得到重复结果,我们不仅要跳过重复元素,而且要保证2Sum找的范围要是在我们最先选定的那个数之后的。

[Leetcode] Implement Queue using Stacks 用栈实现队列

2015-09-12
阅读 2 分钟
3.4k
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element.empty() -- Return whether the queue is empty. Notes: You must use only standard operations of a stack --...

[Leetcode] Merge Two Sorted Lists Merge K Sorted Lists 归并有序列表

2015-09-08
阅读 3 分钟
4.4k
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

[Leecode] Linked List Cycle 链表环

2015-09-08
阅读 2 分钟
6k
Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?

[CS101] Common Sorting Algorithms 常见排序算法

2015-09-07
阅读 6 分钟
6.2k
插排的思路是保证遍历到每个元素时,该元素前面的序列都是有序的。基于这个前提,我们将遍历到的新元素和它前面的序列相比对,然后将这个元素插入到适当的位置,使得这个包含新元素的序列也是有序的。虽然外层遍历只要O(N)时间,但是因为找到新元素的适当位置后要将之后的所有元素后移,所以最差时间是O(N^2)。

[CS101] Programming Languages and OOP 编程语言及面向对象基础题

2015-09-07
阅读 15 分钟
3.3k
What is singleton? What's its cons and pros? How to implement it?Definition: Singleton pattern is a design pattern that ensure that only one instance of a class is created and Provide a global access point to the object.

[Leetcode] Roman to Integer and Integer to Roman 罗马数阿拉伯数转换

2015-09-07
阅读 3 分钟
3.1k
首先我们要熟悉罗马数的表达方式。M是1000,D是500,C是100,L是50,X是10,V是5,I是1。验证字符串是否是罗马数,我们先看一下有效的罗马数是什么样的,假设该数字小于5000,从千位到个位依次拆解。千位的表达方式 M{0,4}

[CS101] Operating System and Low Level Fundamental 操作系统及底层基础面试题

2015-09-06
阅读 5 分钟
2.7k
What's the difference between thread and process?A process is an instance of a computer program that is being executed. It contains the program code and its current activity. A process may be made up of multiple threads.

[Leetcode] Implement Trie 实现前缀树

2015-09-06
阅读 3 分钟
5.6k
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z.

[Leetcode] Find Median from Data Stream 数据流中位数

2015-09-06
阅读 5 分钟
14.5k
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Design a data structure that supports the following two op...

[Leetcode] Maximal Square 最大正方形

2015-09-05
阅读 2 分钟
15.9k
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: {代码...} Return 4.

[Leetcode] Contains Duplicate 包含重复

2015-09-05
阅读 2 分钟
8.6k
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

[Leetcode] Isomorphic Strings 同构字符串

2015-09-05
阅读 2 分钟
7.8k
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character ...