目前在阿里巴巴搬砖
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 参考:http://www.cnblogs.com/AnnieKim/archive/2013/04/25/3041982.html 首先简单介绍一下Anagram(回文构词法)。
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 cos...
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below.
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.
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
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. 大整数乘法 我们以289*785为例 首先我们把每一位相乘,得到一个没有进位的临时结果,如图中中间的一行红色数字就是临时结果,然后把临时结果从低位起依次进位。
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 思路:首先以start排序,至于使用algorithm中的sort,需要自定义比较函数。
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:开始做过两两合并的链表,此时做k路合并,即将k个链表进行合并,可以先将这k个链表进行两两合并,知道合并为一个链表。
Implement int sqrt(int x). Compute and return the square root of x. 这里给出两种实现方法:一是二分搜索,二是牛顿迭代法。 1. 二分搜索 对于一个非负数n,它的平方根不会小于大于(n/2+1)。
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 should run in O(n) time and uses constant space. 对于正数A[i],我们希望它能被放在第i个位置上,这样子当我们找到A[i] != i时,我们就找到了想要的数。
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree.
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree.
Dynamic Programming Given a string S and a string T, count the number of distinct subsequences of T in S.
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Dynamic Programming 参考:http://fisherlei.blogspot.com/2012/12/leetcode-jump-game.html http://blog.
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order.
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4].
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a funct...
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? ...
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 注意与求树的高度的区别。
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1].
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 首先我是要AVL树的创建过程进行操作,不过提交之后出现超时,但还是让我将AVL树的创建过程实现了一遍,...
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 dep...
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 nu...
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit.
Dynamic Programming Say you have an array for which the ith element is the price of a given stock on day i.
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. 提交的错误,不知道为什么,但是要自己避免。
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 思路:将元素一个一个的插入,首先只有一个元素{1},此时,插入之后会的到两个vector,{1,2},{2,1},然后继续插入第三个元素3,会得到{3,1,2},{1,3,2},{1,2,3}和{3,2,1},{2,3,1},{2,1,3}。
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /.
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? C++实现代码: ...
Implement pow(x, n). C++实现代码: #include using namespace std; class Solution { public: double pow(double x, int n) { if(...
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
Dynamic Programming Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
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. 注意:当给定的k为0的时候,不需要旋转。
Dynamic Programming Follow up for "Unique Paths": Now consider if some obstacles are added to the grids.
Dynamic Programming A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
Dynamic Programming Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接。 http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c 水平有限,如有翻译不当,欢迎探讨、批评与指正。
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the".