
leetcode OJ
文章平均质量分 78
gsama
这个作者很懒,什么都没留下…
展开
-
Plus One
今天是个比较简单的题,但还是不能"Accepted"Problem: Given a number represented as an array of digits, plus one to the number有两点,一个是array digits[0]应该存储的是数据的高位另一点是如果array是999,加1会进位,需要新的array长度为digits.length+1找不出原创 2014-01-29 06:19:26 · 402 阅读 · 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.Easy one, if you原创 2014-02-11 09:45:03 · 392 阅读 · 0 评论 -
Remove Element
Remove Element Total Accepted: 7901 Total Submissions: 24070Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be原创 2014-02-12 07:09:59 · 388 阅读 · 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原创 2014-02-12 07:08:08 · 444 阅读 · 0 评论 -
permutation I & II
Permutations Total Accepted: 7785 Total Submissions: 25366Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3],原创 2014-02-15 23:53:24 · 521 阅读 · 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).Tree related problems are very hard to understand for me. So far, this one原创 2014-02-18 01:38:30 · 356 阅读 · 0 评论 -
sqrt(x)
Sqrt(x) Total Accepted: 7788 Total Submissions: 36159Implement int sqrt(int x).Compute and return the square root of x.Some one suggest using binary search, However a Newton method原创 2014-02-18 01:33:11 · 639 阅读 · 0 评论 -
First Missing Positive
Back to LeetcodeProblem:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should ru原创 2014-04-23 02:29:31 · 400 阅读 · 0 评论 -
Remove Nth Node From end of List
Question: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 fr原创 2014-05-19 03:24:22 · 370 阅读 · 0 评论 -
Evaluate Reversed 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", "1",原创 2014-04-28 05:36:47 · 400 阅读 · 0 评论 -
LRUCache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if原创 2014-04-28 04:30:01 · 453 阅读 · 0 评论 -
Maximum Subarray
Question: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 subarra原创 2014-05-21 12:40:55 · 392 阅读 · 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 th原创 2014-02-04 01:18:41 · 473 阅读 · 0 评论 -
Merge K Sorted List
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.1. scan every head of each list, find the minimum value, remove minimum one to the result ListNode原创 2014-02-11 09:42:21 · 466 阅读 · 0 评论 -
Add Two Number
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原创 2014-02-10 10:10:12 · 457 阅读 · 0 评论 -
Single number and Single Number IIt
题目分别是:Given an array of integers, every element appears twice except for one. Find that single one.Given an array of integers, every element appears three times except for one. Find that sin原创 2014-01-29 08:43:59 · 448 阅读 · 0 评论 -
Linked List Merge Sort
开始刷leetcode,进度缓慢...以下我的解答,不过time limit exceeded。要求是in O(n log n) time using constant space complexity.估计是跑two-runner部分太慢了...问了大神,他的解法是bottom-up merge sort,借助了queue的帮助贴自己答案/** * Definition for singly-原创 2014-01-28 05:41:17 · 565 阅读 · 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.要比convert sorted linked list简单/** * Definition for binary tree * public class TreeNode { * i原创 2014-02-05 12:55:44 · 406 阅读 · 0 评论 -
String to Integer(atoi)
经典题目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 possibl原创 2014-02-04 09:07:36 · 416 阅读 · 0 评论 -
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.目前为止提交次数最少的一次。很简单,都是LinkedList基本操作/** * Definiti原创 2014-02-05 04:32:03 · 434 阅读 · 0 评论 -
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思路:已经是sorted list并且按升序排列,那么和in-order traversal的顺序是一致的,所以采用类似in-order的顺序来构建麻烦的点是recursion原创 2014-02-05 12:52:56 · 445 阅读 · 0 评论 -
Valid Parentheses
使用stack简单一题做的复杂了public class Solution { public boolean isValid(String s) { Stack st = new Stack(); int i = 0; char bracket = '0'; boolean flag原创 2014-02-04 10:36:07 · 367 阅读 · 0 评论 -
3 sums
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 (a,b,c原创 2014-02-04 05:23:10 · 522 阅读 · 0 评论 -
merge intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].又是逻辑缺陷所以检查了很久才ac的一道。By using of self-defined c原创 2014-02-08 00:04:50 · 576 阅读 · 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?3 solutionsStill c原创 2014-02-09 02:29:52 · 391 阅读 · 0 评论 -
set matrix zeros
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Copied method from:http://www.programcreek.com/2012/12/leetcode-set-matrix-zeroes-java/This pro原创 2014-02-09 04:07:13 · 507 阅读 · 0 评论 -
Reverse Words in a String
3rd time begin!!Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarificat原创 2014-07-01 23:31:49 · 447 阅读 · 0 评论