leetcode题解 面试算法
文章平均质量分 68
高质量代码题解,更新ing……
Havenoidea
这个作者很懒,什么都没留下…
展开
-
leetcode:Rotate Image (旋转矩阵)【面试算法题】
题目: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?题意:逆时针旋转矩阵,原地旋转,意思是不能使用额外的空间存储矩阵。原创 2013-12-03 20:03:57 · 6037 阅读 · 2 评论 -
leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】
题目: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原创 2013-12-02 23:08:39 · 2234 阅读 · 0 评论 -
leetcode:Reverse Linked List II (反转链表中的一部分)【面试算法题】
题目: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.Note:Given m, n原创 2013-11-29 19:42:11 · 2370 阅读 · 0 评论 -
leetcode:Reverse Integer(反转数字)【算法面试题】
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask原创 2013-11-28 22:31:59 · 3592 阅读 · 0 评论 -
leetcode:Restore IP Addresses (修复ip地址) 【面试算法题】
题目:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]原创 2013-11-28 21:29:12 · 2113 阅读 · 0 评论 -
leetcode:Reorder List (修改链表排列不改变值) 【面试算法题】
题目: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原创 2013-11-28 20:10:39 · 1696 阅读 · 0 评论 -
leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】
题目:Given a linked list, remove the nth node from the end of list and return its head.题意删除链表中倒数第n个节点,不改变链表其他节点结构,返回链表头指针。原创 2013-10-21 21:42:13 · 2496 阅读 · 0 评论 -
leetcode:Remove Duplicates from Sorted List II (留下链表中只出现一次的元素)【面试算法题】
题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.题意要把链表中有重复的元素全部去除,只留下没有重复过的元素。原创 2013-10-20 13:30:38 · 5032 阅读 · 4 评论 -
leetcode:Remove Element (删除给定元素)【面试算法题】
题目: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.题意把数组中与给定值相同的元素删除,在原数组上修改,返回值是最终元素个数。原创 2013-10-20 19:13:47 · 14086 阅读 · 0 评论 -
leetcode:Remove Duplicates from Sorted List(去除链表中重复元素)【面试算法题】
题目:Given a sorted linked list, delete all duplicates such that each element appear only once.题意去掉链表中重复的元素。原创 2013-10-20 11:44:54 · 3901 阅读 · 0 评论 -
leetcode:Remove Duplicates from Sorted Array II (允许重复一次,去掉数组多余数字)【面试算法题】
题目: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].题意每个数只允许重复一次,输出去重后的数组和个数,依旧上一题的要求,常数空间也就是只能在原数组上操作。原创 2013-10-19 16:20:38 · 4629 阅读 · 1 评论 -
leetcode: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.Do not allocate extra space for another array, you must do this in place with constant memory.题意去掉数组重复的数字,并返回不同数字的个数,常数空间限制。原创 2013-10-19 15:45:34 · 5434 阅读 · 0 评论 -
leetcode:Recover Binary Search Tree (修正二叉查找树错误节点,不改变结构)【面试算法题】
题目:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.题意二叉查找树不合法,有两个节点的值被交换了,找出这两个节点并且不改变树的结构,使得二叉查找树合法,常数空间限制。原创 2013-10-19 11:13:29 · 8224 阅读 · 1 评论 -
leetcode:Pow(x, n) (计算x的n次方) 【面试算法题】
题目:Implement pow(x, n).题意计算x的n次方,考虑复杂度和n的取值。原创 2013-10-18 15:02:11 · 12089 阅读 · 0 评论 -
leetcode:Populating Next Right Pointers in Each Node II (顺序连接二叉树每一层节点)【面试算法题】
题目:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?题意把二叉树每一层所有的节点顺序链接,只能开辟常数空间。原创 2013-10-18 10:40:15 · 3678 阅读 · 0 评论 -
leetcode:Populating Next Right Pointers in Each Node(常数空间,连接二叉树每一层所有节点)【面试算法题】
题目:Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL.Note:You may only use constant extra space.You may assume that it is a pe原创 2013-10-17 17:53:07 · 3836 阅读 · 0 评论 -
leetcode:Plus One (加一)【面试算法题】
题目:Given a number represented as an array of digits, plus one to the number.题意动态数组存了一些个位数字,组成一个大数,计算这个大数加一之后的值。原创 2013-10-17 17:17:53 · 10815 阅读 · 0 评论 -
leetcode:Permutations II (序列所有无重复排列)【面试算法题】
题目:Given a collection of numbers that might contain duplicates, return all possible unique permutations.题意输出给定序列的所有无重复排列。原创 2013-10-17 16:03:29 · 8160 阅读 · 1 评论 -
leetcode:Permutations (序列所有排列)【面试算法题】
题目:Given a collection of numbers, return all possible permutations.题意输出给定序列的所有排列。原创 2013-10-17 15:49:41 · 6587 阅读 · 0 评论 -
leetcode:Permutation Sequence (顺序排列第k个序列) 【面试算法题】
题目:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order。题意1~n组成的序列从小到大排列,按顺序找出第k个序列输出。原创 2013-10-17 15:15:17 · 7544 阅读 · 0 评论 -
leetcode:Path Sum II (路径之和,记录结果路径)【面试算法题】
题目:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.题意找到所有路径,满足从根节点到叶子节点的和等于sum,输出所有满足条件的路径。原创 2013-10-16 16:48:13 · 2869 阅读 · 0 评论 -
leetcode:Path Sum (路径之和) 【面试算法题】
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.题意判断从根节点到叶子节点的路径和是否有等于sum的值。原创 2013-10-16 16:23:54 · 1731 阅读 · 0 评论 -
leetcode:Pascal's Triangle II (杨辉三角形,空间限制)【面试算法题】
题目:Given an index k, return the kth row of the Pascal's triangle.optimize your algorithm to use only O(k) extra space题意输出杨辉三角形第k行,空间限制为O(k)原创 2013-10-15 23:06:10 · 4106 阅读 · 0 评论 -
leetcode: Pascal's Triangle (杨辉三角形) 【面试算法题】
题目:Given numRows, generate the first numRows of Pascal's triangle.题意给出行数,输出对应的杨辉三角形。原创 2013-10-15 22:52:46 · 2305 阅读 · 0 评论 -
leetcode:Partition List (链表处理)【面试算法题】
题意求一个新的链表,满足原链表比x小的数在左边,比x大的数在右边,他们之间的相对顺序不变。原创 2013-10-15 22:33:14 · 4715 阅读 · 0 评论 -
leetcode:Palindrome Partitioning (字符串分割成回文串的所有方式) 【面试算法题】
Given a string s, partition s such that every substring of the partition is a palindrome。Return all possible palindrome partitioning of s.For example, given s = "aab",题意把给定的字符串分割,使得每个子串都是回文串,枚举所有的可能情况。原创 2013-09-30 11:10:32 · 4591 阅读 · 0 评论 -
leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
Determine whether an integer is a palindrome. Do this without extra space.题意判断数字是否是回文串,只能用常数空间,不能转换成字符串,不能反转数字。原创 2013-09-29 22:01:05 · 10306 阅读 · 0 评论 -
leetcode:Next Permutation (求下一个排列) 【面试算法题】
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement mus原创 2013-09-29 21:18:02 · 11049 阅读 · 0 评论 -
leetcode:N-Queens II (n皇后问题2)【面试算法题】
题目:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.题意输出n×n的皇后棋子布局有多少种不同的情况。和上题一样的思路,只是不需要输出图,每次满足条件的时候记录一下次数。原创 2013-09-29 17:13:18 · 3380 阅读 · 0 评论 -
leetcode:N-Queens (n皇后问题) 【面试算法题】
题目:Given an integer n, return all distinct solutions to the n-queens puzzle.Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.n皇后问题,题意就是求n×n矩阵中,每行放一个原创 2013-09-29 16:22:09 · 10524 阅读 · 0 评论 -
leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: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.题意给两个字符串表示的数字,计算他们的乘积。原创 2013-09-27 11:30:14 · 5387 阅读 · 0 评论 -
leetcode:Minimum Window Substring(最小覆盖子串)【面试算法题】
题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".Note:If there is no such window in S that covers all cha原创 2013-09-27 02:29:00 · 6267 阅读 · 0 评论 -
leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】
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.Note: You can only move either down or right at any point in time.题意从左上到右下,所有可能的路径中,求经过的元素和最小值。原创 2013-09-27 01:00:32 · 3241 阅读 · 0 评论 -
leetcode:Minimum Depth of Binary Tree(树的根节点到叶子节点的最小距离)【面试算法题】
题目: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.题意求到叶子节点最短的距离。递归遍历,注意必须要到原创 2013-09-26 00:30:42 · 4199 阅读 · 0 评论 -
leetcode:Merge Two Sorted Lists (链接两个有序链表)【面试算法题】
题目: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.题意链接两个有序链表。原创 2013-09-26 00:18:17 · 3776 阅读 · 3 评论 -
leetcode:Merge Sorted Array(合并两个有序数组到其中一个数组中)【面试算法题】
题目:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.题意要把两个有序的数组合并到他们中的一个原创 2013-09-25 23:59:53 · 7210 阅读 · 0 评论 -
leetcode:Merge k Sorted Lists(按大小顺序连接k个链表)【面试算法题】
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.题意:把k个排序成一个有序链表。原创 2013-09-25 23:49:58 · 2212 阅读 · 0 评论 -
leetcode:Merge Intervals(合并相交集合) 【面试算法题】
题目: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排序,遍历的时候判断前后集合是否原创 2013-09-25 18:49:00 · 3479 阅读 · 0 评论 -
leetcode:Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法】
题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).题意已只两个有序的序列,找到他们的中位数,复杂度要求O(log (m+n))。原创 2013-09-25 16:57:35 · 2346 阅读 · 0 评论 -
leetcode:Maximum Subarray(最大的连续子数组) 【面试算法】
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6.题意 求连续的子串和最大。原创 2013-09-23 22:21:03 · 2335 阅读 · 0 评论