leetcode
文章平均质量分 71
小灰灰52309
图像处理方向,大家多多交流
展开
-
Lowest Common Ancestor of a Binary Search Tree
题目:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined原创 2016-08-27 19:13:35 · 330 阅读 · 0 评论 -
Contains Duplicate II
题目:Contains Duplicate IITotal Accepted: 72866Total Submissions: 237922Difficulty: EasyGiven an array of integers and an integer k, find out whether there are two distinct原创 2016-08-27 19:52:03 · 308 阅读 · 0 评论 -
Pow(x, n)
题目:Implement pow(x, n).Subscribe to see which companies asked this question分析:n属于INT_MIN时的n=0-n会溢出n的最大值,需特别考虑代码:class Solution {public: double myPow(double x, int n) {原创 2016-08-27 21:11:41 · 335 阅读 · 0 评论 -
Reverse Linked List
题目:Reverse a singly linked list.click to show more hints.分析:定义三个指针,分别指向当前节点,当前节点的下一个节点,和下一节点的下一节点,然后反转前两个节点,然后当前节点后移。代码:/** * Definition for singly-linked list. * struct ListNode { *原创 2016-08-28 11:38:46 · 301 阅读 · 0 评论 -
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 gett.All occurrences of a character must be replaced with原创 2016-08-28 11:54:57 · 337 阅读 · 0 评论 -
Happy Number
题目: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 squa原创 2016-08-28 12:51:21 · 315 阅读 · 0 评论 -
Remove Linked List Elements
题目:Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5分析:考虑多种情况,只有一个节点,前面的节点等原创 2016-08-28 13:13:10 · 303 阅读 · 0 评论 -
Count Primes
题目:Description:Count the number of prime numbers less than a non-negative number, n.分析: 计算出一个素数表,然后计算出小于n的素数个数。代码: class Solution {public: int countPrimes(int n) {原创 2016-08-28 14:12:11 · 417 阅读 · 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 adjacen原创 2016-08-28 14:44:23 · 295 阅读 · 0 评论 -
Number of 1 Bits
题目:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).For example, the 32-bit integer ’11' has binary representation 00原创 2016-08-28 14:56:33 · 328 阅读 · 0 评论 -
Find the Difference
题目:Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that w原创 2016-08-29 09:20:53 · 336 阅读 · 0 评论 -
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 as001原创 2016-08-30 10:22:23 · 247 阅读 · 0 评论 -
Rotate Array
题目:Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to[5,6,7,1,2,3,4]. Note:Try to come up as many solutions as原创 2016-08-30 17:18:43 · 199 阅读 · 0 评论 -
leetcode Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given nums = [2,原创 2016-08-20 12:50:08 · 209 阅读 · 0 评论 -
leedcode ZigZag Conversion
ZigZag ConversionThe 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 legibi原创 2016-08-20 14:01:31 · 190 阅读 · 0 评论 -
leedcode Ransom Note
Ransom Note Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the原创 2016-08-20 14:15:08 · 206 阅读 · 0 评论 -
Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that appearsmore than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority elem原创 2016-08-30 20:22:30 · 199 阅读 · 0 评论 -
Excel Sheet Column Title
题目:Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 分析:移原创 2016-08-30 21:30:45 · 184 阅读 · 0 评论 -
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 a原创 2016-08-31 11:03:58 · 157 阅读 · 0 评论 -
Guess Number Higher or Lower
leetcode题目374. Guess Number Higher or LowerWe are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you g原创 2016-08-21 13:50:56 · 230 阅读 · 0 评论 -
Reverse Integer
Reverse IntegerReverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321分析:求余求商迭代到最后商为0,注意:翻转后的数可能会越界,做好处理代码:class Solution原创 2016-08-21 14:29:15 · 159 阅读 · 0 评论 -
Binary Tree Paths
Binary Tree PathsGiven a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]原创 2016-08-21 15:37:40 · 169 阅读 · 0 评论 -
Intersection of Two Arrays
Intersection of Two ArraysTotal Accepted: 39169Total Submissions: 87928Difficulty: EasyGiven two arrays, write a function to compute their intersection.Example:Given nu原创 2016-08-21 16:24:49 · 238 阅读 · 0 评论 -
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()原创 2016-08-31 20:17:12 · 186 阅读 · 0 评论 -
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?分析:此题还是值得考虑的,判断链表有没有环 一开始使用了复杂度O(n^2)的方法,使用两个指针a,b。a从表头开始一步一转载 2016-08-31 21:17:05 · 205 阅读 · 0 评论 -
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 usin原创 2016-08-31 22:44:14 · 174 阅读 · 0 评论 -
Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return[2, 2].Note:Each element in the result should appear as many ti原创 2016-08-22 14:04:28 · 169 阅读 · 0 评论 -
Power of Four
Power of FourGiven 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原创 2016-08-22 16:56:12 · 186 阅读 · 0 评论 -
Invert Binary Tree
题目:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1分析:分析左右子树情况,利用递归代码:/** * Definition for a binary tree nod原创 2016-08-22 18:01:28 · 170 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
题目:Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie,原创 2016-09-01 10:04:16 · 186 阅读 · 0 评论 -
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.For example:Given the below binary tree a原创 2016-09-01 10:45:15 · 163 阅读 · 0 评论 -
Path Sum
题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.分析:使用递归代码:/** * Definiti原创 2016-09-01 11:00:16 · 168 阅读 · 0 评论 -
Balanced Binary Tree
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never原创 2016-09-01 11:33:21 · 256 阅读 · 0 评论 -
Power of Three
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为质数,3^n最大值对3^n求余为0代码:class Solution {p原创 2016-08-22 22:13:14 · 220 阅读 · 0 评论 -
Maximum Depth of Binary Tree
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.分析:递归求最大深度代码:/** * Definit原创 2016-09-01 14:36:45 · 159 阅读 · 0 评论 -
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,null,15,7], 3 / \原创 2016-09-01 15:31:24 · 180 阅读 · 0 评论 -
Binary Tree Level Order Traversal 1+2
题目:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,n原创 2016-09-01 15:41:59 · 165 阅读 · 0 评论 -
First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.分析: 使原创 2016-08-23 09:29:00 · 248 阅读 · 0 评论 -
Merge Sorted Array
题目:Given two sorted integer arrays nums1 and nums2, merge nums2 intonums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal tom + n) to hol原创 2016-09-01 17:44:04 · 193 阅读 · 0 评论 -
Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?分析:此题为斐波那契数列,当前数的值为前两个数值的原创 2016-09-01 21:49:38 · 205 阅读 · 0 评论