- 博客(52)
- 收藏
- 关注
原创 Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in
2015-07-31 22:42:12 215
原创 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 a binary tree node. * public class TreeNode { * int val; * TreeNode
2015-07-31 11:04:19 157
原创 Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public String intToRoman(int num) { if (num <= 0) { r
2015-07-31 00:05:53 192
原创 roman to integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public int romanToInt(String s) { if (s == null || s.
2015-07-30 23:13:50 225
原创 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.
2015-07-30 22:19:03 189
原创 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]
2015-07-30 20:49:16 183
原创 Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \
2015-07-29 21:09:23 263
原创 Product of Array Except Self
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-07-28 22:29:02 236
原创 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-07-28 22:02:37 191
原创 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 false if every element
2015-07-28 20:57:57 197
原创 Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.
2015-07-28 20:20:06 205
原创 Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1/** * Definition for a binary tree node. * public class TreeNode { *
2015-07-28 19:13:14 198
原创 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 a binary tre
2015-07-28 17:52:27 239
原创 Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value
2015-07-28 17:52:02 171
原创 Single Number
Given an array of integers, every element appears twice except for one. Find that single one.public class Solution { public int singleNumber(int[] nums) { int result = 0;
2015-07-28 17:48:29 203
原创 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./** * Def
2015-07-28 17:46:20 190
原创 Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solutio
2015-07-28 17:45:36 213
原创 Binary Tree Preorder Traversal
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]./** * Definition for a
2015-07-28 17:45:10 189
原创 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-07-28 17:41:00 135
原创 Number of 1 Bits
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-07-28 17:40:09 187
原创 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
2015-07-28 17:38:05 204
原创 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on
2015-07-27 17:42:13 174
原创 remove-duplicates-from-sorted-array-ii
容易 删除排序数组中的重复数字 II查看运行结果 29%通过跟进“删除重复数字”:如果可以允许出现两次重复将如何处理?您在真实的面试中是否遇到过这个题? Yes样例给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2,3]。public class Soluti
2015-07-14 12:39:37 197
原创 remove-duplicates-from-sorted-array
容易 删除排序数组中的重复数字查看运行结果 32%通过给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。您在真实的面试中是否遇到过这个题? Yes样例给出数组A =[1,1,2],你的函数应该返回长度2,此
2015-07-14 11:51:37 206
原创 median of two sorted array
http://blog.csdn.net/zxzxy1988/article/details/8587244http://blog.csdn.net/yutianzuijin/article/details/11499917/
2015-07-14 11:24:14 233
原创 search for a range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found
2015-07-12 17:37:15 226
原创 search in rotated sorted array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array retur
2015-07-12 11:46:45 297
原创 first bad version
中等 第一个错误的代码版本查看运行结果 34%通过代码库的版本号是从 1 到 n 的整数。某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错。请找出第一个错误的版本号。你可以通过 isBadVersion 的接口来判断版本号 version 是否在单元测试中出错,具体接口详情和调用方法请见代码的注释部分。
2015-07-11 12:07:36 963
原创 find peak element
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, in
2015-07-11 11:35:38 285
原创 binary search
容易 二分查找查看运行结果 28%通过给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。您在真实的面试中是否遇到过这个题? Yes样例在数组 [1, 2, 3, 3, 4, 5, 10] 中二
2015-07-11 10:28:23 312
原创 recover-rotated-sorted-array
容易 恢复旋转排序数组26%通过给定一个旋转排序数组,在原地恢复其排序。您在真实的面试中是否遇到过这个题? Yes样例[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]挑战使用O(1)的额外空间和O(n)时间复杂度说明什么是旋转数组?比如,原始数组为[1
2015-07-11 08:58:05 253
原创 Search Insert Position
容易 搜索插入位置29%通过给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。你可以假设在数组中无重复元素。您在真实的面试中是否遇到过这个题? Yes样例[1,3,5,6],5 → 2[1,3,5,6],2 → 1[1,3,5,6],
2015-07-10 08:51:48 209
原创 merge-sorted-array
容易 合并排序数组 II36%通过合并两个排序的整数数组A和B变成一个新的数组。您在真实的面试中是否遇到过这个题? Yes样例给出A = [1, 2, 3, empty, empty] B = [4,5]合并之后A将变成[1,2,3,4,5]注意你可以假设A具有足够的空间
2015-07-09 10:31:12 221
原创 jump game ii
中等 跳跃游戏 II33%通过给出一个非负整数数组,你最初定位在数组的第一个位置。数组中的每个元素代表你在那个位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。您在真实的面试中是否遇到过这个题? Yes样例给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃
2015-07-08 12:16:20 353
原创 triangle
容易 数字三角形26%通过给定一个数字三角形,找到从顶部到底部的最小路径和。每一步可以移动到下面一行的相邻数字上。您在真实的面试中是否遇到过这个题? Yes样例比如,给出下列数字三角形:[ [2], [3,4], [6,5,7], [4,1,8,3]]从顶到底部的最小路径和为
2015-07-07 13:39:27 306
原创 minimum-path-sum
容易 最小路径和34%通过给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径。您在真实的面试中是否遇到过这个题? Yes样例注意你在同一时间只能向下或者向右移动一步public class Solution {
2015-07-07 10:00:28 274
原创 第一个ios程序
ios程序的执行过程:1.调用main函数2.调用UIApplicationMain函数3.开启消息循环4.根据传入的类名,创建一个代理对象,并且设置为application的代理5.当应用的生命周期改变的时候,会通知代理对象,调用代理对象的一些方法6.当应用程序加载完毕时,调用- (BOOL)application:(UIApplication *)applicati
2015-07-06 21:11:34 315
原创 climbing-stairs
容易 爬楼梯查看运行结果 40%通过假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部?您在真实的面试中是否遇到过这个题? Yes样例比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法返回 3思路:利用DP的方法,一
2015-07-06 12:33:48 336
原创 unique-paths-ii
容易 不同的路径 II查看运行结果 27%通过跟进“不同的路径”:现在考虑网格中有障碍物,那样将会有多少条不同的路径?网格中的障碍和空位置分别用1和0来表示。您在真实的面试中是否遇到过这个题? Yes样例如下所示在3x3的网格中有一个障碍物:[ [0,0,0], [0,1,0],
2015-07-06 10:32:15 343
原创 unique-paths
容易 不同的路径39%通过有一个机器人的位于一个M×N个网格左上角(下图中标记为'Start')。机器人每一时刻只能向下或者向右移动一步。机器人试图达到网格的右下角(下图中标记为'Finish')。问有多少条不同的路径?您在真实的面试中是否遇到过这个题? Yes样例1,11,2
2015-07-06 09:45:17 1084
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人