LeeCode
文章平均质量分 61
Bernini_buffalo
这个作者很懒,什么都没留下…
展开
-
LeeCode_003 Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { int L=s.size(); int num=0; int max=0; int tab[300]; int h=0,t=0, h_2=0; fill(tab,tab+30原创 2016-01-29 16:42:38 · 229 阅读 · 0 评论 -
leecode_264 Ugly Number II
Write 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 the first原创 2016-06-10 04:10:42 · 184 阅读 · 0 评论 -
leecode_279 Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n =原创 2016-06-10 06:30:32 · 166 阅读 · 0 评论 -
leecode_220 Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i an原创 2016-06-02 11:31:12 · 217 阅读 · 0 评论 -
leecode_300 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, 101], ther原创 2016-06-12 07:19:45 · 234 阅读 · 0 评论 -
leecode_221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0原创 2016-06-06 11:05:49 · 163 阅读 · 0 评论 -
leecode_222 Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely fille原创 2016-06-07 02:56:27 · 172 阅读 · 0 评论 -
leecode_307 Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.The update(i, val) function modifies nums by updating the element at index i to val.Examp原创 2016-06-14 04:23:59 · 254 阅读 · 0 评论 -
leecode_309 Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2016-06-14 09:10:08 · 166 阅读 · 0 评论 -
leecode_310 Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called mini原创 2016-06-14 13:13:21 · 340 阅读 · 0 评论 -
leecode_313 Super Ugly Number
Write a program to find the nth super ugly number.Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13,原创 2016-06-14 14:36:26 · 279 阅读 · 0 评论 -
leecode_342 Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example:Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without转载 2016-06-16 04:53:02 · 161 阅读 · 0 评论 -
leecode_319 Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off原创 2016-06-17 05:03:32 · 156 阅读 · 0 评论 -
leecode_322 Coin Change
You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of mon原创 2016-06-17 06:32:49 · 206 阅读 · 0 评论 -
leecode_229 Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.这样的元素最多两个//solution from bernin原创 2016-07-26 02:22:20 · 175 阅读 · 0 评论 -
leecode_357 Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x [11,22,33,4原创 2016-06-18 04:00:09 · 249 阅读 · 0 评论 -
leecode_324 Wiggle Sort II
Given an unsorted arraynums, reorder it such that nums[0] nums[2] .Example:(1) Givennums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].(2) Givennums = [1, 3,原创 2016-06-18 04:35:33 · 267 阅读 · 0 评论 -
leecode_260 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:Given原创 2016-06-10 04:02:20 · 211 阅读 · 0 评论 -
leecode_241 Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +,- and *.Example 1原创 2016-06-10 03:50:40 · 184 阅读 · 0 评论 -
leecode_199 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原创 2016-05-25 11:57:59 · 240 阅读 · 0 评论 -
leecode_200 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 assu原创 2016-05-26 14:18:40 · 179 阅读 · 0 评论 -
leecode_201 Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.class Solution {public: int rangeBitwiseAnd(int m, int n) { int i=0; while(m!=n){原创 2016-05-27 04:12:59 · 222 阅读 · 0 评论 -
leecode_208 Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.class TrieNode {public: // Initialize your data structure here. char c; unordered_map sub; TrieNode() {原创 2016-05-28 15:03:02 · 155 阅读 · 0 评论 -
LeeCode_050 Pow(x, n)
Implement pow(x, n).用位操作来实现class Solution {public: double myPow(double x, int n) { if(n<0) { if(n==INT_MIN) return 1.0 / (pow(x,INT_MAX)*原创 2016-02-07 11:25:22 · 175 阅读 · 0 评论 -
leecode_142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?原创 2016-02-26 03:51:42 · 231 阅读 · 0 评论 -
LeeCode_139 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"原创 2016-02-18 10:06:05 · 270 阅读 · 0 评论 -
LeeCode_136 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 e原创 2016-02-18 08:47:00 · 188 阅读 · 0 评论 -
LeeCode_Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2016-02-07 13:58:07 · 174 阅读 · 0 评论 -
LeeCode_055 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.Determine i原创 2016-02-07 15:51:40 · 525 阅读 · 0 评论 -
LeeCode_130 Surrounded Regions
深度优先算法:class Solution {public: void solve(vector>& board) { vector> vec; int m=board.size(); if (m!=0) { int n=board[0].size();原创 2016-02-17 11:31:01 · 236 阅读 · 0 评论 -
leecode_210 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 expressed as原创 2016-05-31 04:05:20 · 279 阅读 · 0 评论 -
leecode_18 4Sum (k Sum)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Element原创 2016-06-01 16:10:04 · 203 阅读 · 0 评论 -
leecode_236 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 between two node原创 2016-06-08 03:55:13 · 190 阅读 · 0 评论 -
leecode_240 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.Integers in原创 2016-06-09 05:44:33 · 161 阅读 · 0 评论 -
leecode_338 Counting Bits
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 an array.Example:Fornum转载 2016-06-18 14:35:05 · 220 阅读 · 0 评论