
leetcode算法心得
gdmmzmj
这个作者很懒,什么都没留下…
展开
-
242. Valid Anagram
问题: Given two strings s and t, write a function to determine if t is an anagram of s.For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false.Note: You may assume原创 2016-04-27 16:28:08 · 288 阅读 · 0 评论 -
169. Majority Element
问题:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alwa原创 2016-04-27 18:32:32 · 250 阅读 · 0 评论 -
217. Contains Duplicate
问题: 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 false if every elemen原创 2016-04-29 09:42:02 · 290 阅读 · 0 评论 -
70. 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-05-02 15:31:48 · 289 阅读 · 0 评论 -
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 00000000原创 2016-05-02 11:13:26 · 280 阅读 · 0 评论 -
263. 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 not ugly since原创 2016-05-02 19:50:50 · 268 阅读 · 0 评论 -
326. 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?我的解答:class Solution {public: bool isPowerOfThree(int n原创 2016-05-02 16:43:55 · 269 阅读 · 0 评论 -
231. Power of Two
问题: Given an integer, write a function to determine if it is a power of two.我的解答:class Solution {public: bool isPowerOfTwo(int n) { if (n<=0) return false;int num = pow(2,(int)(log2(INT_MAX)))原创 2016-05-03 12:42:38 · 315 阅读 · 0 评论 -
202. 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 squares原创 2016-05-04 23:14:43 · 305 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
问题: Say 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 and sell one share of the stock),原创 2016-05-06 09:50:02 · 297 阅读 · 0 评论 -
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 extra me原创 2016-09-18 14:26:05 · 216 阅读 · 0 评论 -
389. 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 was added in原创 2016-09-18 20:10:55 · 241 阅读 · 0 评论 -
383. 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 ransom note can be construc原创 2016-09-18 23:57:03 · 2098 阅读 · 0 评论 -
349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note: Each element in the result must be unique. The result can be in原创 2016-09-20 12:19:32 · 185 阅读 · 0 评论 -
387. First Unique Character in a String
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 = “loveleetc原创 2016-09-20 12:31:19 · 206 阅读 · 0 评论 -
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 loops/re原创 2016-09-21 12:09:15 · 154 阅读 · 0 评论 -
21. Merge Two Sorted Lists
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./** * Definition for singly-linked list. * struct ListNode原创 2016-10-11 01:30:32 · 189 阅读 · 0 评论 -
400. Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231).Example 1:Input: 3Output原创 2016-10-22 21:57:31 · 217 阅读 · 0 评论 -
27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The o原创 2016-09-29 11:03:54 · 162 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree(unsolved)
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.Subscribe to see which companies asked this原创 2016-10-25 23:25:55 · 312 阅读 · 0 评论 -
1. 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, 7, 11,原创 2016-09-30 09:41:15 · 175 阅读 · 0 评论 -
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.class Solution {public: bool isPalindrome(int x) { if(x<0||((x!=0)&&(x%10==0))) return false;原创 2016-09-30 23:46:18 · 154 阅读 · 0 评论 -
205. 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 get t.All occurrences of a character must be replaced with another chara原创 2016-10-01 11:34:42 · 201 阅读 · 0 评论 -
374. Guess Number Higher or Lower
We 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 guess wrong, I’ll tell you whether the number is higher or lowe原创 2016-10-01 14:29:44 · 280 阅读 · 0 评论 -
412. Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For n原创 2016-10-16 22:24:35 · 393 阅读 · 0 评论 -
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. Rectangle Area Assume that t原创 2016-10-16 23:22:23 · 217 阅读 · 0 评论 -
7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321click to show spoilers.Have you thought about this? Here are some good questions to ask before coding. Bonus原创 2016-10-01 17:49:34 · 179 阅读 · 0 评论 -
397. Integer Replacement
Given a positive integer n and you can do operations as follow:If n is even, replace n with n/2. If n is odd, you can replace n with either n + 1 or n - 1. What is the minimum number of replacements原创 2016-10-01 21:01:02 · 209 阅读 · 0 评论 -
88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional原创 2016-10-01 23:21:55 · 172 阅读 · 0 评论 -
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5/** * Definition for singly-linked list.原创 2016-10-02 19:34:01 · 188 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2016-10-02 19:38:29 · 167 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2016-10-03 11:30:39 · 185 阅读 · 0 评论 -
189. 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 you can, th原创 2016-10-03 12:09:42 · 191 阅读 · 0 评论 -
396. Rotate Function
Given an array of integers A and let n to be its length.Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function” F on A as follow:F(k) = 0 * Bk[原创 2016-10-05 20:37:57 · 204 阅读 · 0 评论 -
415. Add Strings(unsolved)
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both原创 2016-11-12 16:40:39 · 155 阅读 · 0 评论 -
204. Count Primes(unsolved)
Description:Count the number of prime numbers less than a non-negative number, n.其他人写法: 主要思路就是质数得从奇数中找,而且考察的该数不能是前面较小的奇数的倍数。class Solution {public://考虑的范围是:首先是奇数,然后质数是那些不是前面奇数的倍数的数 int countPrime原创 2016-11-12 16:42:51 · 166 阅读 · 0 评论 -
453. Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.Example:Input:[1,2,3]Output:3Ex原创 2016-11-12 18:02:55 · 221 阅读 · 0 评论 -
441. Arranging Coins
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be formed.n is a no原创 2016-11-12 20:11:36 · 228 阅读 · 0 评论 -
165. Compare Version Numbers (unsolved)
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 and conta原创 2016-11-12 21:06:59 · 179 阅读 · 0 评论 -
455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cook原创 2016-11-16 17:20:28 · 463 阅读 · 0 评论