Leetcode
BlitzSkies
这个作者很懒,什么都没留下…
展开
-
Binary Tree Preorder Traversal —— Leetcode
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive soluti原创 2015-09-11 17:04:18 · 601 阅读 · 0 评论 -
Binary Search Tree Iterator —— Leetcode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()原创 2015-09-03 23:09:15 · 451 阅读 · 0 评论 -
Number of Islands —— Leetcode(重要的一类题)
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原创 2015-09-01 20:46:40 · 843 阅读 · 0 评论 -
Different Ways to Add Parentheses —— Leetcode(分治思想)
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 1I原创 2015-09-02 16:22:56 · 774 阅读 · 0 评论 -
实现atoi——从char*到int的转换
int a2i(const char* str);步骤:1. 判断str的第一位*str是否为'-';2.str++;3.以后转换成int;源码:int a2i(const char *s){ int sign=1; if(*s == '-') sign = -1; s++; int num=0; while(*s) { n原创 2015-09-01 10:13:22 · 766 阅读 · 0 评论 -
Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.你可以列出一部分连续数的二进制,也许你会发现2^i次方在其中起得作用,也许你想直接从m &到 n,然而,这并没有什么卵用,所得的结果只能是Time Limited。下面的方法,慢慢体会:class Solut原创 2015-09-01 16:57:39 · 563 阅读 · 0 评论 -
Kth Smallest Element in a BST —— Leetcode
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.Follow up:What if the原创 2015-08-04 11:31:35 · 495 阅读 · 0 评论 -
Binary Tree Paths
Given 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"]二话不说,上代码:原创 2015-08-31 21:40:28 · 477 阅读 · 0 评论 -
Longest Substring Without Repeating Characters —— Leetcode
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2015-08-08 15:01:23 · 434 阅读 · 0 评论 -
Binary Tree Right Side View —— Leetcode(精巧的方法,第二遍)
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原创 2015-05-06 21:55:56 · 1291 阅读 · 0 评论 -
Linked List Cycle —— Leetcode
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?这题大部分刷题书上都有,定义两个指针,一个快一个慢,快的每次走两步,慢的每次走一步,如下情况为有环情况:我的代码还是挺简洁的,如下:/** * D原创 2015-05-05 20:15:07 · 508 阅读 · 0 评论 -
Product of Array Except Self —— Leetcode
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 division and in O原创 2015-09-02 19:47:51 · 429 阅读 · 0 评论 -
Repeated DNA Sequences —— Leetcode(教训,重做)
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri原创 2015-09-03 21:26:11 · 495 阅读 · 0 评论 -
Missing Number —— Leetcode
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 algorithm shoul原创 2015-09-11 15:56:56 · 594 阅读 · 0 评论 -
Letter Combinations of a Phone Number —— Leetcode
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st原创 2015-09-06 17:20:42 · 560 阅读 · 0 评论 -
Insertion Sort List —— Leetcode
Sort a linked list using insertion sort.链表与普通的插入排序最大的区别在于:需记录插入结点和被插入结点的前驱,以便快速插入。我的代码运行时间不是最快的:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next原创 2015-09-10 15:05:44 · 536 阅读 · 0 评论 -
3Sum —— Leetcode
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原创 2015-09-05 20:51:24 · 509 阅读 · 0 评论 -
Find Peak Element —— Leetcode
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, i原创 2015-09-05 15:46:36 · 472 阅读 · 0 评论 -
Reverse Words in a String —— Leetcode
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".Update (2015-02-12):For C programmers: Try to solve it in-place原创 2015-09-09 18:29:50 · 621 阅读 · 0 评论 -
Largest Number —— Leetcode(sort的妙用)
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve原创 2015-09-04 16:42:43 · 643 阅读 · 0 评论 -
Container With Most Water —— Leetcode
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2015-09-02 21:04:52 · 441 阅读 · 0 评论 -
Maximum Product Subarray —— Leetcode
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the larges原创 2015-09-09 10:31:40 · 485 阅读 · 0 评论 -
Two Sum —— Leetcode
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2015-09-04 15:19:48 · 617 阅读 · 0 评论 -
Same Tree —— Leetcode
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.递归之下,简单的不能原创 2015-05-01 22:35:07 · 430 阅读 · 0 评论 -
Add Binary —— Leetcode(重做)
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".这题不难,从后往前;难的是如何使代码变得更简洁,参考如下代码:class Solution {public: string addBinary(s原创 2015-05-04 16:23:23 · 462 阅读 · 0 评论 -
Remove Nth Node From End of List —— Leetcode
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the原创 2015-03-31 10:51:08 · 505 阅读 · 0 评论 -
Min Stack —— Leetcode
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() -- Get原创 2015-03-27 14:54:55 · 472 阅读 · 0 评论 -
Longest Common Prefix —— Leetcode
Write a function to find the longest common prefix string amongst an array of strings.本想用C++写,但想想太没挑战性,干脆直接用C。下面是代码,然而,和很多leetcoder提交的答案一样,在[""]这个测试用例下卡住了,不知为何。方法很简单,就是所有字符串从第一个开始向后遍历,直到有不一样的,即为公共原创 2015-03-31 10:29:55 · 568 阅读 · 0 评论 -
统计一个数的阶乘后面0的个数
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.要求对数时间。首先分析,只有2*5=10才会产生一个0,N!中2的数量永远大于5的数量,所以该题的目标简化为求N!中5的个数。原创 2015-03-23 15:21:39 · 943 阅读 · 0 评论 -
String to Integer (atoi) —— Leetcode
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2015-03-30 15:41:04 · 432 阅读 · 0 评论 -
输出Pascal's Triangle(杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]C++做此题,很简单,但由于STL的vector掌握不好原创 2015-03-22 22:09:12 · 940 阅读 · 0 评论 -
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原创 2015-03-06 18:17:38 · 460 阅读 · 0 评论 -
二进制中1的个数
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 000000原创 2015-03-20 10:24:05 · 487 阅读 · 0 评论 -
链表反转
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy t原创 2015-03-04 21:57:32 · 493 阅读 · 0 评论 -
反转二进制数
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0011100101原创 2015-03-20 10:59:53 · 3204 阅读 · 0 评论 -
版本号的比较
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 co原创 2015-03-24 20:47:36 · 469 阅读 · 0 评论 -
Palindrome Number —— Leetcode(再做一遍)
Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string原创 2015-04-09 22:23:09 · 661 阅读 · 0 评论 -
Plus One —— Leetcode
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.注意点:1. insert的运用 2.原创 2015-05-04 16:57:48 · 470 阅读 · 0 评论 -
Remove Duplicates from Sorted List —— Leetcode
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.一次通过,自认为方法还是比较简洁的,关于头的定原创 2015-05-04 10:04:22 · 476 阅读 · 0 评论 -
Merge Sorted Array —— Leetcode
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements fro原创 2015-05-03 21:15:45 · 455 阅读 · 0 评论