自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 怎样用指定的随机数生成函数来实现新的随机数生成函数

阿里巴巴笔试题:假设函数rand_k会随机返回一个【1,k】之间的随机数(k>=2),并且每个整数出现的概率相等。目前有 rand_7,通过调用rand_7()和四则运算符,并适当增加逻辑判断和循环控制逻辑,下列函数可以实现的有:ABCDA:rand_5 B:rand_21 C:rand_23 D:rand_49解析:先考虑如何用rand_7()实现rand_5():

2014-08-27 23:29:11 628

原创 Edit Distance leetcode

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:

2014-08-27 20:50:45 416

原创 Gas Station leetcode

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to

2014-08-27 08:48:17 396

原创 Palindrome Partitioning I and II leetcode

Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","

2014-08-26 19:38:15 342

原创 Combination Sum leetcode

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb

2014-08-22 19:15:15 364

原创 Triangle leetcode

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [

2014-08-21 20:15:07 345

原创 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

2014-08-21 09:49:22 369

原创 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, whe

2014-08-21 08:21:12 327

原创 C++堆,栈,静态存储区介绍

1、栈区(stack)— 由编译器自动分配释放 ,存放函数参数值,局部变量值等。其操作方式类似于数据结构中栈。2、堆区(heap) — 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中堆是两回事,分配方式倒是类似于链表,呵呵。3、全局区(静态区)(static)—,全局变量和静态变量存储是放在一块,初始化全局变量和静态变量在一块区域, 未初

2014-08-20 16:23:39 463

原创 C++ 栈对象 堆对象 理解

在C++中,类的对象建立分为两种,一种是静态建立,如A a;另一种是动态建立,如A* ptr=new A;这两种方式是有区别的。1、静态建立类对象:是由编译器为对象在栈空间中分配内存,是通过直接移动栈顶指针,挪出适当的空间,然后在这片内存空间上调用构造函数形成一个栈对象。使用这种方法,直接调用类的构造函数。2、动态建立类对象,是使用new运算符将对象建立在堆空间中。这个过程分为两步

2014-08-20 16:20:00 8312 2

原创 Jump Game I and II leetcode

Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i

2014-08-20 09:03:04 352

原创 Convert Sorted List to Binary Search Tree leetcode

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.方法一:把

2014-08-19 21:35:54 344

原创 Unique Binary Search Trees II leetcode

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3

2014-08-19 20:26:27 303

原创 Valid Sudoku and Sudoku Solver leetcode

Valid Sudoku

2014-08-18 21:53:02 367

原创 Subsets leetcode

Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For exa

2014-08-18 20:05:00 446

原创 C++中map、hash_map、unordered_map、unordered_set通俗辨析

一、hash_map、unordered_map这两个的内部结构都是采用哈希表来实现。区别在哪里?unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unordered_map比较好。哈希表的好处是什么?查询平均时间是O(1)。顾名思义,unordered,就是无序了,数据是按散列函数插入到槽里面去的,数据之间无顺序可言,但是有些时候

2014-08-18 13:17:56 9381

原创 Longest Consecutive Sequence leetcode

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3

2014-08-18 13:04:18 406

原创 Flatten Binary Tree to Linked List leetcode

Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1

2014-08-16 19:21:18 312

原创 Trapping Rain Water leetcode

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]

2014-08-15 17:12:27 280

原创 Palindrome Number leetcode

Determine whether an integer is a palindrome. Do this without extra space.题意判断数字是否是回文串,只能用常数空间,不能转换成字符串,不能反转数字。先计算数字的位数,分别通过除法和取余,提取出要对比的对应数字。

2014-08-15 16:34:07 310

转载 一随机数以概率 p 生成0,以概率(1-p)生成1,怎样生成等概率的 0 和 1

这涉及到概率 分布的内容了,其实也简单只要能找到一个概率为1/2p的函数就解决了. 另外还有一个比较方便的实现: 一随机数f(x)以概率 p 生成0, 那么设g(x)=f(x)>0?0:1;  刚g(x)以概率 1-p 生成0. 所以f(x),g(x)同时生成0的概率为p(1-p)等于同时生成1的概率. 得等概率随机数 function g(x){   int v=

2014-08-13 19:39:18 8058

转载 随机数范围扩展(如rand7()到rand10())(转)

题目:已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10。分析:要保证rand10()在整数1-10的均匀分布,可以构造一个1-10*n的均匀分布的随机整数区间(n为任何正整数)。假设x是这个1-10*n区间上的一个随机整数,那么x%10+1就是均匀分布在1-10区间上的整数。由于(rand7()-1)*7+rand7()可以构造

2014-08-13 19:27:47 765

原创 Maximum Depth of Binary Tree leetcode

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.Anwser 1 :  DFSclass Solut

2014-08-13 09:06:32 358

原创 Sum Root to Leaf Numbers leetcode

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota

2014-08-13 08:10:16 254

原创 Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]

2014-08-12 20:42:44 273

原创 Populating Next Right Pointers in Each Node II leetcode

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL.What if the given

2014-08-12 19:19:47 279

原创 Pascal's Triangle II leetcode

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?

2014-08-12 15:09:23 282

原创 leetcode 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

2014-08-11 20:16:08 362

原创 leetcode Reverse Words in a String

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.Clarification:What constitutes

2014-08-04 16:08:15 400

原创 leetcode Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m

2014-08-04 14:24:56 452

原创 leetcode Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]

2014-08-04 12:42:23 313

原创 leetcode Linked List Cycle II 单链表中的环问题集锦

Linked List CycleGiven a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?如何判断一个单链表中有环?Linked List Cycle IIGiven a linked list,

2014-08-02 08:31:20 536 1

原创 leetcode Container With Most Water

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 line i is at (i, ai) and (i, 0). Fin

2014-08-01 17:37:01 300

原创 leetcode Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solut

2014-08-01 15:43:17 307

原创 leetcode 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

2014-08-01 09:22:19 417

空空如也

空空如也

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

TA关注的人

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