
leetcode
最大的敌人是自己
欢迎大牛指点
展开
-
283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your原创 2016-04-19 16:02:02 · 246 阅读 · 0 评论 -
2. 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原创 2016-04-20 15:18:30 · 227 阅读 · 0 评论 -
Contains Duplicate
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 f原创 2016-04-20 16:40:03 · 230 阅读 · 0 评论 -
112. 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原创 2016-04-20 20:24:34 · 212 阅读 · 0 评论 -
58. 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 def原创 2016-04-20 21:00:41 · 254 阅读 · 0 评论 -
118.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]]我的解决思路: 第n行的元素个数为n,就想到了数字三角,可原创 2016-04-21 10:03:47 · 279 阅读 · 0 评论 -
119. 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?我的解决思路: 先画出二维数组看规原创 2016-04-21 10:43:22 · 362 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
问题描述: 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.java代码:/** * Definition for a binar原创 2016-04-21 11:59:57 · 256 阅读 · 0 评论 -
136. Single Number
问题描述: Given an array of integers, every element appears twice except for one. Find that single one.我的解决思路: 数组里的每个元素都出现两次除了某一个数,将数组排好序后,将相邻的两个数进行异或操作,相同的两个数异或结果为0。如果异或的结果不为0,则a[i]就是要找的那个数,不可能是a[i+1],注原创 2016-04-21 22:05:05 · 289 阅读 · 0 评论 -
137. Single Number II
问题描述: Given an array of integers, every element appears three times except for one. Find that single one我的解决思路: 数组中的每个元素都出现三次,除了某个数,这里的这个数可能出现一次也可能出现两次,所以在后面的异或操作中应该写成(nums[i]^nums[i+2])!=0。java代码:pu原创 2016-04-21 22:33:29 · 240 阅读 · 0 评论 -
260. Single Number III
问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums原创 2016-04-21 22:57:53 · 256 阅读 · 0 评论 -
343. Integer Break
问题描述: Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, r原创 2016-04-22 12:05:51 · 256 阅读 · 0 评论 -
235. 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 betw原创 2016-04-22 13:05:14 · 255 阅读 · 0 评论 -
155. 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() – G原创 2016-04-20 13:11:57 · 213 阅读 · 0 评论 -
28. Implement strStr()
问题描述: Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.下面附代码:public class Solution { public int strStr(String haystack, S原创 2016-04-20 11:59:17 · 219 阅读 · 0 评论 -
171. Excel Sheet Column Number
**问题描述: Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27 AB -> 28** 自己原创 2016-04-19 17:24:12 · 267 阅读 · 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 elem原创 2016-04-19 17:52:42 · 251 阅读 · 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.**我的思路:由于最近在复习归并排序,所以就采取了类似的方法,大牛勿喷,欢迎指点下面附上代码:/**原创 2016-04-19 19:41:02 · 230 阅读 · 0 评论 -
100. 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.**解决方法:用分治原创 2016-04-19 16:42:57 · 249 阅读 · 0 评论 -
226. Invert Binary Tree
以下博客纯属上手,大牛们勿喷。 Invert a binary tree.4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Google: 90% of our engineers use the software you wrote原创 2016-04-19 15:48:34 · 231 阅读 · 0 评论 -
283. Move Zeroes
问题描述: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2016-04-19 19:49:59 · 262 阅读 · 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 s原创 2016-04-19 23:18:09 · 316 阅读 · 0 评论 -
198. 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 adjace原创 2016-04-20 07:58:47 · 270 阅读 · 0 评论 -
24. 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 spac原创 2016-04-20 08:43:05 · 250 阅读 · 0 评论 -
232. Implement Queue using Stacks
问题描述: Implement the following operations of a queue using stacks.push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue. peek() – Get the front element. e原创 2016-04-20 09:26:04 · 224 阅读 · 0 评论 -
83. 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.我的解决思路: 用一个引用指向当前的节点,如果p原创 2016-04-20 10:04:09 · 220 阅读 · 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,原创 2016-04-20 11:34:25 · 270 阅读 · 0 评论 -
53. Maximum Subarray
题目描述: Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1]原创 2016-04-22 18:15:03 · 314 阅读 · 0 评论