MyLeetcode
1. 用自己的思维和代码
2. 扩散,举一反三
VintNee
老夫聊发少年狂
十年生死两茫茫
一树梨花压海棠
展开
-
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 Credits原创 2015-01-03 21:16:21 · 1430 阅读 · 0 评论 -
Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ...原创 2015-01-03 21:22:58 · 993 阅读 · 0 评论 -
Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.题意: 求n!后面有多少个0, 尾部这里就不考虑负数和0了, 这种情况直接认为0方法1:0只可能是10, 4x5原创 2015-01-03 17:57:42 · 1149 阅读 · 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 评论 -
Linked List Cycle
一次ACGiven a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?/** * Definition for singly-linked list. * struct ListNode { * in原创 2013-12-11 11:51:01 · 755 阅读 · 0 评论 -
Rome to Intger
一次AC关键是读懂题意记数方法基本字符IVXLCDM相应的阿拉伯数字表示为1510501005001000原创 2013-12-11 02:47:54 · 1087 阅读 · 0 评论 -
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2013-12-11 02:01:58 · 944 阅读 · 0 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2013-12-10 23:56:58 · 853 阅读 · 0 评论 -
leetcode开篇
受猛人鼓舞, 开始leetcode之路.时间暂定三月.向者耕耘于杭电,北大acm .奈何题量浩繁,种类各异,良莠不齐.加之无人指点,好似永无止境,致锐气尽脱,心灰意懒. leetcode题量适中,还得到高人源码与博文,喜不自胜.特记之leetcode: http://oj.leetcode.com/src1: https://github.com/iphkwan原创 2013-12-10 18:21:25 · 1235 阅读 · 0 评论 -
Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Total Accepted: 4309 Total Submissions: 12898My SubmissionsGiven a binary tree, return the preorder traversal of its nodes' values.For example:Given bi原创 2013-12-14 19:36:48 · 746 阅读 · 0 评论 -
Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2013-12-15 09:01:01 · 803 阅读 · 0 评论 -
Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2013-12-15 11:30:14 · 766 阅读 · 0 评论 -
Plus One
Given a number represented as an array of digits, plus one to the number.class Solution {public: vector plusOne(vector &digits) { int len = digits.size(); int i; int原创 2013-12-15 12:01:17 · 793 阅读 · 0 评论 -
Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2013-12-15 19:32:55 · 717 阅读 · 0 评论 -
Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2013-12-15 22:35:46 · 873 阅读 · 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 评论 -
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?这题如果用递归, 超时了, 只能模拟了原创 2013-12-18 14:42:29 · 790 阅读 · 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 评论 -
Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?相比之前,原创 2013-12-18 16:41:48 · 739 阅读 · 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 评论 -
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 评论 -
n皇后问题
原理很简单#include using namespace std;int x[20]; //解向量int sum; //可行方案个数int n;//在第t列是否可放置bool place(int t){ int i; for(i=1; i<t; i++) //如果 行-行 == 列-列 或者 在同一列。 if(abs(t-i) == abs(x[t]-x[i])原创 2014-02-12 17:32:00 · 944 阅读 · 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 评论 -
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-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 评论 -
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-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-3Sum
class Solution {public: vector > threeSum(vector &num) { vector>result; vectorvec; int len = num.size(); //assert(len<3); sort(num.begin(),num.end());原创 2014-06-05 08:59:12 · 759 阅读 · 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-gas station
Gas Station Total Accepted: 12395 Total Submissions: 50855My SubmissionsThere are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car w原创 2014-06-06 10:57:35 · 849 阅读 · 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-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-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-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 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-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 评论