LeetCode
kaitankedemao
这个作者很懒,什么都没留下…
展开
-
[LeetCode]Count Primes
Count the number of prime numbers less than a non-negative number, n一个合数总是可以分解成若干质数的乘积。所以,如果把质数的倍数全都去掉,剩下的就是质数了。 要查找n以内的质数,首先2是质数,把2的倍数:4,6,8…去掉;此时3没有被去掉,可以认为是质数,再被3的倍数去掉;然后再到5,再到7,一直到sqrt(n)。设一个布尔类型原创 2015-05-04 18:58:52 · 677 阅读 · 0 评论 -
[LeetCode]Partition List
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. You should preserve the original relative order of the nodes in each o原创 2015-03-22 18:30:20 · 595 阅读 · 0 评论 -
[LeetCode]Find Minimum in Rotated Sorted Array
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原创 2015-03-11 22:01:25 · 692 阅读 · 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},原创 2015-03-17 21:49:06 · 517 阅读 · 0 评论 -
[LeetCode]Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: [“2原创 2015-03-17 20:59:51 · 663 阅读 · 0 评论 -
[LeetCode]Rotate List
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次,每次原创 2015-03-20 22:23:50 · 547 阅读 · 0 评论 -
[LeetCode]Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 001原创 2015-03-10 12:00:10 · 1198 阅读 · 0 评论 -
[LeetCode]Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solu原创 2015-03-10 11:49:14 · 611 阅读 · 0 评论 -
[LeetCode]Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 这道题是要求删除有原创 2015-03-20 22:10:05 · 605 阅读 · 0 评论 -
[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 wit原创 2015-03-17 15:36:54 · 585 阅读 · 0 评论 -
[LeetCode]Same Tree
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.这道题是要判断两棵树是不原创 2015-03-17 15:26:45 · 557 阅读 · 0 评论 -
[LeetCode]Sort List
Sort a linked list in O(n log n) time using constant space complexity.这道题是要求对单链表进行排序,有个O(nlogn)的时间复杂度的要求。我的想法是采取类似头插法的方式,遍历链表结点,设3个指针,min指向当前链表中的最小值,max指向当前链表中的最大值,cur指向前一插入的值。min , max , cur 初始时都指向第原创 2015-03-17 14:42:14 · 454 阅读 · 0 评论 -
[LeetCode]Insertion Sort
Sort a linked list using insertion sort.这道题是要求用插入排序的方式对单链表进行排序。先不考虑边界情况: 1. 将第一个结点看做有序区,之后的所有结点看做无序区。 2. 从第二个结点p开始,遍历有序区,知道遇到比结点p值大的结点q,将结点p插入到结点q之前。 3. 重复上述步骤直到链表遍历结束。需要注意的是: 1. 遍历无序区时,需要保存当前结点的后原创 2015-03-20 22:04:28 · 566 阅读 · 0 评论 -
[LeetCode]Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \原创 2015-02-14 14:47:33 · 554 阅读 · 0 评论 -
[LeetCode]Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.原创 2015-02-15 22:19:57 · 910 阅读 · 0 评论 -
[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.这道题与上一题类似, 要求根据二叉树的后序遍历序列和中序遍历序列构建二叉树。后序遍历序列的末尾是根节点,在中原创 2015-02-14 13:33:32 · 663 阅读 · 0 评论 -
[LeetCode]Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result m原创 2015-02-14 21:48:30 · 732 阅读 · 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:原创 2015-03-22 18:38:47 · 571 阅读 · 0 评论 -
[LeetCode]Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space?这道题是要求在不额外使用存储空间的基础上判断一个单链表中是否存在一个环。环的存在,就是在无环的基础上,将最后一个结点的next不设为NULL,而是指向该结点前的任意原创 2015-03-22 18:55:49 · 594 阅读 · 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. For example, Given 1->2->3->3->4->4->5, return 1->2->5.原创 2015-03-22 18:06:44 · 659 阅读 · 0 评论 -
[LeetCode]Isomorphic Strings
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 a原创 2015-05-03 21:07:27 · 662 阅读 · 0 评论 -
[LeetCode]Pow(x, n)
Implement pow(x, n).这道题要求完成pow(),即求出x的n次方的结果。二分法,注意n<0的情况。 1. 若n < 0,则返回 1.0 / pow(x , - n )的结果。 2. 若n >= 0,二分递归求解。例如,求2^5,可以先算2^2=4,2^2=4,然后4*4 = 16,最后16*2=32.double power(double x, int n){原创 2015-04-26 09:44:52 · 618 阅读 · 0 评论 -
[LeetCode]Single Number
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原创 2015-04-26 09:04:24 · 604 阅读 · 0 评论 -
[LeetCode]Sort Colors
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. Here, we will use the integers原创 2015-04-26 09:58:18 · 619 阅读 · 0 评论 -
[LeetCode]Validate Binary Search Tree
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原创 2015-04-09 22:04:05 · 464 阅读 · 0 评论 -
[LeetCode]Binary Tree Right Side View
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 example: Given the following binary tre原创 2015-04-09 19:52:51 · 537 阅读 · 0 评论 -
[LeetCode]Find Minimum in Rotated Sorted Array II
Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot原创 2015-04-09 19:34:58 · 571 阅读 · 0 评论 -
[LeetCode]Happy Number
Write an algorithm to determine if a number is “happy”. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the square原创 2015-04-24 22:44:42 · 704 阅读 · 0 评论 -
[LeetCode]Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5这道题很简单,要求删除单链表中与指定值相等的结点。在遍历单链表的原创 2015-04-24 22:13:52 · 606 阅读 · 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.给定两个字符串表示的数,要求求这两个数的乘积,乘积用字符串表示。其中,字符串表原创 2015-04-24 22:01:45 · 587 阅读 · 0 评论 -
[LeetCode]Symmetric Tree
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 / \ 2 2 / \ / \ 3 4 4 3原创 2015-04-09 20:47:07 · 574 阅读 · 0 评论 -
[LeetCode]Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.这道题要求根据一个有序的数组建立一棵二叉排序树。利用折半的思想,构造根节点,再递归构造左右子树。递归出口是数组中只含一个元素。TreeNode *sortedArrayToBST(vector<int> &原创 2015-04-14 20:55:51 · 599 阅读 · 0 评论 -
[LeetCode]Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has th原创 2015-03-13 23:06:46 · 630 阅读 · 0 评论 -
[LeetCode]Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.这道题是给定一个有序的单链表,将它转换成一棵平衡的二叉搜索树。首先要知道平衡的二叉搜索树的特点: 1. 左子树上的所有结点的值小于根节点的值,右子树上所有结点的值大于根节点的值。即原创 2015-03-22 18:47:29 · 697 阅读 · 0 评论 -
[LeetCode]Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.这道题是要求归并k个有序单链表。采用分治法,将任务分成两个子任务,然后递归求子任务,最后回溯:将k个list分成两半,然后继续划分,剩余连个list就合并。/** * Definition for si原创 2015-03-22 21:45:42 · 424 阅读 · 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,原创 2015-03-22 20:24:12 · 464 阅读 · 0 评论 -
[LeetCode]Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3原创 2015-02-13 18:28:20 · 739 阅读 · 0 评论 -
[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.这道题要求根据二叉树的前序遍历序列和中序遍历序列构建二叉树。 举个例子: 前序序列:A B D E F C原创 2015-02-14 13:06:53 · 619 阅读 · 0 评论 -
[LeetCode]Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2015-02-13 22:03:45 · 423 阅读 · 0 评论 -
[LeetCode]Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB 这道题是Excel Sheet Column Number的反转。 与一般的N进制的原创 2015-02-07 12:31:12 · 507 阅读 · 0 评论