
OJ
文章平均质量分 50
楚兴
这个作者很懒,什么都没留下…
展开
-
[LeetCode] Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解题思路:只有因子2和因子5相乘会产生10,同时因为因子2的数量大于因子5的数量,所以只需看序列中因子5的个数。它可以通过n/5得到,同原创 2015-02-06 16:22:46 · 670 阅读 · 0 评论 -
[LeetCode] Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ...原创 2015-02-06 14:30:19 · 716 阅读 · 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, in that case原创 2015-02-07 13:05:42 · 696 阅读 · 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 exists in the array.原创 2015-02-08 17:36:05 · 634 阅读 · 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 the largest produ原创 2015-02-08 21:53:42 · 630 阅读 · 0 评论 -
[LeetCode] String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.N原创 2015-02-07 15:00:52 · 722 阅读 · 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 unknown to you原创 2015-02-08 18:53:54 · 693 阅读 · 0 评论 -
[LeetCode] Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:begin to intersect at node c1. 解题思路 首先把两个链表的所有非空结点入栈,然后比较栈顶元素并出原创 2015-02-08 15:01:41 · 878 阅读 · 0 评论 -
[LeetCode] Compare Version Numbers
Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and co原创 2015-02-06 21:55:45 · 800 阅读 · 0 评论 -
[LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the原创 2015-02-08 16:29:16 · 639 阅读 · 0 评论 -
[LeetCode] Best Time to Buy and Sell Stock II
Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy原创 2015-02-17 23:26:02 · 765 阅读 · 0 评论 -
[LeetCode] Reverse Words in a String
Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, return “blue is sky the”.Clarification:1.What constitutes a word? A sequence of non-space characters原创 2015-02-09 11:04:34 · 812 阅读 · 0 评论 -
[LeetCode] LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if the k原创 2015-02-16 18:10:02 · 958 阅读 · 0 评论 -
[LeetCode] Best Time to Buy and Sell Stock III
Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note: You may no原创 2015-02-22 18:11:28 · 1457 阅读 · 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", "1", " ", "3", "*"]原创 2015-02-09 15:28:11 · 643 阅读 · 0 评论 -
[LeetCode] Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路 若两个点共线,则有如下情况:两个点重合 两个点的x坐标相同(斜率无穷大) 两个点的斜率相同原创 2015-02-09 20:59:41 · 938 阅读 · 0 评论 -
[LeetCode] Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements.You may原创 2015-02-07 12:50:30 · 1000 阅读 · 0 评论 -
[LeetCode] Best Time to Buy and Sell Stock
Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock原创 2015-02-17 22:49:20 · 791 阅读 · 0 评论 -
[LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?递归原创 2015-02-24 01:19:13 · 1959 阅读 · 1 评论 -
[LeetCode] Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n=7n = 7 and k=3k = 3, the array [1,2,3,4,5,6,7][1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4][5,6,7,1,2,3,4].Note: Try to come u原创 2015-02-24 17:47:10 · 3502 阅读 · 0 评论 -
[LeetCode] Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?原创 2015-02-24 02:42:36 · 1553 阅读 · 0 评论 -
[LeetCode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Write a原创 2015-02-08 11:18:12 · 922 阅读 · 0 评论 -
[LeetCode] Sort List
Sort a linked list in O(n log n) time using constant space complexity. 可以利用归并排序解决该问题。普通的归并排序算法时间复杂度为O(nlogn),空间复杂度为O(n),因为需要建立两个数组来存储原来数组的值,这两个数组的长度加起来恰好为原数组的长度。但是对于链表可以省去复制两个已经排好序的数组的操作,从而使空间复杂度原创 2015-02-15 22:58:12 · 959 阅读 · 0 评论 -
[LeetCode] Insertion Sort List
Sort a linked list using insertion sort. 解题思路 对于得到结点current的插入位置,从头结点开始遍历,直到遍历到值大于等于节点current的结点,然后将从该结点到current的前驱结点的所有结点的值依次和current结点的值交换,从而达到将该节点插入所遍历到的位置的目的。原创 2015-02-16 00:12:37 · 758 阅读 · 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 00111001011110原创 2015-03-16 21:32:25 · 1365 阅读 · 0 评论 -
[LeetCode] Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representation 0000000000000原创 2015-03-16 22:22:19 · 1386 阅读 · 0 评论 -
[POJ] DNA Sorting
One measure of `unsortedness` in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence `DAABEC`, this measure is 5, since D is great原创 2015-03-23 09:51:37 · 3082 阅读 · 0 评论 -
[LeetCode] Reorder List
Given a singly linked list L: L0→L1→…→Ln−1→LnL_0→L_1→…→L_{n-1}→L_n, reorder it to: L0→Ln→L1→Ln−1→L2→Ln−2→…L_0→L_n→L_1→L_{n-1}→L_2→L_{n-2}→…You must do this in-place without altering the nodes’ values.原创 2015-03-18 10:01:43 · 716 阅读 · 0 评论 -
[LeetCode] Linked List Cycle II
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,头结点与循环节点之间的长度为k。定义两个指针slow和fast,slow每次走原创 2015-03-18 13:13:33 · 820 阅读 · 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?解题思路使用快慢两个指针,如果快的指针赶上了慢的,则说明存在回路。实现代码/** * Definition for singly-linked list. * struct Li原创 2015-03-18 12:13:00 · 670 阅读 · 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 another chara原创 2015-04-29 13:57:51 · 1344 阅读 · 0 评论 -
[LeetCode] Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.For example, given the range [5, 7], you should return 4.解题思路① n&(n-1),可以去除n的最低位的1。原创 2015-04-29 18:56:21 · 1453 阅读 · 0 评论 -
[LeetCode] House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses原创 2015-04-30 12:22:49 · 882 阅读 · 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 tree, 1原创 2015-04-30 13:00:14 · 1195 阅读 · 1 评论 -
[LeetCode] Count Primes
Description: Count the number of prime numbers less than a non-negative number, n解题思路采用Eratosthenes筛选法,依次分别去掉2的倍数,3的倍数,5的倍数,……,最后剩下的即为素数。实现代码class Solution { public: int countPrimes(int n) {原创 2015-04-28 10:16:15 · 1729 阅读 · 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解题思路定义两个指针pre和cur,如果cur的值为val,则删除该结点。需要注意原创 2015-04-28 12:19:48 · 1005 阅读 · 0 评论 -
[LeetCode] Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume原创 2015-05-01 19:29:19 · 1197 阅读 · 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 satisfy the following co原创 2015-05-16 16:59:23 · 1185 阅读 · 0 评论 -
[LeetCode] Reverse Linked List(递归与非递归反转链表)
Reverse a singly linked list.解题思路对于非递归实现,思路是依次将从第二个结点到最后一个结点的后继设为头结点,然后将该节点设为头结点(需记住将原头结点的后继设为空)。 对于递归实现,首先反转从第二个结点到最后一个结点的链表,然后再将头结点放到已反转链表的最后,函数返回新链表的头结点。递归实现代码1//Runtime:10 ms class Solution { publ原创 2015-05-16 14:28:52 · 9841 阅读 · 0 评论 -
[ACMcoder] Sum Problem
Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + … + n. Input The input will consist of a series o原创 2015-05-26 18:43:47 · 1023 阅读 · 0 评论