leetcode
VintNee
老夫聊发少年狂
十年生死两茫茫
一树梨花压海棠
展开
-
leetcode:Two Sum
struct Node { int val; int index; Node(){} Node(int x,int y):val(x),index(y){} }; bool compare(const Node &aa, const Node &bb) { return aa.val<bb.v原创 2014-02-18 10:47:09 · 880 阅读 · 0 评论 -
Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2014-03-28 00:02:32 · 1309 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; *原创 2014-03-27 23:43:08 · 1022 阅读 · 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,#,#,15,7}, 3 / \ 9 20原创 2014-03-08 19:16:25 · 956 阅读 · 0 评论 -
Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota原创 2014-03-08 16:33:50 · 735 阅读 · 0 评论 -
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2014-03-08 21:11:48 · 925 阅读 · 0 评论 -
Pow(x, n)
Implement pow(x, n).第一个版本:double pow(double x, int n){ if(n==0) return 1.0; if(n<0) return 1.0/pow(x,-n); return x*pow(x,n-1);}超时第二个版本:class Solution {public: double pow(double原创 2014-03-08 18:18:04 · 1238 阅读 · 0 评论 -
Palindrome Number
很2的一个解法, 好多情况没考虑, 只考虑正数了class Solution {public: bool isPalindrome(int x) { if(x<0)return false; int numbers = x; //位数 int digit = 0; int times = 10;原创 2014-02-18 21:16:27 · 1168 阅读 · 0 评论 -
Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "原创 2014-02-26 09:30:28 · 828 阅读 · 0 评论 -
Reverse Linked List II
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 the fol原创 2013-12-22 10:34:40 · 852 阅读 · 0 评论 -
Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to原创 2013-12-22 07:58:41 · 754 阅读 · 0 评论 -
Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.一个小错误, 简直可以说是笔误, 浪费我近两个小时悲夫~原创 2013-12-22 09:51:21 · 741 阅读 · 0 评论 -
Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [原创 2013-12-22 07:12:14 · 801 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.方法相当2b之前判定超时, 于是用到了class Solution {public: int sqrt(int x) { if(x==0) return 0;原创 2013-12-18 17:34:26 · 1190 阅读 · 0 评论 -
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 3But the f原创 2014-04-01 15:24:32 · 869 阅读 · 0 评论 -
leetcode--Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".Have you been asked this question in an interview? YesDiscus原创 2014-04-06 18:55:23 · 1139 阅读 · 0 评论 -
leetcode-Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { int hash[256]; for(int i=0;i<256;i++) hash[i] = -1; int len = s.length(); int i;原创 2014-09-18 14:30:51 · 791 阅读 · 0 评论 -
leetcode-list
自己整理一下leetcode的list.jf原创 2014-09-18 10:36:27 · 730 阅读 · 0 评论 -
leetcode-Add Two Numbers
这里进位是向后的,个位在前面下面的代码是个位原创 2014-09-18 16:18:43 · 748 阅读 · 0 评论 -
leetcode-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原创 2014-06-22 16:59:34 · 939 阅读 · 0 评论 -
leetcode-Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()原创 2014-06-22 02:48:53 · 867 阅读 · 0 评论 -
leetcode-Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.Th原创 2014-06-21 21:32:34 · 827 阅读 · 0 评论 -
Leetcode-Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.原创 2014-06-22 02:28:33 · 914 阅读 · 0 评论 -
leetcode-Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.原创 2014-06-21 23:43:07 · 1007 阅读 · 0 评论 -
leetcode-Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.原创 2014-06-21 23:29:49 · 1131 阅读 · 0 评论 -
leetcode-3sumclosest
class Solution {public: int threeSumClosest(vector &num, int target) { int len = num.size(); sort(num.begin(),num.end()); int diff = INT_MAX; int result;原创 2014-06-05 09:15:19 · 822 阅读 · 0 评论 -
leetcode-Binary Tree Zigzag Level Order Traversa
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-06-05 07:55:42 · 987 阅读 · 0 评论 -
leetcode-longest common prefix
class Solution {public: string longestCommonPrefix(vector &strs) { int size = strs.size(); if(size==0) return ""; if(size==1) return strs[原创 2014-06-04 15:04:14 · 870 阅读 · 0 评论 -
leetcode-String to Integer (atoi)
class Solution {public: int atoi(const char *str) { bool negative = false; while(*str==' ')str++ ; if(*str=='-') { negative = true;原创 2014-06-04 11:57:21 · 865 阅读 · 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]]Discusscl原创 2013-12-18 16:28:09 · 699 阅读 · 0 评论 -
Search a 2D Matrix
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 from left to right.The first integer of each原创 2013-12-18 16:56:04 · 733 阅读 · 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 and sum原创 2013-12-15 21:58:55 · 727 阅读 · 0 评论 -
Same Tree
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/**原创 2013-12-14 04:23:03 · 688 阅读 · 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.先贴一个我超时的代码:/** * Definition for原创 2013-12-14 04:17:56 · 717 阅读 · 0 评论 -
Single Number
Single Number Total Accepted: 6830 Total Submissions: 14936My SubmissionsGiven an array of integers, every element appears twice except for one. Find that single one.Note:Your algori原创 2013-12-14 03:05:28 · 747 阅读 · 0 评论 -
Merge Two Sorted Lists
Merge Two Sorted Lists Total Accepted: 4404 Total Submissions: 13615My SubmissionsMerge two sorted linked lists and return it as a new list. The new list should be made by splicing toget原创 2013-12-13 11:25:14 · 712 阅读 · 0 评论 -
Remove Nth Node From End of List
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原创 2013-12-13 10:48:16 · 733 阅读 · 0 评论 -
Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.原创 2013-12-13 07:11:30 · 742 阅读 · 0 评论 -
Remove Duplicates from Sorted List
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.本欲援笔立就, 奈何两次夭折其一, 略else原创 2013-12-13 06:52:56 · 746 阅读 · 0 评论 -
Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2013-12-13 06:40:18 · 779 阅读 · 0 评论