C++
文章平均质量分 62
EricGuo55
兜兜转转,团团圆圆
展开
-
(LeetCode 326)Power of Three
po主第一篇LeetCode练习题: Given an integer, write a function to determine if it is a power of three.Follow up: Could you do it without using any loop / recursion? 意思就是判断一个数是不是3的幂次方,尽量不用循环与递归?solution: 使用递原创 2016-04-15 23:24:46 · 696 阅读 · 4 评论 -
(LeetCode 299) Bulls and Cows (HashTable)
Q: 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 provide a hint原创 2016-04-19 12:23:38 · 462 阅读 · 0 评论 -
(LeetCode 279) Perfect Squares
Q: 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 = 13, re原创 2016-04-19 10:36:01 · 336 阅读 · 0 评论 -
(LeetCode 101) 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 题意就是问一个二叉树是不是对称的。sol原创 2016-04-18 23:32:31 · 264 阅读 · 0 评论 -
(LeetCode 223) Rectangle Area
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. Assume that the total area is原创 2016-04-19 21:58:14 · 595 阅读 · 0 评论 -
(LeetCode 130)Surrounded Regions(并查集)
Q: Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.For example, X X X X X O O X X X O X原创 2016-04-20 22:07:34 · 3805 阅读 · 0 评论 -
(LeetCode 200)Number of Islands(并查集、DFS)
Q: 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 ass原创 2016-04-20 23:23:59 · 3632 阅读 · 0 评论 -
并查集(Union-Find)算法介绍
原博地址:http://blog.csdn.net/dm_vincent/article/details/7655764本文主要介绍解决动态连通性一类问题的一种算法,使用到了一种叫做并查集的数据结构,称为Union-Find。更多的信息可以参考Algorithms 一书的Section 1.5,实际上本文也就是基于它的一篇读后感吧。原文中更多的是给出一些结论,我尝试给出一些转载 2016-04-20 23:27:42 · 429 阅读 · 0 评论 -
(LeetCode 16) 3Sum Closest
Q: 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 have exac原创 2016-04-22 00:36:28 · 358 阅读 · 0 评论 -
(LeetCode 130) Surrounded Regions(BFS)
Q: Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.For example, X X X X X O O X X X O X原创 2016-04-20 16:45:54 · 1742 阅读 · 0 评论 -
(LeetCode 15) 3Sum
Q: 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: Elements in a triplet (a,b,c) must原创 2016-04-22 00:57:42 · 381 阅读 · 0 评论 -
(LeetCode 342) Power of Four
Q: 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 loop原创 2016-04-19 10:56:11 · 556 阅读 · 0 评论 -
(LeetCode 322) Coin Change
Q: 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 amount of mon原创 2016-04-18 11:16:46 · 598 阅读 · 0 评论 -
(LeetCode 264) Ugly Number II
Q: 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 10原创 2016-04-18 21:58:40 · 349 阅读 · 0 评论 -
(LeetCode 191) 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 000000000原创 2016-04-16 00:29:33 · 341 阅读 · 0 评论 -
(LeetCode 190) Reverse Bits
Q: 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 0011100101原创 2016-04-16 01:16:42 · 374 阅读 · 0 评论 -
(LeetCode 231)Power of Two
题: Given an integer, write a function to determine if it is a power of two.也就是判断一个数是不是2的幂次方solution: 这道题解法很多,最简单的就是循环。 已知2的幂次方的二进制中有且只有一位为1。 我们向右移动,直到当前位为1。再在当前状态右移一位,如果是零那么就是2的幂次方。class Solution {原创 2016-04-15 23:54:43 · 460 阅读 · 0 评论 -
(LeetCode 303) Range Sum Query - Immutable
Q: 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) -> 1 sumRange(2, 5) -> -1 sumRange(原创 2016-04-16 09:49:57 · 354 阅读 · 0 评论 -
(LeetCode 304) Range Sum Query 2D - Immutable
Q: 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). Range Sum Query 2D The above rectang原创 2016-04-16 12:47:54 · 364 阅读 · 0 评论 -
(LeetCode 307) Range Sum Query - Mutable(树状数组讲解)
Q: 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. Example:原创 2016-04-17 15:45:04 · 3219 阅读 · 0 评论 -
(LeetCode 307) Range Sum Query - Mutable(Segment Tree)
Q: 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. Example:原创 2016-04-17 15:10:10 · 1136 阅读 · 0 评论 -
(LeetCode 202) Happy Number
Q: 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 squares原创 2016-04-17 17:11:39 · 296 阅读 · 0 评论 -
(LeetCode 79) Word Search
Q: Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically原创 2016-04-18 21:04:41 · 370 阅读 · 0 评论 -
poj1664 放苹果(递归)
嘿嘿,这道题解法有好几种,原创 2014-04-12 22:32:30 · 667 阅读 · 0 评论 -
(LeetCode 18) 4Sum
Q: 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: Elements in a原创 2016-04-22 01:04:44 · 396 阅读 · 0 评论