
高效计算
ojshilu
https://github.com/lucky521
展开
-
数组中出现次数超过一半的数字 MoreThanHalfNum
9oj编号1370. 剑指29. 编程之美的发帖水王。普通方法:排序,然后找位于中间位置的数。进阶方法:Hash映射次数。高级方法:擂台PK法。排序法(堆排序)的代码:#include //向堆中加入新元素并调整void MinHeapFixup(int a[], int i){ int j, temp; if(i==0) return;原创 2013-09-28 17:51:11 · 1365 阅读 · 0 评论 -
寻找最长的括号匹配 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 "()",原创 2014-02-27 14:02:07 · 1384 阅读 · 0 评论 -
股票最大收益问题3 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.给出一个数组,这一次,要求的原创 2014-02-28 21:49:36 · 1807 阅读 · 0 评论 -
寻找最少次跳数 - 贪心VS动态规划 Jump Game 2
非常好的问题: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.Your原创 2014-03-14 12:35:44 · 4565 阅读 · 0 评论 -
股票最大收益 Best Time to Buy and Sell Stock I
题目源自于leetcode。题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one sha原创 2013-11-20 16:34:28 · 1797 阅读 · 0 评论 -
方形矩阵顺时针旋转90度 Rotate Image
题目源自于Leetcode。原地变形题。题目:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).要求,原地进行,空间复杂度为O(1)。思路:观察规律就知道把每个点A[i][j]放到A[j][n-1-j]去。为了节省空间,我必须连着串有序的挨个原创 2013-11-26 13:29:57 · 3352 阅读 · 0 评论 -
交错字符串 Interleaving String
题目源自待字闺中的微信。判断字符串c是否是字符串a和字符串b按顺序的交错(interleave)。关键问题:判断一个字符串是否是另一个字符串的子序列。需要用O(n)时间来解决。int issubsequence(char *m, char *s){ if(m == NULL || s == NULL) return NULL; int i=0,j=0; while(原创 2013-09-17 13:46:10 · 2025 阅读 · 0 评论 -
寻找不在数组中最小的正整数 First Missing Positive
问题:给出一个无序的数组,其中包含有任意的整数。现在要求返回不包含在数组中的最小的正整数。要求:时间复杂度O(n),空间复杂度O(1)。思路:如果不要求空间复杂度,可以用hash、map进行统计。但是现在,不允许借助临时空间。但是对于无序数组,不借助空间怎么可能统计的出来。因此,唯一的出路,就是把输入数组自己作为临时空间使用。这道题用了一个类似计数排序的技巧。把一个正数是否存在这一原创 2014-03-08 20:14:49 · 3170 阅读 · 0 评论 -
直方图蓄水问题 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],原创 2014-02-20 21:45:57 · 2061 阅读 · 0 评论 -
找出序列中求和最接近于target的三个数 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原创 2013-12-14 15:36:44 · 2731 阅读 · 0 评论 -
数组中,有三个数只出现1次,其他都出现2次,找出来那三个数。 3 Unique
问题:数组中,有三个数只出现1次,其他都出现2次。找出来那三个数。注意:由于3是奇数,所以这三个数都相同的位异或起来也会是1。思路:假设将数组中所有数异或起来之后。假如某一位为0,说明所求那三个数在这一位是000或者110. 假如某一位为1,说明所求那三个数在这一位是100或者111.因此根据这个异或结果没办法找出一个位进行有效的分组。原创 2014-03-20 10:23:56 · 2055 阅读 · 0 评论 -
寻找排列数中的第k个数 Permutation Sequence
问题:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""2原创 2014-03-24 21:11:32 · 2303 阅读 · 0 评论 -
寻找直方图中的最大矩形 Largest Rectangle in Histogram
题目:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.For example,Given height = [2,1,5,6,2原创 2014-02-20 22:18:35 · 1463 阅读 · 0 评论 -
读取文件中的随机一行 Random Probability
假设有一个文本文件,文件中有若干行。要求返回随机的一行。每行的被选概率相同。两种情况:1、如果文件很大,不能全放入内存2、如果是文件流大文件的特点是不能载入内存随机读取,文件流的特点是只能读取一次。//伪代码i = 1chosen_line = ""while line has next:if random() < 1/i: # random return原创 2014-02-25 10:49:01 · 4088 阅读 · 4 评论 -
Euler: Amicable numbers
Amicable numbersThis is the 21st issue of Euler Project Challenge Game.Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).原创 2015-03-09 19:03:21 · 1483 阅读 · 0 评论 -
寻找循环有序数组的最小值 Find Minimum in Rotated Sorted Array
二分查找是典型的分治思想,主要应用于有序顺组相关的查找问题。典型原创 2014-10-20 10:14:02 · 2578 阅读 · 0 评论 -
数组的最大子数组积 Maximum Product Subarray
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the large原创 2014-10-20 10:40:24 · 1272 阅读 · 0 评论 -
寻找最长回文子串 Longest Palindrome DP解法
Longest Palindrome回文基础:如果子串P是回文,那么xPx是回文。如果子串P不是回文,那么xPx不是回文。根据回文基础,我们可以得到动态规划的状态转移函数。设置状态量LP[i][j]来表示任意某个子串a[i]...a[j]是否是回文。那么,1. 初始条件:空串 看作是回文的最初始条件,LP[i][i-1]=1。这作为初始状态,并不认为原创 2013-09-29 11:01:33 · 1571 阅读 · 0 评论 -
寻找两个不等长数组的中位数 Median of Two Sorted Arrays
题目源自于Leetcode。经典好题。题目:给出两个有序数组,长度不一定相同,一个是m一个是n,要求给出他们合并在一起之后的数组的中位数。要求时间复杂度为O(log(m+n)),所以不可以合并数组再找中位数,否则复杂度就是O(m+n)。数组的中位数如果有序数组有奇数个元素,那么中位数是最中间那个元素。如果有序数组有偶数个元素,那么中位数是中间两个元素的平均数。原创 2013-11-10 14:42:02 · 5019 阅读 · 1 评论 -
树的最大路径和 Binary Tree Maximum Path Sum
问题:从二叉树中找出一个路径,使其途径的节点权值之和最大。路径是随意的,首末结点在哪儿都可以,也不要求必须经过根节点。由于结点元素有正有负,并非路径越长和越大。思路一:递归思路。找出从左孩子向下到叶子结点的最大单向路径和,找出从右孩子向下到叶结点的最大单向路径和,两者相加再加上根节点本身的值,这是经过根节点的最大路径和。然后分别递归求两个孩子的最大路径和。三个最大路径和从中选最大值。原创 2014-03-04 20:48:13 · 2763 阅读 · 0 评论 -
在无序序列中找出最长的连续序列 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,原创 2014-03-03 22:23:30 · 1753 阅读 · 0 评论 -
两个字符串的最小编辑距离 Edit Distance
问题: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-03-05 14:40:40 · 5634 阅读 · 0 评论 -
大数的乘法 Multiply Strings
# include# include# include void multiply(char* a,char* b,char* c){ int i,j,ca,cb,* s; ca=strlen(a); cb=strlen(b); s=(int*)malloc(sizeof(int)*(ca+cb)); for (i=0;i<ca+cb;i++)原创 2013-08-22 16:16:32 · 1514 阅读 · 1 评论 -
荷兰国旗问题 将3种数中重复数聚集 Sort Colors
题目源自于leetcode。好题。普通方法很好想,高效方法想半天。题目:有一个数组,元素只有0,1,2这三种,现在要将其排序。0全放前面,2全放后面,1放中间。要求:只遍历一趟。空间复杂度为O(1)。思路: 换个角度,海阔天空。起初我一直想用左右端指针做,把左端的非0换成0,把右端的非2换成2。但是到中间部位就乱了,因为左右端是同时进行的。 看了网上的解答。方法非常原创 2013-11-28 21:22:44 · 1588 阅读 · 0 评论 -
只用位运算来实现整数的加减乘除四则运算
问题一: 位运算实现加法问题二:只用加法实现减法、乘法、除法问题三:只用加法实现1+2+3+...+n,(循环、判断语句也不用)下面逐一解决:问题三:方法1:只用加法实现1+2+3+...+n1、利用递归来代替循环结构;2、利用&&与运算的特性来代替if结构。int add(int n, int &sum)原创 2013-09-05 22:21:38 · 15903 阅读 · 5 评论 -
计算1~n之间的所有十进制整数中1的出现次数
问题是这样的,给出一个n,让你求出1~n之间的所有整数的十进制形式中的1的总个数。本问题假设n>1。比如给出一个数n=12,那么1到12之间的数1,2,3,4,5,6,7,8,9,10,11,12里面,能数出来是有5个1。最直观的方法:依次判断每一个区间内的每一个整数,对一个数逐渐除10判断其每一位。这个方法的时间复杂度为O(n*lgn)。介绍一个优化的方法(注意其中分治和原创 2013-12-03 10:49:36 · 1813 阅读 · 1 评论 -
找出求和为0的三个数 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原创 2013-12-15 22:32:10 · 2100 阅读 · 0 评论 -
Hash表的一种实现
Hash是在数据统计和海量数据处理中经常使用到的一个方法和数据结构。Hash支持的外部操作:插入新数据、查找数据。(一般不支持删除数据)Hash的使用包括两个重要部分:一个是Hash函数,一个是存储方法。Hash函数:把数据集的一个单元转换成hashID。比如要存储一个个字符串,就需要把字符串转换为hashID。存储方法:如何组织数据集的数据。这里涉及到一个问题就是,相同h原创 2014-02-17 09:56:57 · 965 阅读 · 0 评论 -
逆转链表 Reverse Linked List
#include#include#includeusing namespace std;typedef struct node{ int data; struct node *next;}node,*List;int main(){ List head = NULL; node *n; int input; cin>>input; while(input != -原创 2013-07-12 15:43:22 · 1440 阅读 · 0 评论 -
蛇形螺旋矩阵的生成和遍历 Spiral Matrix
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]一个n*n矩阵像上面这样,自然数从1到n,由外到里螺旋者走,这样叫Spiral Matrix。现在有两件事:第一件事是让你生成一个这样的矩阵;第二件事是给你一个普通的矩阵,让你按照这样的顺序(Spiral Order)来遍历这个矩阵。数学上位置和数字似乎没有什么特别原创 2014-02-27 20:10:51 · 2410 阅读 · 1 评论 -
字符串窗口覆盖 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 "原创 2014-02-25 21:37:36 · 1115 阅读 · 0 评论 -
求一个序列的逆序数 Inversion Count for an array
在这篇博文《通过交换相邻数来完成排序所需要的最少交换次数》中,提到了逆序数的概念。它的用处还是很广泛的。本文研究一下怎么求逆序数。按照定义来做的直观求解方法的时间复杂度是O(n^2)的,这个复杂度是不理想的。有没有O(nlogn)的方法呢?当然了,一种优化的求法引入了分治思想(Divide and Conquer)。分治思想的两个经典应用,都在解决排序问题上,一个是归并排序算法,一个是快速原创 2014-01-01 20:13:12 · 2547 阅读 · 1 评论 -
丑数 Ugly Numbers
问题:Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …shows the first 11 ugly numbers. By convention, 1 is included. Write a program to原创 2013-12-29 16:22:04 · 1677 阅读 · 0 评论 -
二分查找有序数组中某个数的所在范围 Search for a Range
题目源自于leetcode。二分查找题。题目:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the原创 2013-12-04 20:38:52 · 3444 阅读 · 0 评论 -
有重叠区间段中寻找最值 Pinterest面试题
题目源自于待字闺中的微信,是Pinterest公司的一道面试题。题目:给如下的数据格式:比如有一组数据:1, 3, 1002, 4, 2005, 6, 300……这些数据时间区间可能有重合的部分。比如在时间段2~3之间,value的和是100+200 = 300.。要求找出这组数据中最高的value和。思路: 不好的做法:看到题目原创 2013-12-10 10:38:55 · 2088 阅读 · 0 评论 -
序列中,除了2个数字只出现一次外,其他数字成对出现。找出落单的2个数。
本题是上一题的续篇,扩展篇。想把这一问题先转化为上一个问题,然后解决掉。于是需要将序列分成2类,正好把两个落单的数划分到两个类中去。1,位运算的优先级真是搞不清楚。以后遇上^&|>,这样的运算外都用小括号括上。 if((a[i]&cls) == 0) 这句话 如果不给&加上小括号,就会出错。2,第一次先把序列异或起来,得到的结果其实就是两个落单的数的异或。分析结果,结果的二进制中为1的原创 2013-07-15 21:59:29 · 2418 阅读 · 0 评论 -
一个序列里的数字都是成对的,只有一个是单个的,找出来。
#include#includeusing namespace std;int main(){ int n,result=0; scanf("%d",&n); if(n%2 == 0) { printf("impossible!\n"); //这样的序列的个数一定是奇数个元素 return 0; } int *a = new int[n]; f原创 2013-07-15 21:07:52 · 2254 阅读 · 1 评论 -
杨氏矩阵中的查找问题
1,太不细心了:行列不分!!与或不分!!2,边界值要想清楚。3,申请完内存要释放。4,scanf来替代cin5,整型数组的scanf也得有&#include#includeusing namespace std;int main(){ int row_num, col_num, value; int index_row, index_col; int f原创 2013-07-13 10:06:57 · 1338 阅读 · 0 评论 -
判断出入栈序列的合法性 Stack Sequence
给出一个入栈序列,一个出栈序列。判断,是否存在合法的出入栈操作,满足这样的入栈序列和出栈序列。思路:故事重现一遍,能顺利完成所有操作就存在,中间出现矛盾就不存在。所有数都要在栈里走一回。如果栈为空,没的出,只能是要入栈;如果栈顶元素不是要出栈的元素就继续入栈;如果栈顶元素是要出栈的元素,就要出栈(注意只有这样才能出栈);如果最后出栈序列的元素都出来了,栈也为空了原创 2014-03-02 20:21:14 · 2687 阅读 · 0 评论 -
二分算法实现幂函数x的n次方 Pow(x, n)
题目源自于leetcode。二分算法。题目:Implement pow(x, n).思路: 应该先考虑清楚,再下笔coding。 特殊情况:x取0时,0^n=0。 x取1时,1^n=1。 n取0时,x不能为负数,x^0=1。 x取负原创 2013-11-29 20:42:55 · 6892 阅读 · 2 评论