01 2016 档案
摘要:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For exam
阅读全文
摘要:Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm s
阅读全文
摘要:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum
阅读全文
摘要:题目:给几个Email的list,输出全部list的交集(在全部list中都出现过的email)。 思路:用set记录前i个list中都含有的email,当进行第i+1时,检查每个email是否在该set中存在,若存在,则说明该email在前i+1个list中都有,则将它记录到另一个set中。整个算
阅读全文
摘要:Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 采用位运算。我们先将两个数都看作是正数。 我们将除数左移一位就是将它
阅读全文
摘要:问题:给一个循环链表,和一个要删除的节点,将其删除。时间复杂度O(n)。核心代码如下: 1 ListNode *next = node->next; 2 ListNode *cur = node; 3 while (cur->next != node) 4 cur = cur->next; 5 cu
阅读全文
摘要:Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assum
阅读全文
摘要:给一个sorted array 0 0 0 1 1 1 1,然后找出第一个1的位置。 边界情况:array为空或者全0。 思路:二分查找。为了优化,可以先判断最后一个数是不是0。 1 class Solution 2 { 3 public: 4 int FindBrokenCode(vector<i
阅读全文
摘要: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.
阅读全文
摘要: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
阅读全文
摘要:Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowes
阅读全文
摘要:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia
阅读全文
摘要: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 num
阅读全文
摘要:Preorder 1 class Solution { 2 public: 3 vector<int> preorderTraversal(TreeNode* root) { 4 vector<int> res; 5 if (!root) return res; 6 stack<TreeNode *
阅读全文
摘要:Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. 思路:递归和迭代。
阅读全文
摘要:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and
阅读全文
摘要:Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the dept
阅读全文
摘要:Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["
阅读全文
摘要: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
阅读全文
摘要:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For exa
阅读全文
摘要:共有三种思路。 哈希表。 将较小的那个数组中的所有元素存在哈希表中。然后依次验证另一个数组中的数字是否有出现过。时间复杂度O(m + n),空间复杂度O(min(m, n)) 二分搜索法 将较小的那个数组中的每一个元素,都用二分搜索的方法在较大数组中验证是否出现过。当两个数组大小相差很大时,这种方法
阅读全文
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:将这个矩阵沿左对角线(从左上角到...
阅读全文
摘要:Given a set of distinct integers,nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not...
阅读全文
摘要:Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct ...
阅读全文
摘要:Given an array of strings, group anagrams together.For example, given:["eat", "tea", "tan", "ate", "nat", "bat"],Return:[ ["ate", "eat","tea"], ["na...
阅读全文
摘要:Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume...
阅读全文
摘要:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.思路:构建一个大小为K的小顶堆。每次从堆顶取走一个元素后,都将这个元素所属的链表中下一个元素加入堆中。若没有...
阅读全文
摘要: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 n...
阅读全文
摘要:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,2...
阅读全文
摘要:Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain lead...
阅读全文
摘要:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".For C programmers: Try to solv...
阅读全文
摘要:Description:Count the number of prime numbers less than a non-negative number,n.思路:这题第一种思路是写一个is_prime函数,来对每一个数验证一次是否是prime。这个方法在这个题里表现不快,但也是一个学习is_pr...
阅读全文
摘要:Given an arraynumsand a target valuek, find the maximum length of a subarray that sums tok. If there isn't one, return 0 instead.Example 1:Givennums=[...
阅读全文
摘要:Given a collection of integers that might contain duplicates,nums, return all possible subsets.Note:Elements in a subset must be in non-descending ord...
阅读全文
摘要:Given a string arraywords, find the maximum value oflength(word[i]) * length(word[j])where the two words do not share common letters. You may assume t...
阅读全文
摘要:Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your...
阅读全文
摘要:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters fo...
阅读全文

浙公网安备 33010602011771号