自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(42)
  • 收藏
  • 关注

原创 Implement strStr() -- leetcode

Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Update (2014-11-02):The signature of the function had been updated to

2014-12-30 10:24:37 578

原创 Remove Element -- leetcode

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.

2014-12-29 11:25:59 494

原创 Remove Duplicates from Sorted Array -- leetcode

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-12-28 12:04:34 534

原创 Reverse Nodes in k-Group -- leetcode

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is

2014-12-27 21:28:30 438

原创 Swap Nodes in Pairs -- leetcode

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-12-27 18:04:46 402

原创 Merge k Sorted Lists -- leetcode

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.算法一,自底至顶递归。时间复杂度为O(nlogk)。空间复杂度为O(k)。此算法在leetcode上实际执行时间为 144ms (130 test cases)。/**

2014-12-26 18:11:55 416

原创 Generate Parentheses -- leetcode

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-12-25 17:53:30 435

原创 Valid Parentheses -- leetcode

Given a string containing just the characters '(', ')','{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid b

2014-12-25 15:40:36 553

原创 Remove Nth Node From End of List -- leetcode

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 l

2014-12-24 17:45:03 625

原创 Letter Combinations of a Phone Number -- leetcode

Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string

2014-12-23 17:10:02 747

原创 4Sum -- leetcode

Given an array S of n integers, are there elements a,b, c, and d in S such that a + b +c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements

2014-12-22 22:11:47 466

原创 3Sum Closest -- leetcode

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly

2014-12-20 21:41:18 480

原创 3Sum -- leetcode

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) m

2014-12-20 14:36:29 406

原创 Longest Common Prefix -- leetcode

Write a function to find the longest common prefix string amongst an array of strings.方法一,单个字符横向全体比较,纵向逐个的收集。class Solution {public: string longestCommonPrefix(vector &strs) { i

2014-12-19 15:18:26 554

原创 Roman to Integer -- leetcode

Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.class Solution {public: int romanToInt(string s) { int weight[26];

2014-12-19 13:53:09 483

原创 Integer to Roman -- leetcode

Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.class Solution {public: string intToRoman(int num) { char symbol[] = {'I',

2014-12-18 11:06:53 694

原创 Container With Most Water -- leetcode

Given n non-negative integers a1, a2, ...,an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of linei is at (i, ai) and (i, 0). Find

2014-12-17 11:50:31 529

原创 Regular Expression Matching -- leetcode

Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input

2014-12-16 14:05:53 607

原创 Palindrome Number -- leetcode

Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of convertin

2014-12-14 21:48:55 482

原创 String to Integer (atoi) -- leetcode

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 possible input ca

2014-12-14 18:58:02 454

原创 Reverse Integer -- leetcode

Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c

2014-12-13 20:55:06 508

原创 ZigZag Conversion -- leetcode

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I

2014-12-13 18:30:42 446

原创 Add Two Numbers -- leetcode

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-12-12 13:43:56 775

原创 Longest Substring Without Repeating Characters -- leetcode

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo

2014-12-12 10:33:38 776

原创 Median of Two Sorted Arrays -- leetcode

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).下列算法的时间复杂度为O(log (k)), k = mi

2014-12-11 15:02:46 549

原创 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 they add up to the target, w

2014-12-10 21:46:09 351

转载 百度面试题:找出数组中出现次数超过一半的数

答案:创建一个hash_map,key为数组中的数,value为此数出现的次数。遍历一遍数组,用hash_map统计每个数出现的次数,并用两个值存储目前出现次数最多的数和对应出现的次数。这样可以做到O(n)的时间复杂度和O(n)的空间复杂度,满足题目的要求。但是没有利用“一个数出现的次数超过了一半”这个特点。也许算法还有提高的空间。答案2:使用两个变量A和B,其中A存储某个数组

2014-12-10 11:36:24 573

转载 谷歌面试题:如何拷贝特殊链表

有一个特殊的链表,其中每个节点不但有指向下一个节点的指针pNext,还有一个指向链表中任意节点的指针pRand,如何拷贝这个特殊链表?拷贝pNext指针非常容易,所以题目的难点是如何拷贝pRand指针。假设原来链表为A1 -> A2 ->... -> An,新拷贝链表是B1 -> B2 ->...-> Bn。为了能够快速的找到pRand指向的节点,并把对应的关系拷贝到B中。我们

2014-12-10 10:45:52 398

转载 将无向无环连通图转换成深度最小的树

树的深度取决于根节点到最深叶节点的距离,所以我们可以从叶节点入手。叶节点会且只会和某一个节点连通(反之不成立,因为根节点也可能只和一个节点连通),所以我们很容易找到所有可能的叶节点。题目可以等价于找到了两个叶节点,使得两个叶节点之间的距离最远。根节点就是这两个叶节点路径的中间点(或者中间两个点的任意一个)。我们可以每次都将连接度为1的节点删掉,直到最后只剩下1个或2个节点,则这一个节点,或

2014-12-10 10:29:38 897

转载 leetcode-Symmetric Tree 对称树

判断一个二叉树是否是轴对称的是一个经典的算法问题,下面结合leetcode上的Symmetric Tree给出判断对称树的两种方法。      先看看问题描述:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, t

2014-12-10 09:41:05 434

原创 Symmetric Tree -- leetcode

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 followi

2014-12-09 16:48:10 440

原创 Find Minimum in Rotated Sorted Array II -- leetcode

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot u

2014-12-06 19:25:45 470

原创 Find Minimum in Rotated Sorted Array -- leetcode

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

2014-12-06 17:52:45 412

转载 桶排序

桶排序 (Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶子里。每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序)。桶排序是鸽巢排序的一种归纳结果。当要被排序的数组内的数值是均匀分配的时候,桶排序使用线性时间(Θ(n))。但桶排序并不是 比较排序,他不受到 O(n log n) 下限的影响。桶排序以下列程序进行:

2014-12-06 09:35:52 471

转载 数组中数对差最大

题目:数组中某数字减去其右边的某数字得到一个数对之差,求所有数对之差的最大值。例如:数组{2, 4, 1, 16, 7, 5, 11, 9}中,数对之差的最大值是11(16 - 5)分析:看到这个题目,很多人的第一反应是找到这个数组的最大值和最小值,然后觉得最大值减去最小值就是最终的结果。但由于我们无法保证最大值一定位于数组的左边,因此这个思路不管用。让每一个数字逐个减

2014-12-05 16:32:12 595

转载 LeetCode:Best Time to Buy and Sell Stock III

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 at most two transactions.Note:You may not

2014-12-05 15:57:39 415

转载 Best Time to Buy and Sell Stock III -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这道题是Best Time to Buy and Sell Stock的扩展,现在我们最多可以进行两次交易。我们仍然使用动态规划来完成,事实上可以解决非常通用的情况,也就是最多进行k次交易的情况。这里我们先解释最多可以进行k次交易的算法,

2014-12-05 15:53:31 425

原创 Best Time to Buy and Sell Stock -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ 这道题求进行一次交易能得到的最大利润。如果用brute force的解法就是对每组交易都看一下利润,取其中最大的,总用有n*(n-1)/2个可能交易,所以复杂度是O(n^2)。很容易感觉出来这是动态规划的题目,其实跟Maximum Subarra

2014-12-04 18:08:45 402

转载 股市买入卖出时间点选择问题

题目:给你一个股价序列,告诉你每个时间点的股价,问你什么时候买什么时候卖获利最大。时间复杂度越低越好。 解答:方法一:只需要从左往右遍历一遍序列Sequence[N],获得每一个元素对应的左边的最小值即可。例如,设为min[N]。再次遍历序列时,通过计算Sequence[i] - min[i],并获得最大值即可。当然,本题还要求给出买入卖出位

2014-12-04 17:41:28 710

转载 虚函数、虚继承、sizeof

// 练习.cpp : 定义控制台应用程序的入口点。    #include "stdafx.h"  #include   using namespace std;    class A  {  public:      A(){}      A(int a):m_a(a){}      virtual void print()      {

2014-12-04 16:21:54 495

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除