![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 70
houshion
即使没有收获的指望,也要心平气和地耕种。
展开
-
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".class Solution {public: void reverseWords(string &s) {原创 2016-04-25 16:22:03 · 248 阅读 · 0 评论 -
Count Primes
题目描述:Count the number of prime numbers less than a non-negative number, n.解题思路:使用素数筛选法class Solution {public: int countPrimes(int n) { vector prime(n, true); prime[0] = false,原创 2016-04-25 16:42:51 · 259 阅读 · 0 评论 -
Basic Calculator
题目描述:Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative intege原创 2016-05-26 21:34:29 · 270 阅读 · 0 评论 -
Implement Stack using Queues
题目描述: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() -- Get the top element.empty() --原创 2016-05-26 22:03:33 · 261 阅读 · 0 评论 -
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 adj原创 2016-05-17 19:27:16 · 237 阅读 · 0 评论 -
House Robber II
题目描述:Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention原创 2016-05-17 19:35:04 · 278 阅读 · 0 评论 -
Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are consist of lowercase letters a-z.class TrieNode {public: // Initialize your data stru原创 2016-05-08 10:21:08 · 209 阅读 · 0 评论 -
Course Schedule II
题目描述:There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is ex原创 2016-05-08 15:11:46 · 436 阅读 · 0 评论 -
Add and Search Word
题目描述:Design a data structure that supports the following two operations:void addWord(word)bool search(word)search(word) can search a literal word or a regular expression string containin原创 2016-05-08 15:54:39 · 392 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Tree
题目描述:Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwe原创 2016-06-07 19:50:07 · 202 阅读 · 0 评论 -
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 nums[i].Solve it without divisi原创 2016-06-07 20:14:12 · 173 阅读 · 0 评论 -
Sliding Window Maximum
题目描述:Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the slidi原创 2016-06-07 21:08:53 · 235 阅读 · 0 评论 -
Search a 2D Matrix II
题目描述:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.I原创 2016-06-07 21:20:46 · 179 阅读 · 0 评论 -
Kth Smallest Element in a BST
题目描述:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.解题思路:考虑二叉搜索树原创 2016-06-05 10:17:54 · 173 阅读 · 0 评论 -
Power of Two
题目描述:Given an integer, write a function to determine if it is a power of two.解题思路1:2的幂的数的二进制表示只有一个1,例如1->01,2->10,4->100,因此统计输入的数n的二进制的1的个数,如果二进制位的1的个数为1则为2的幂,否则不是2的幂。AC代码如下:class Solution原创 2016-06-05 10:53:00 · 219 阅读 · 0 评论 -
Implement Queue using Stacks
题目描述: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.peek() -- Get the front ele原创 2016-06-05 11:16:39 · 283 阅读 · 0 评论 -
Single Number III
题目描述:Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example原创 2016-06-10 10:22:19 · 240 阅读 · 0 评论 -
Ugly Number
题目描述:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is no原创 2016-06-10 11:03:10 · 231 阅读 · 0 评论 -
Missing Number
题目描述:Given 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 algo原创 2016-06-10 14:32:05 · 244 阅读 · 0 评论 -
Integer to English Words
题目描述:Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.For example,123 -> "One Hundred Twenty Three"12345 -> "Twelve原创 2016-06-10 15:27:56 · 306 阅读 · 0 评论 -
H-Index && H-Index II
题目描述:H-IndexGiven an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to the definition of h-i原创 2016-06-11 12:01:02 · 449 阅读 · 0 评论 -
Maximum Product of Word Lengths
题目描述:Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lo原创 2016-06-27 10:29:38 · 237 阅读 · 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+1=101+0=010+1=010+0=00原创 2016-07-10 11:13:57 · 220 阅读 · 0 评论 -
Coin Change
题目描述:You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amoun原创 2016-06-28 22:00:56 · 348 阅读 · 0 评论 -
Add Two Numbers
题目描述: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 i原创 2016-07-11 21:40:45 · 149 阅读 · 0 评论 -
Longest Palindromic Substring
题目描述:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.解题思路:枚举原创 2016-07-12 20:39:17 · 191 阅读 · 0 评论 -
ZigZag Conversion
题目描述:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N原创 2016-07-12 21:01:01 · 705 阅读 · 0 评论 -
Reverse Integer
题目描述:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321解题思路:使用stringstream,注意翻转后的数值可能超出int范围AC代码如下:class Solution {public: int revers原创 2016-07-12 21:18:08 · 186 阅读 · 0 评论 -
String to Integer (atoi)
题目描述:Implement atoi to convert a string to an integer.解题思路:题目并不难,主要是要考虑各种各样的输入,以及数值超出int范围的情况。AC代码如下:class Solution {public: int myAtoi(string str) { long long int ans = 0; int tag原创 2016-07-12 21:44:02 · 202 阅读 · 0 评论 -
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原创 2016-07-24 10:29:31 · 196 阅读 · 0 评论 -
Serialize and Deserialize Binary Tree
题目描述:Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection原创 2016-06-15 21:41:03 · 223 阅读 · 0 评论 -
Integer to Roman
题目描述:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.解题思路:用4个辅助数组分别保存个位、十位、百位和千位的0到9的罗马数值的表示。然后把输入数值的各位的罗马数值表示连接到一起即可AC代码如原创 2016-07-13 20:37:22 · 173 阅读 · 0 评论 -
Roman to Integer
题目描述:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.AC代码如下:class Solution{public: int romanToInt(string s){ vector unit =原创 2016-07-13 20:46:28 · 172 阅读 · 0 评论 -
Longest Common Prefix
题目描述:Write a function to find the longest common prefix string amongst an array of strings.暴力解法即可,AC代码如下:class Solution {public: string longestCommonPrefix(vector& strs) { string ans = "";原创 2016-07-13 20:54:09 · 174 阅读 · 0 评论 -
3Sum
题目描述:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must原创 2016-07-13 21:24:06 · 188 阅读 · 0 评论 -
3Sum Closest
题目描述:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would h原创 2016-07-13 21:45:14 · 172 阅读 · 0 评论 -
Bulls and Cows
题目描述:You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provi原创 2016-06-16 20:32:10 · 291 阅读 · 0 评论 -
Longest Increasing Subsequence
题目描述:Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7,原创 2016-06-16 20:57:50 · 199 阅读 · 0 评论 -
Range Sum Query - Immutable
题目描述:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -原创 2016-06-17 10:17:40 · 225 阅读 · 0 评论 -
Range Sum Query 2D - Immutable
题目描述:Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).The above rectangle (with原创 2016-06-17 11:03:01 · 234 阅读 · 0 评论