自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

jeason29的专栏

爱户外,爱生活

  • 博客(25)
  • 收藏
  • 关注

原创 Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate element must exist. Assume that there is only one duplicate number,

2015-09-28 15:27:20 828 3

原创 两个排序数组第k大的数

double findKth(int a[], int m, int b[], int n, int k){ //always assume that m is equal or smaller than n if (m > n) return findKth(b, n, a, m, k); if (m == 0) return b[k -

2015-09-25 12:06:14 904

原创 Valid Phone Numbers

题目描述:Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.You may assume that a valid phone number must

2015-09-23 10:00:41 361

原创 move zeros

solution1:双指针思路,遍历一次class Solution {public: void moveZeroes(vector& nums) { int i=0; //Not Zero int j=0; //Zero int l=nums.size(); while(j<l && nums[j]!=0)

2015-09-22 11:54:12 580

原创 Kth Smallest Element in a BST

题目描述:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:W

2015-09-22 09:11:35 285

原创 翻转二叉树

nvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howel

2015-09-22 08:59:42 371

原创 在数组中找到出现频率大于1/4的数

算法的核心思想很简单,每次删去不同的4个数,最后剩下的元素有可能是频繁项。假设数组有15个元素,若一个元素的出现频率大于1/4,其必须出现4次。不妨设数组为{1,2,1,4,1,4,2,9,1,7,4,3,9,4,3},d表示删去该数。我们来模拟一下算法的过程。第一次 1d,2d,1,4d,1,4,2,9d,1,7,4,3,9,4,3 剩下 1,1,4,2,1,7,4,3,9,4,

2015-09-17 09:04:27 871

原创 Lowest Common Ancestor of a Binary Search Tree (BST)

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 betwee

2015-09-16 14:52:03 324

原创 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-09-16 14:40:50 350

转载 子树判断

//遍历Tree1,查找与Tree2 root相同的节点 boolean HasSubtree(TreeNode root1, TreeNode root2){ boolean result = false; if(root1 != null && root2 != null){ if(root1.val == root2.val){

2015-09-16 13:22:42 413

原创 连续最长子序列

问题给出一个未排序的整数数组,找出最长的连续元素序列的长度。如:给出[100, 4, 200, 1, 3, 2],最长的连续元素序列是[1, 2, 3, 4]。返回它的长度:4。你的算法必须有O(n)的时间复杂度 。初始思路要找连续的元素,第一反应一般是先把数组排序。但悲剧的是题目中明确要求了O(n)的时间复杂度,要做一次排序,是不能达到的。

2015-09-16 10:32:28 2360

原创 Unique Binary Search Trees II

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

2015-09-15 13:37:15 325

原创 Different Ways to Add Parentheses

Example Input: "2*3-4*5"(2*(3-(4*5))) = -34((2*3)-(4*5)) = -14((2*(3-4))*5) = -10(2*((3-4)*5)) = -10(((2*3)-4)*5) = 10Output: [-34, -14, -10, -10, 10]class Solution {public: v

2015-09-15 13:34:30 255

转载 binary tree recursion problem

1. Recover Binary Search TreeTwo elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty

2015-09-15 10:37:05 383

原创 第n个丑数

Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 

2015-09-15 10:11:57 335

原创 Integer to English Words 整数转为英文单词

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.For example,123 -> "One Hundred Twenty Three"12345 -> "Twelve Thousand Th

2015-09-15 09:56:37 543

原创 Perfect Squares

题意:定义perfect squares number 为(1,4,9,16,...) 给出一个正整数n,返回和为n的最小的perfect squares number 的数目。例如:n=12 ,返回3, 因为 12=4+4+4 ;            n=13,返回2,因为13=4+9 。分析:动态规划,squares[i]表示i的最小perfect squares n

2015-09-14 17:06:36 355

原创 将一个矩阵翻转90度

今天碰到这个问题,后来发现这个问题的最优解比较有意思,如下:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place? 解题思路1:先将矩阵

2015-09-14 13:52:26 764

原创 Subset I & II

一:subset I题目: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 duplicat

2015-09-14 10:42:42 282

原创 linux下用fdisk命令制作U盘启动盘

FDISK进行硬盘分区从实质上说就是对硬盘的一种格式化。当我们创建分区时,就已经设置好了硬盘的各项物理参数,指定了硬盘主引导记录(即MasterBootRecord,MBR)和引导记录备份的存放位置。而对于文件系统以及其他操作系统管理硬盘所需要的信息则是通过之后的高级格式化,即Format命令来实现。用一个形象的比喻,分区就好比在一张白纸上画一个大方框。而格式化好比在方框里打上格子。安装各种软件就

2015-09-11 15:51:32 986

转载 K路归并

问题描述    将k个已经排序的数组归并成一个大的排序的结果数组。这些数组可能数量比较大,以至于不能直接装载到内存中。    这个问题比较有意思。后面一截的描述是我个人加上去的。实际上一个简单的模型就是将k个已经排好序的数组或者序列合并成一个排好序的结果数组。那么我们该怎么来考虑这个问题呢? 初步分析    其实这个问题可以说是一个问题的更加普遍形式。回顾一

2015-09-10 17:00:33 850

原创 strstr实现(练习KMP算法的好例子)

solution 1.暴力破解class Solution {public: int strStr(string haystack, string needle) { int m = haystack.size(); int n = needle.size(); for(int i = 0; i <= m-n; i ++)

2015-09-10 16:45:09 375

原创 Check whether two strings are anagram of each other

Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be diff

2015-09-10 16:36:49 312

原创 MSI-X中断的好处

MSI(Message Signaled Interrupt)是PCI2.2提出的新的中断处理形式,即,有中断产生时在系统特定内存地址写入中断数据已通知CPU一个中断。该种方式脱离了中断引脚(PIN)带来的数目限制,并且延迟小、效率高。MSI的扩展MSI-X在3.0里定义,相较MSI,MSI-X支持更多的消息数量(2048)以及独立的消息地址。MSI支持32个消息,一个MSI地址。

2015-09-10 15:30:19 2544

原创 do...while妙用

1、避免goto语句:通常,如果一个函数开始要分配一些资源,然后如果在中途遇到错误则要退出函数,当然,退出前要释放资源,我们的代码可能如下:  1 #defien N 10 2 3 bool Execute() 4 { 5 // 分配资源 6 int *p = (int *)malloc(N * sizeof(int)); 7 bool

2015-09-10 14:36:12 338

空空如也

空空如也

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

TA关注的人

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