算法
风空
无间路上,做最真实的自己
展开
-
字符串的全排列
#include<iostream>#include<vector>#include<cstring>using namespace std;string s;int a[100];void dfs_print(int n){ if(n==s.size()) { for(int i=0; i<n; i++) cout<<s[a[i]原创 2016-04-05 17:14:47 · 263 阅读 · 0 评论 -
leetcode
Given two words word1 and word2, find the minimum number of steps required to convertword1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word:原创 2016-05-18 20:48:08 · 299 阅读 · 0 评论 -
死锁的原因
产生死锁的原因主要是:(1) 因为系统资源不足。(2) 进程运行推进的顺序不合适。(3) 资源分配不当等。如果系统资源充足,进程的资源请求都能够得到满足,死锁出现的可能性就很低,否则就会因争夺有限的资源而陷入死锁。其次,进程运行推进顺序与速度不同,也可能产生死锁。产生死锁的四个必要条件:(1) 互斥条件:一个资源每次只能被一个进程使用。(2) 请求与保持条件:一个进转载 2016-03-14 10:07:15 · 219 阅读 · 0 评论 -
二叉树的深度
/*struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}};*/class Solution {public: int TreeDepth(TreeNode*原创 2016-04-02 15:36:24 · 268 阅读 · 0 评论 -
多重部分和(2)
题目: 有n种不同大小的数字a, 每种各m个. 判断是否可以从这些数字之中选出若干使它们的和恰好为K #include using namespace std;int A[3]= {3,5,8};int B[3]= {3,2,2};int n=3;int K=17;bool d原创 2016-04-10 22:27:51 · 248 阅读 · 0 评论 -
多重部分和问题 递归搜索解法
//对于部分和的问题,我们通常会用DP来做,其实对于这类问题,在我们一时之间想不到好的解决方法的时候,我们可以先用深度搜索来做#include using namespace std;int A[3]={3,5,8};int B[3]={3,2,2};int n=3;int K=17;static int i1=0;bool dfs(int i,int j){原创 2016-04-10 21:24:17 · 378 阅读 · 0 评论 -
完全背包问题
#include #include using namespace std;const int maxn=100;int d[maxn][maxn];int n=3;int w[3]={3,4,2};int v[3]={4,5,3};int max(int i,int j){ return i>j?i:j;}int dfs(int i,int j){ in原创 2016-04-08 21:36:43 · 223 阅读 · 0 评论 -
最长公共子序列,记忆化搜索
#include <iostream>#include <string>using namespace std;const int maxn=100;int max(int i,int j){ return i>j?i:j;}string s1,s2;int d[maxn][maxn];//记忆化搜索void dfs(int i,int j){ if(i>=s1.s原创 2016-04-08 20:54:16 · 490 阅读 · 2 评论 -
求一组数的全部子集算法
求出全部的子集,有很多用处,例如下面这个题,在时间复杂度要求不高的情况下,可以用全排列来求。怎样将数字的全排列算法,用到具体的题目中去:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary w原创 2016-04-29 10:36:27 · 715 阅读 · 1 评论 -
动态规划
Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s ="aab", Return1翻译 2016-05-10 22:02:47 · 285 阅读 · 0 评论 -
Excel的列编号 例如:A对应1,Z对应26,AA对应27,AZ对应52 ......
//今天华为笔试的一道上机题 Excel的列编号 例如:A对应1,Z对应26,AA对应27,AZ对应52 ......随机输入一个字符串求他对于数字,当时想了一个比较复杂的算法,虽然也OJ了,但是总是觉得开辟内存太多,后来发现其实挺简单的。总结:每道题一定要在思路清晰之后在动手敲代码,可以借助纸笔等工具来清晰你的思路,否则花很多时间。而且算法的复杂度也不高。#include #inclu原创 2016-04-09 18:11:16 · 16788 阅读 · 2 评论 -
堆排序
//堆排序的最坏,平均,最好时间复杂度为O(nlogn),是不稳定的排序,空间复杂度是O(1).#include <iostream>using namespace std;int A[9]={0,1,7,8,3,5,6,7,4};int n=8;void sift(int low,int high){ int i=low,j=2*i; int t=A[i]; wh原创 2016-04-06 16:05:07 · 194 阅读 · 0 评论 -
各种排序算法
#include <iostream>using namespace std;int A[8]={1,7,8,3,5,6,7,4};int n=8;void insertsort() //插入排序{ for(int i=1;i<n;i++) { int t=A[i]; int j=i-1; while(j>=0&&A原创 2016-04-06 15:30:41 · 201 阅读 · 0 评论 -
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 examp原创 2016-05-18 20:57:45 · 640 阅读 · 0 评论