
leetcode
文章平均质量分 62
阳安子
技术在成长。
展开
-
LeetCode:382. Linked List Random Node(蓄水池抽样算法C++)
382. Linked List Random NodeGiven a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.Follow up: What if the linked list i原创 2016-08-23 16:18:22 · 1642 阅读 · 0 评论 -
LeetCode:Word Pattern
LeetCode:Word Pattern1、题目: Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-e原创 2016-07-19 20:57:28 · 391 阅读 · 0 评论 -
LeetCode:Path Sum系列
Path Sum1、题目: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.2、代码:/** * Definition for a b原创 2016-07-19 20:12:20 · 268 阅读 · 0 评论 -
LeetCode:Remove Nth Node From End of List
LeetCode:Remove Nth Node From End of List1、题目: 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.After removing原创 2016-07-19 19:02:03 · 372 阅读 · 0 评论 -
微信红包(腾讯2016研发工程师编程题)
微信红包(腾讯2016研发工程师编程题)1、题目: 春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。 给定一个红包的金额数组gifts及它的大小n,请返回所求红包的金额。 若没有金额超过总数的一半,返回0。 测试样例: [1,2,3,2,2],5 返原创 2016-07-08 11:18:24 · 1391 阅读 · 0 评论 -
LeetCode:Intersection of Two Linked Lists
LeetCode:Intersection of Two Linked Lists1、题目: Write a program to find the node at which the intersection of two singly linked lists begins. Notes:If the two linked lists have no intersection at all,原创 2016-07-19 17:02:28 · 357 阅读 · 0 评论 -
LeetCode:Contains Duplicate系列
Contains Duplicate1、题目: Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return f原创 2016-07-19 13:00:15 · 314 阅读 · 0 评论 -
LeetCode:Isomorphic Strings(同构字符串)
LeetCode:Isomorphic Strings(同构字符串)1、题目: 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 char原创 2016-07-19 10:41:46 · 499 阅读 · 0 评论 -
LeetCode:Rectangle Area
LeetCode:Rectangle Area1、题目: Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.原创 2016-07-19 10:17:25 · 320 阅读 · 0 评论 -
LeetCode:Unique Binary Search Trees系列
96. Unique Binary Search TreesGiven 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.1 3 3原创 2016-07-28 22:56:39 · 707 阅读 · 0 评论 -
LeetCode:Parentheses系列
Valid ParenthesesGiven a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.The brackets must close in the correct order, “()” and “()[]{}” ar原创 2016-07-20 19:20:56 · 477 阅读 · 0 评论 -
LeetCode: Implement strStr()(字符串匹配:Sunday算法)
LeetCode: Implement strStr()(字符串匹配)Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.暴利解法:一个一个反复比较。没有尝试。 函数调用:find()。 Sunday算原创 2016-07-21 22:16:40 · 515 阅读 · 0 评论 -
LeetCode:414. Third Maximum Number
题目Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:I原创 2016-10-29 18:01:38 · 577 阅读 · 0 评论 -
LeetCode:Single Number系列
Single NumberGiven 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 u原创 2016-07-24 09:36:01 · 373 阅读 · 0 评论 -
LeetCode: Duplicate Emails
LeetCode: Duplicate EmailsWrite a SQL query to find all duplicate emails in a table named Person. +—-+———+ | Id | Email | +—-+———+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +—-+———+原创 2016-08-13 21:15:18 · 568 阅读 · 0 评论 -
LeetCode:384. Shuffle an Array、随机数生成(C++)
标题Shuffle a set of numbers without duplicates.Example:// Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums);// Shuffle the array [1,2,3] and return its r原创 2016-09-03 22:01:04 · 1454 阅读 · 1 评论 -
LeetCode:50. Pow(x, n)
50. Pow(x, n)Implement pow(x, n).很好的题,让我对库函数的实现有了新的认识,原来他的实现与你想的并不一样,还是要多读书。 1、快速找到与结果接近的值; 2、进入递归; 3、符号的处理,以后一定要注意!!!class Solution {public: double myPow(double x, int n) { if(!n) ret原创 2016-08-23 19:51:16 · 384 阅读 · 0 评论 -
LeetCode: 341. Flatten Nested List Iterator
LeetCode: 341. Flatten Nested List IteratorGiven a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or oth原创 2016-08-21 19:14:01 · 584 阅读 · 0 评论 -
LeetCode:Palindrome Linked List(回文链表)
LeetCode:Palindrome Linked ListGiven a singly linked list, determine if it is a palindrome.Follow up: Could you do it in O(n) time and O(1) space?回文无处不在,还跑到链表去了。 将链表后半部分逆置,然后开始相等性判断。 类似于C写的: htt原创 2016-07-21 09:37:42 · 664 阅读 · 1 评论 -
LeetCode:Best Time to Buy and Sell Stock(买卖股票)系列
121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one an原创 2016-07-25 10:26:20 · 580 阅读 · 0 评论 -
LeetCode: Count Primes(计算n以内素数个数:高效算法)
LeetCode: Count Primes(计算n以内素数个数:高效算法)Description:Count the number of prime numbers less than a non-negative number, n. A prime number is a natural number that has exactly two distinct natural number原创 2016-07-22 11:07:58 · 1716 阅读 · 0 评论 -
LeetCode: Implement Stack using Queues
LeetCode: Implement Stack using Queues1、题目: Implement the following operations of a stack using queues.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – G原创 2016-07-18 18:16:49 · 375 阅读 · 0 评论 -
LeetCode:Convert Sorted Array(List) to Binary Search Tree
108. Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for a binary tree node. * struct TreeN原创 2016-07-28 18:07:00 · 875 阅读 · 0 评论 -
int转string与LeetCode:Count and Say
int转stringstring流的使用:#include <iostream>#include <string>#include <sstream>using namespace std;int main(){ string str{"is"}; int a; cin>>a; stringstream ss; ss<<a; str+=ss.s原创 2016-07-14 10:11:16 · 440 阅读 · 0 评论 -
LeetCode:347. Top K Frequent Elements
LeetCode:347. Top K Frequent ElementsGiven a non-empty array of integers, return the k most frequent elements.For example, Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is alway原创 2016-07-25 13:35:03 · 367 阅读 · 0 评论 -
LeetCode:238. Product of Array Except Self(除自身外的乘积)
LeetCode:238. Product of Array Except Self(除自身外的乘积)Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except原创 2016-07-24 13:22:17 · 362 阅读 · 0 评论 -
LeetCode:268. Missing Number
LeetCoed:268. Missing NumberGiven an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your原创 2016-07-24 11:07:20 · 454 阅读 · 0 评论 -
LeetCode:Counting Bits(高效计算n内所有数的置位数)
LeetCode:Counting Bits(高效计算n内所有数的置位数)Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as原创 2016-07-23 18:46:40 · 483 阅读 · 0 评论 -
LeetCode:第三天
1、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,null,nu原创 2016-07-11 21:41:22 · 408 阅读 · 0 评论 -
LeetCode第二天
1、Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determi原创 2016-07-10 22:49:16 · 378 阅读 · 0 评论 -
LeetCode:Merge Two Sorted Lists
Merge Two Sorted Lists1、题目: 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.2、代码:/** * Definition for sing原创 2016-07-11 17:16:31 · 354 阅读 · 0 评论 -
LeetCode第一天
1、Sum of Two Integers Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example: Given a = 1 and b = 2, return 3.递归求和,注意得到位不同,和位相同(这个要进位,<<1)。class Soluti原创 2016-07-09 22:12:42 · 367 阅读 · 0 评论 -
LeetCode:Super Ugly Number系列
1、Ugly Number IIWrite a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of t原创 2016-07-13 21:44:41 · 735 阅读 · 0 评论 -
LeetCode:357. Count Numbers with Unique Digits
LeetCode:357. Count Numbers with Unique DigitsGiven a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.Example: Given n = 2, return 91. (The answer should be the tota原创 2016-07-25 19:08:00 · 475 阅读 · 0 评论 -
LeetCode:House Robber系列
198. House Robber 已经有说明了,这篇第九个题。 http://blog.youkuaiyun.com/bestzem/article/details/51884230213. House Robber IINote: This is an extension of House Robber.After robbing those houses on that street, the原创 2016-07-28 11:05:10 · 485 阅读 · 0 评论 -
LeetCode:Valid Sudoku与Sudoku Solver
Valid Sudoku1、题目: 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 ‘.’. Note: A原创 2016-07-17 21:38:31 · 366 阅读 · 0 评论 -
LeetCode: Combination Sum系列
77. CombinationsGiven two integers n and k, return all possible combinations of k numbers out of 1 … n.For example, If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3]原创 2016-07-27 16:02:17 · 456 阅读 · 0 评论 -
LeetCode:Implement Queue using Stacks
LeetCode:Implement Queue using Stacks1、题目: Implement the following operations of a queue using stacks.push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue原创 2016-07-16 22:08:18 · 460 阅读 · 0 评论 -
LeetCode:Symmetric Tree(镜像二叉树)
LeetCode:Symmetric Tree(镜像二叉树)1、题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric:1 / \原创 2016-07-16 11:23:31 · 630 阅读 · 0 评论 -
LeetCode:Binary Tree Traversal(二叉树遍历非递归)
144. Binary Tree Preorder Traversal/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NU原创 2016-07-26 15:56:32 · 661 阅读 · 0 评论