LeetCode题解
每周坚持刷LeetCode题,无论AC与否坚持记录解题思路,锻炼自己express thoughts precisely.
kaitankedemao
这个作者很懒,什么都没留下…
展开
-
[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 · 649 阅读 · 0 评论 -
[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 · 666 阅读 · 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 · 603 阅读 · 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 · 602 阅读 · 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 · 591 阅读 · 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 · 576 阅读 · 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 · 590 阅读 · 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 · 690 阅读 · 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 · 582 阅读 · 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 · 562 阅读 · 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 · 527 阅读 · 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 · 553 阅读 · 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 · 405 阅读 · 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 · 453 阅读 · 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 · 571 阅读 · 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 · 677 阅读 · 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 · 555 阅读 · 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 · 572 阅读 · 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 · 644 阅读 · 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 · 555 阅读 · 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 · 557 阅读 · 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 · 531 阅读 · 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 · 499 阅读 · 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 · 652 阅读 · 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 · 570 阅读 · 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 · 544 阅读 · 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 · 441 阅读 · 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 · 593 阅读 · 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 · 673 阅读 · 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 · 1168 阅读 · 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 · 594 阅读 · 0 评论 -
[LeetCode]Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks,原创 2015-02-21 20:54:44 · 894 阅读 · 3 评论 -
[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 · 890 阅读 · 0 评论 -
[LeetCode]Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note原创 2015-02-15 16:58:17 · 712 阅读 · 0 评论 -
[LeetCode]String to Integer
Q:Implement atoi to convert a string to an integer.其实咯,这个题目思路炒鸡简单啦,就是要细心一点,考虑问题全面一点,把 所有可能的输入情况都考虑到。下面贴上官方atoi的标准:Requirements for atoi:The function first discards as many whitespace characters原创 2015-01-30 20:32:54 · 423 阅读 · 0 评论 -
[LeetCode]Palindrome Number
Q:Determine whether an integer is a palindrome. Do this without extra space.这道题也是基础题,判断一个数是否是回文数,条件是不能使用额外的空间,意思就是说,你不能把一个Intege转换成string,然后对string进行回文字符串的判断。另外,如果用Reverse Integer的方法,可能会造成数值溢出,比如判原创 2015-01-31 10:57:27 · 560 阅读 · 0 评论 -
[LeetCode]Binary Tree Postorder Traversal
Q:Given a binary tree, return the postorder traversal of its nodes' values.Note:Recursive solution is trivial, could you do it iteratively?题目的意思就是不用递归求二叉树的后序遍历。后续遍历的递归方式很简单,首先遍历左子树,然后遍历右子树,最原创 2015-01-31 12:39:42 · 599 阅读 · 0 评论 -
[LeetCode]Two Sum
Q: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 numbers such that they add up to the targe原创 2015-01-31 13:37:21 · 619 阅读 · 0 评论 -
[LeetCode]Longest Substring Without Repeating Characters
Q: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.原创 2015-02-01 18:25:02 · 523 阅读 · 0 评论 -
[LeetCode]Add Two Numbers
Q: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原创 2015-02-02 22:39:19 · 503 阅读 · 0 评论