自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

转载 求全排列

本文取自July的《编程之法》一书。求n个字符的n!个全排列。解法一:       从字符串中选出一个字符作为排列的第一个字符,然后对剩余的字符进行全排列。如此递归处理,从而得到所有字符的全排列。如字符串为“abc”,按一下步骤进行。将a固定在第一位,求后面bc的排列,得到:“abc”和“acb”。将b固定在第一位,求后面ac的排列,得到:“bac”和“bca”。将c固定在

2016-07-28 22:19:27 475

原创 折半查找算法的正确实现

从一个有序数组中查找元素,可以用折半查找,时间复杂度为O(logn)。该算法理解起来比较简单,但能否正确实现的人并不多。下面给出该算法的正确实现int binarySearch(int *arr, int size, int key) { int left = 0; int right = size - 1; /*如果上面这句是int right = size的话,则下面有两

2016-07-28 10:35:49 1991

转载 红黑树和平衡二叉树 区别

红黑树和平衡二叉树区别如下:1、红黑树放弃了追求完全平衡,追求大致平衡,在与平衡二叉树的时间复杂度相差不大的情况下,保证每次插入最多只需要三次旋转就能达到平衡,实现起来也更为简单。2、平衡二叉树追求绝对平衡,条件比较苛刻,实现起来比较麻烦,每次插入新节点之后需要旋转的次数不能预知。平衡二叉树又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度

2016-07-25 22:52:49 1136

原创 找出现奇数次的两个数

一个数组中,有两个数出现了奇数次,其余的数都出现偶数次,找出这两个数。用异或操作可在O(n)时间复杂度和O(1)空间复杂度内找到。    设要找的两个数为A和B,第一步让所有的数异或,结果就是A^B ,记为C,则C中一定有不为0的二进制位,假设第k位。则A或B的第k位不为0,数组中的其余的数中若有第k位不为0的数对,则A或B与这些数对异或,可得到A或B,及异或数组中所有第k位不为0的所有数

2016-07-24 22:14:12 1587

转载 找数组中唯一重复的元素

1-1000放在含有1001个元素的数组中,只有唯一的一个元素值重复,其它均只出现一次.每个数组元素只能访问一次,设计一个算法,将它找出来;不用辅助存储空间。(1)   方法一:(当N为比较大时警惕溢出)将1001个元素相加减去1,2,3,……1000数列的和,得到的差即为重复的元素。int Find(int* a) { int i; for (i

2016-07-24 11:00:49 363

原创 寻找最小的k个数

从n个书中找出最小的k个数。解法有如下几种:(1)全部排序之后找前k个,时间为O(n*n)(2)部分排序。维护一个大小为k的数组a[k],遍历n个数的过程中更新数组a。时间为O(k*n)(3)维护一个容量为k 的大顶堆,遍历过程中更新堆。时间为O(nlogk)。(4)下面着重介绍一种平均时间复杂度为O(n)的算法,线性选择算法。该算法的思想类似于快速排序。

2016-07-24 09:29:29 267

原创 快速旋转数组(或字符串)

介绍一种时间复杂度为O(n),空间复杂度为O(1)的算法。俗称三步翻转法。例如想把“123456”旋转为“456123”,第一步,旋转123为321,第二部旋转456为654,整个变为“321654”,第三步对此整体做一次旋转,即变为“456123”class QuickReverse {private: void reverse(int arr[], int begin, int e

2016-07-23 22:10:32 355

原创 Java遍历容器注意问题

今天写了个小程序,要遍历LinkedList容器,遍历过程会进行删除操作,allJobs是一个LinkedList,代码:for(int i=0; i<allJobs.size(); i++){ Job j = allJobs.get(i); if(j.requstTime <= job.duration +job.startTime){ readyJobs.

2016-04-22 21:53:55 661

原创 数字的拆分问题和换零钱问题

一、(1)输入n,和k,问将n用1到k这k个数字进行拆分,有多少种拆分方法。例如:n=5,k=3 则有n=3+2, n=3+1+1, n=2+1+1+1, n=2+2+1, n=1+1+1+1+1这5种拆分方法。  这个问题是动态规划问题,用a[i][j]表示考虑到用数j进行拼接时数字i的拼接方法,可以得到状态转移方程如下:a[i][j]=a[i][j-1]+a[i-j][j-1]+a[i

2016-03-30 18:59:14 489

转载 求两个有序数组的中位数(扩展求第k大元素)

数组A和数组B都是已经排好序的数组,求其中位数。Solutions:  开辟一个数组,返回其中位数。时间复杂度O(n),空间也是O(n)。class Solution {public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int size1=nums1.size(),size2=num

2015-09-29 17:36:09 567

原创 判断是否是二叉查找树

给定一个二叉树,判断它是否是合法的二叉查找树(BST)一棵BST定义为:节点的左子树中的值要严格小于该节点的值。节点的右子树中的值要严格大于该节点的值。左右子树也必须是二叉查找树。样例一个例子: 1 / \ 2 3 / 4 \ 5解答:容易忽略根节点要大于所有左子树

2015-09-24 21:22:12 648

原创 First Bad Version(二分查找的应用)

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the

2015-09-23 21:12:00 309

原创 求字典序的下一个排列(对应lc的Next Permutation)

如果字典序的下一个排列不存在,则返回升序排序。过程如下:(1) 求满足关系式pj-1       I=max{ j | pj-1(2) 求满足关系式pj-1(3) 交换pj-1和ph的值(4) 从pj开始进行逆序排列class Solution {public: void nextPermutation(vector& nums) { if(n

2015-06-20 16:38:51 679

原创 Remove Duplicates from Sorted Array

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

2015-06-14 16:02:30 294

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

2015-06-14 15:32:54 291

原创 Generate Parentheses

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:"((()))", "(()())", "(())()", "()(())", "()()

2015-06-14 11:38:41 327

原创 Letter Combinations of a Phone Number

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 st

2015-06-13 23:25:37 351

原创 3sum和4sum(从数组中选3/4个数,使其和为目标值)

都要求结果集中不能有重复的。且每个子集中的元素为非递减顺序。可以从实现O(N^(k-1))的复杂度。利用求两个数相加和为target的算法,扩展到3个和4个的情况。一下给出4个时的程序,3个的情况可以类推。class Solution {public: vector > fourSum(vector& nums, int target) { vector >

2015-06-04 16:49:15 576

原创 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 linei is at (i, ai) and (i, 0

2015-06-03 00:04:40 359

原创 删除链表倒数第N个元素

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

2015-06-02 22:31:06 447

原创 实现atoi函数

要求:The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or m

2015-06-01 23:22:08 291

原创 Two sum(在数组中找两个数,使其和为指定值)

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, where

2015-05-31 17:08:05 426

原创 求字符串中最长回文

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.Hide Tags Stri

2015-05-31 00:13:24 496

原创 不用额外空间判断一个数是否是回文形式

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

2015-05-29 00:44:09 412

原创 Longest Substring Without Repeating Characters

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

2015-05-28 11:01:23 314

原创 将整数翻转输出

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

2015-05-27 23:51:15 467

原创 [LeetCode]Add Two Numbers

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

2015-05-27 00:21:45 302

原创 Excel Sheet Column Title(1->A, 2->B, 28->AB )

Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB SOLUT

2015-05-21 17:20:23 387

原创 ZigZag Conversion(Z形排列的字符/数,求其横向观察结果)

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

2015-05-21 16:28:56 738

原创 Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",

2015-05-19 22:40:39 334

原创 Add Binary(二进制字符串相加)

Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".Solutions:刚开始走了弯路,将2进制字符串转化为整型int,相加后结果转化为2进制string。麻烦且字符串太长的话会出错。后来用一个进

2015-05-19 16:44:43 490

原创 判断二叉数是否是平衡树

Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe

2015-05-19 11:47:00 462

原创 Repeated DNA Sequences(统计字符串出现次数)

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri

2015-05-18 22:16:07 447

原创 实现字典树Trie的基本操作

字典树:根节点不包含字符,除根节点外每一个节点都只包含一个字符; 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串; 每个节点的所有子节点包含的字符都不相同。实现字典树的插入insert(),查找search(string key),判断是否有以某字符串为前缀的字符串starsWith(string s)。实现代码如下,可以将bool hasWord改为一个int型来统计

2015-05-16 21:41:07 540

原创 用fork创建进程树

创建一个如下图所示的进程树。用getpid()和getppid()梳理其关系。要注意的是fork之后的子进程和父进程共享代码段,并且如果有写的情况下会有各自的数据段。#include#include#include#includeint main(int argc, char *argv) { printf("My pid is %d\n", getpid()); pid_

2015-05-16 19:53:01 13218 1

原创 求数组中出现次数查过n/2的元素

前提:此数组不为空,且这样的元素存在solutions:分析:可以将数组排序,出现在n/2+1位置的一定是所求。故排序的话也可以只排前n/2+1个。class Solution {public: void quikSort(vector& nums, int low1, int high1) { //int low=0, high=nums.size(); int

2015-05-11 22:02:56 563

原创 求一个数的阶乘后面有多少个0

Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Solutions:n!可以分解为n!=2x*3y*5z*....。可以得知结果中0的个数取决于5z*。(1,n)的

2015-05-10 10:13:12 951

原创 实现二分查找树的迭代器(Binary Search Tree Iterator )

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()

2015-05-09 16:10:32 580

原创 HouseRobber(即求一串数中不连续的数之和中最大的)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house

2015-05-09 12:01:15 366

原创 二叉树层序遍历应用之Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1

2015-05-07 17:22:41 399

空空如也

空空如也

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

TA关注的人

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