- 博客(172)
- 收藏
- 关注
原创 堆排序
从大到小排序,小根堆。小根堆的筛选算法:void sift(int data[], int k, int m){ int i = k ,int j = 2 * k; while (j <= m) { if (j < m && data[j] > data[j + 1]) ++j; if (data[i] < data[j]) break;
2015-03-08 16:04:14
698
原创 冒泡排序
基本冒泡排序冒泡排序是最常见的交换排列。void BubbleSort0 (int data[], int n) // index[1, n]{ for (int i = 1; i < n; ++i) { for (int j = 1; j <= n - i; ++i) { if (data[j] > data[j + 1]) {
2015-03-08 15:33:45
702
原创 C/C++中需要注意的一些问题
1. printf参数的压栈顺序C中printf计算参数时是从右到左压栈的。考虑下面代码的输出结果: int arr[] = { 0, 1, 2, 3 }; int *p = arr; printf("%d, %d\n", *p, *(++p));输出结果为“1, 1”。
2015-03-06 16:52:51
983
原创 统计整型数据二进制形式中1的个数
统计整型数据二进制形式中1的个数可以通过如下方式达到:int cnt1bits(int x) { int count = 0; while (x) { ++count; x = x & (x - 1); } return count;}
2015-03-06 15:52:37
773
原创 LeetCode[String]: Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.这个题目非常简单,只要弄清楚题意就可以非常快地解决,我的C++代码实现如下: string longestCommonPrefix(vector<string> &strs) { if (strs.empt
2015-03-04 17:06:28
995
原创 LeetCode[stack]: Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. - push(x) – Push element x onto stack. - pop() – Removes the element on top of the stack. - to
2015-03-04 16:47:40
1014
原创 C++学习:vector用法
功能函数原型说明构造函数vector();创建一个空vectorvector(int nSize);创建一个vector,元素个数为nSizevector(int nSize,const t& t);创建一个vector,元素个数为nSize,且值均为t
2015-03-04 13:51:53
1037
原创 C++学习:string用法
功能函数原型说明构造函数string(const char *s);用C风格字符串s初始化string(int n,char c);用n个字符c初始化 默认构造函数 复制构造函数访问元素const cha
2015-03-04 10:26:28
1589
原创 C++学习:范围for(range for)语句
语法范围for(range for)语句遍历给定序列中的每个元素并对序列中的每个值执行某种操作,其语法形式是: for (declaration : expression) statement其中: expression部分是一个对象,必须是一个序列,比方说用花括号括起来的初始值列表、数组或者vector或string等类型的对象。这些类型的共同特点是拥有能返回迭代器的beg
2015-03-03 23:53:43
6607
5
原创 LeetCode[String]: Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be
2015-03-03 15:47:08
818
原创 LeetCode[Math]: Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.欲了解罗马数字的拼写规则请参考:http://blog.csdn.net/chfe007/article/details/44037079C++代码实现如下: string
2015-03-03 14:46:32
788
原创 LeetCode[Math]: Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.这个题目首先要了解罗马数字的拼写规则(以下引自维基百科):罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的规则
2015-03-03 13:36:26
928
1
原创 LeetCode[Tree]: 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-03-03 00:23:08
1356
原创 LeetCode[BFS]: Surrounded Regions
Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’. A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region. For example, X X X X X O O X
2015-03-02 18:29:07
742
原创 LeetCode[Map]: Longest Consecutive Sequence
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
2015-03-02 13:14:45
835
原创 桶排序
算法介绍桶排序 (Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶子里。每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序)。桶排序是鸽巢排序的一种归纳结果。当要被排序的数组内的数值是均匀分配的时候,桶排序使用线性时间(O(n))。但桶排序并不是 比较排序,他不受到 O(n log n) 下限的影响。算法步骤如下:
2015-03-01 14:37:40
707
原创 LeetCode[Sort]: Largest Number
LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 953433
2015-02-17 19:55:54
876
原创 Linux下安装Java(JDK)
首先从官网下载Java安装包:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html用如下命令解压:tar -xzvf jdk-8u31-linux-x64.tar.gz会得到jdk1.8.0_31文件夹,建议将该文件夹放在一个相对稳定的路径,比如:sud
2015-02-06 13:05:31
785
原创 LeetCode[Sort]: Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements.
2015-02-04 23:19:30
1168
原创 LeetCode[Hash Table]: Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC"
2015-02-04 20:58:02
972
原创 LeetCode[Sort]: Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].这个题目其实不难,只要理清楚各种情况利用插入排序的方法就可以完成,我的C++代码实现如下:
2015-02-04 15:27:41
1081
原创 LeetCode[Tree]: Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22,return这个题目适合用递归来解,我
2015-02-03 20:22:54
647
原创 LeetCode[Tree]: Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and su
2015-02-03 19:19:21
685
原创 LeetCode[Stack]: Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"这个题目比较简单,用很常规的方法解决即可,测试的时候遇到什么问题再解决就行了。我的C++代码如下:
2015-02-03 19:03:56
745
原创 LeetCode[Tree]: Populating Next Right Pointers in Each Node II
Follow up for problem “Populating Next Right Pointers in Each Node”. What if the given tree could be any binary tree? Would your previous solution still work? Note:You may only use constant ex
2015-02-03 16:27:24
772
原创 LeetCode[Linked List]: Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.一开始我没有采用分治法,解题思路是:首先比较每条链表的第一个元素,找出最小的那个,插入新链表并从原链表删除,如此反复直至所有的链表都为空链表。基于这个愚蠢的解题思路,我的C++代码实现如下:
2015-02-03 15:44:11
723
原创 字符串模式匹配算法之二:KMP算法
KMP算法简介KMP算法全称叫做Knuth-Morris-Pratt Algorithm。被搜索的字符串称为主串,待搜索的字符串称为模式串。我们知道朴素模式匹配算法:http://blog.csdn.net/chfe007/article/details/43448655是很低效的,KMP算法从模式串出发,发现模式串中隐藏的信息来减少比较的次数,具体如何做到的可以移步这个链接:http
2015-02-03 15:07:12
887
原创 字符串模式匹配算法之一:朴素模式匹配算法
被搜索的字符串称为主串,待搜索的字符串称为模式串。朴素模式匹配算法的基本思想:对主串的每一个字符作为子串开头,与模式串进行匹配。对主串做大循环,每个字符开头做模式串长度的小循环,直到匹配成功或全部遍历完成为止。代码实现非常简单: int strStr(char *haystack, char *needle) { for (int i = 0;
2015-02-03 13:35:04
4156
1
原创 设置Git不需要每次push都输入用户名和密码
正如苹果的设计理念中的一条——当你频繁地进行某项操作的时候,做这件事情就会变成一种机械的运动。删除邮件时出现的警告框是如此,git push时每次都需要输入用户名和密码也是如此。那么就可以通过如下的操作来避免每次都需要输入用户名和密码了(以windows为例)。第一步:生成RSA KEY在用户文件夹下点右键运行Git Bush,运行如下命令: ssh-agent bash
2015-02-02 00:35:46
9925
原创 LeetCode[Graph]: Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.这个题目我的思路是用两个vector分别记录原图和副本各个节点的地址来完成复制的。我的C++代码实现如下: UndirectedGraphNode *cloneGraph(Undi
2015-02-01 22:34:58
688
原创 LeetCode[Math]: Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x.这个题目适合用位操作来做,时间和空间复杂度都是O(1)。首先找到根的最高位,然后从最高位开始,依次判断各个位值是否为1。C++代码实现如下: int sqrt(int x) { int i, j, res = 0;
2015-02-01 20:01:53
710
原创 LeetCode[Tree]: Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.这个链接给出了这个题目的完美解法:http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.htm
2015-02-01 19:39:05
643
原创 LeetCode[Array]: Trapping Rain Water
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],
2015-02-01 16:52:55
920
原创 《我是歌手》网上报名评审
相信很多人跟我一样,特别想去《我是歌手》竞演现场领略一下歌手们的表演,所以在网上尝试报名评审团了,报名过程如下:首先打开湖南卫视“我是歌手”官网:http://huodong.hunantv.com/wsgs.html如果没有注册的话需要先注册一个,然后点击“我要做评审”:第一步是身份认证,需要填写如下个人信息:第二步是基础测试,需要回答以下
2015-01-30 21:08:02
7093
原创 C/C++学习:慎用static变量
有时候我们需要用到static变量,有时候我们想限制这些变量的作用域便将其定义在函数内部。但是,如果我们对static变量进行了修改,那么程序在软重启的时候,这些static变量并不会被重新初始化,而是仍然使用上次的值。这是一个潜藏的BUG,一定要注意。所以如果使用static变量,一定要注意这些变量是否需要被修改:如果不需要被修改,那么最好将其定义为const变量;如果需要被修改,那么一
2015-01-29 14:58:22
1324
原创 LeetCode[String]: Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a
2015-01-22 20:30:21
910
原创 Linux下安装ffmpeg
下载ffmpeg从ffmpeg官网:http://ffmpeg.org/download.html下载最新的ffmpeg安装包,然后通过如下命令解压:tar -jxvf ffmpeg-2.5.3.tar.bz2或者用git从github下载:配置本文进行一个比较简单的配置:./configure –enable-shared –p
2015-01-21 22:25:00
6793
原创 VS“不能设置下面的断点 断点未能绑定”的解决办法
先说明一下我遇到的状况:之前VS2012用的好好的,单步调试都没问题,突然就出现了这样的状况(对于之前的工程和新建的工程都是如此):F10单步调试的时候经常直接运行到下一断点(非常偶尔的情况能逐过程运行);F11逐语句运行时情况类似;在调试的过程中,不能设置断点,如果设置断点,会出现如下图所示提示:然后我在网上搜索“VS不能单步调试”相关信息,做了如下尝试:有人
2015-01-21 13:22:46
18145
1
原创 VS2012/VS2013安装教程
VS2012的安装非常简单,首先找到一个支持电脑操作系统位数的安装包。安装开始,选择合适的磁盘位置:如果不是特别有把握直接选择全部功能进行安装:接下来就是等待:在我的电脑上大约经过20多分钟便安装成功:It’s that easy!
2015-01-20 18:56:29
2895
原创 LeetCode[Array]: 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 alw
2015-01-20 16:15:13
1020
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅