目前在阿里巴巴搬砖
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).
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.
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 ...
Dynamic Programming Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right.
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3].
Dynamic Programming Find the contiguous subarray within an array (containing at least one number) which has the largest product.
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
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.
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity.
Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 注意:多关键字的multimap不能使用下标操作。
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1. 注意:最后面的子串最长,在循环外也要更新一次最大长度。
Sort a linked list in O(n log n) time using constant space complexity. C++代码的实现: #include #include using namespace std; //Definition for singly-linked list.
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, .
一、使用atoi 说明: itoa( int value, char *string, int radix ); 第一个参数:你要转化的int; 第二个参数:转化后的char*; 第三个参数:你要转化的进制; 举例: /...
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's.
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 ...
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.
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). Find the minimum element. You may assume no duplicate exists in the array. 思路:如果中间节点的值最大,则取后半部分,如果中间节点的值最小,则取前半部分。
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two ...
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 alternate between).
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 本来不是很难的一题,提交了n遍,一直运行时错误,为什么呢?因为一个指针判断的问题,由于要向后移动两个指针,因为p->next也需要判断,而不能仅仅判断p,不然p=p->next->next会有问题。
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.
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 and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8 注意:最后一位如果有进位,需要新创建一个结点来存放。
Sort a linked list using insertion sort. C++代码如下: #include #include using namespace std; //Definition for singly-linked list.
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font ...
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers.
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases.
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are ...
Write a function to find the longest common prefix string amongst an array of strings. (每个字符串从0开始的公共部分即最长公共前缀) C++代码如下: #include #include #incl...
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2.
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. 括号匹配的问题,使用栈解决。
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 给定一个数组,数组中含有一些数,可能包含重复的元素。
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 这个题应该就是求子串的问题,改进的方法是kmp算法。
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
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.
Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list.
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 完全是靠列举结果,推出迭代公式,发现就是斐波那契数列的形式,即当n>=3时,f(n)=f(n-1)+f(n-2)。