Code Planet

为了发现更大的世界


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

笔试练习(二)

发表于 2015-10-13 | 分类于 cpp

重建二叉树

  • 根据先序遍历和中序遍历重建二叉树
  • 问题1:vector分片
  • 问题2:相似语句复制粘贴容易出问题而且不易察觉(特别是仅有下标不同的情况)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    struct TreeNode
    {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };

    struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in)
    {
    if(pre.empty() || in.empty()) return NULL;

    TreeNode* root = new TreeNode(pre[0]);

    int i;
    for(i=0; i<pre.size();i++)
    if(pre[0] == in[i]) break;

    vector<int> pre1(pre.begin()+1, pre.begin()+i+1);
    vector<int> pre2(pre.begin()+i+1, pre.end());
    vector<int> in1(in.begin(), in.begin()+i);
    vector<int> in2(in.begin()+i+1, in.end());

    //vector<int>::iterator it;
    //for(it = pre1.begin();it!=pre1.end();it++)
    // cout << *it << "\t";
    //cout << endl;

    //for(it = pre2.begin();it!=pre2.end();it++)
    // cout << *it << "\t";
    //cout << endl;

    //for(it = in1.begin();it!=in1.end();it++)
    // cout << *it << "\t";
    //cout << endl;

    //for(it = in2.begin();it!=in2.end();it++)
    // cout << *it << "\t";
    //cout << endl;

    root->left = reConstructBinaryTree(pre1,in1);
    root->right = reConstructBinaryTree(pre2,in2);

    return root;
    }
阅读全文 »

笔试练习(一)

发表于 2015-10-13 | 分类于 cpp

1.二维数组中的查找

  • 思路:根据矩阵特点,左下角和右上角对应的值对应两个方向上递增或者递减
  • 问题1:编码中ncol中没有添加下标
  • 问题2:比较时i和j搞混了
  • 问题3:二维数组初始化问题

ac代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
bool Find(vector<vector<int> > array,int target)
{
int nrow = array.size();
int ncol = array[0].size();

int i = ncol-1;
int j = 0;

for(i,j; i>=0 && j<=(nrow-1);)
{
if(array[j][i] == target) return true;
else if(array[j][i] > target) i--;
else if(array[j][i] < target) j++;
}
return false;
}
};

阅读全文 »

关键词提取算法

发表于 2015-08-26 | 分类于 NLP

TFIDF

想要提取文章中最的关键词,最容易想到的就是统计词频。通常在文章中出现最多的词就是文章最中心的词,但我们如果统计一下,会发现其实出现最多的是“的”、“是”等停用词。所以我们需要的关键词应该是在文章中出现多,但是在平常语境下出现少的词(转换思路就是需要让停用词的系数小于文章关键词的系数)。这就引出了TFIDF(文档逆频率)

TextRank

wordpress博客迁移入hexo方法

发表于 2015-08-15 | 分类于 blog

导出文章

登录wordpress管理控制台,选择工具->导出xml文件

安装插件

安装WordPress migrator插件

1
npm install hexo-migrator-wordpress --save

hexo生成

运行hexo命令,生成post文章

1
hexo migrate wordpress source

have fun

hadoop环境配置

发表于 2015-08-04 | 更新于 2015-08-06 | 分类于 Big Data

Hadoop启动模式

Hadoop集群有三种启动模式:

  • 单机模式:默认情况下运行为一个单独机器上的独立Java进程,主要用于调试环境
  • 伪分布模式:在单个机器上模拟成分布式多节点环境,每一个Hadoop守护进程都作为一个独立的Java进程运行
  • 完全分布式模式:真实的生产环境,搭建在完全分布式的集群环境

用户及用户组

需要先添加用来运行Hadoop进程的用户组hadoop及用户hadoop。

  1. 添加用户及用户组,创建用户hadoop

    1
    $ sudo adduser hadoop
  2. 添加sudo权限,将hadoop用户添加进sudo用户组

    1
    $ sudo usermod -G sudo hadoop
阅读全文 »

OpenCv特征点算法

发表于 2015-07-13 | 分类于 CV

OpenCV 中和 2D 特征检测相关的算法的头文件位于
opencv\modules\features2d\include\opencv2\features2d.hpp,除SIFT、SURF以外的特征。
opencv\modules\nonfree\include\opencv2\nonfree\features2d.hpp,只包含SIFT、SURF这两个受专利保护的特征,因此不是免费的(nonfree)。

我们来看具体的类
DescriptorExtractor的子类都是描述子提取器,包含

  • FREAK
  • OpponentColorDescriptorExtractor
  • BriefDescriptorExtractor

FeatureDetector的子类都是特征检测器,包含

  • MSER
  • StarDetector,又名 StarFeatureDetector
  • FastFeatureDetector
  • GFTTDetector,又名 GoodFeaturesToTrackDetector
  • SimpleBlobDetector
  • DenseFeatureDetector

FeatureDetector还提供一系列特殊的适配器子类,用于增强或加速之前的 FeatureDetector 类

  • GridAdaptedFeatureDetector
  • PyramidAdaptedFeatureDetector
  • AdjusterAdapter
  • DynamicAdaptedFeatureDetector
  • FastAdjuster
  • StarAdjuster
  • SurfAdjuster

Feature2D的子类既是 FeatureDetector,又是 DescriptorExtractor,包含

  • BRISK
  • ORB
  • SIFT
  • SURF

来源:
【1】http://zhi.hu/oG6v

bitset实现无重复随机数生成

发表于 2015-07-10 | 分类于 C++

简单的随机数生成采用rand()函数就可以得到:

1
int rv= rand() % UpperBound;

这样得到的随机数,每次运行都一样,我们可以加一个srand(),使每次运行产生的数都不一样

1
srand((int)time(0));

但是这样避免不了产生相同的数值,下面是我运行两次的结果:

1
2
1 7 4 0 9 4 8 8 2 4
5 5 1 7 1 1 5 2 7 6

阅读全文 »

Scrapy运行Import Error解决方法

发表于 2015-07-07 | 分类于 Python

Scrapy安装

所有依赖使用easy_install安装

1
2
3
4
5
6
easy_install Twisted
easy_install Zope.Interface
easy_install w3lib
easy_install pyOpenSSL
easy_install libxml2
easy_install Scrapy

如果libxml2没有安装成功可以使用下面的地址进行安装
下载地址:http://users.skynet.be/sbi/libxml-python/binaries/libxml2-python-2.7.7.win32-py2.7.exe

scrapy Import Error

解决的办法是把下面三个文件复制到C:\python27\Lib\site-packages\win32\目录下

  • pythoncom27.dll
  • pythoncomloader27.dll
  • pywintype.dll

运行tutorial

详见参考3

参考资料

【1】http://www.cnblogs.com/txw1958/archive/2012/07/12/scrapy_installation_introduce.html
【2】http://stackoverflow.com/questions/23529348/scrapy-import-error-scrapy-core-downloader-handlers-s3-s3downloadhandler
【3】http://scrapy-chs.readthedocs.org/zh_CN/latest/intro/tutorial.html

值得学习的Cpp开源项目

发表于 2015-07-02 | 分类于 C++

值得学习的C语言开源项目

1.Webbench

Webbench是一个在linux下使用的非常简单的网站压测工具。它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测试网站的负载能力。Webbench使用C语言编写, 代码实在太简洁,源码加起来不到600行。

下载链接:https://github.com/LippiOuYang/WebBench

2.Tinyhttpd

tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通过阅读这段代码理解一个 Http Server 的本质。

下载链接:https://github.com/LippiOuYang/Tinyhttpd

3.cJSON

cJSON是C语言中的一个JSON编解码器,非常轻量级,C文件只有500多行,速度也非常理想。

cJSON也存在几个弱点,虽然功能不是非常强大,但cJSON的小身板和速度是最值得赞赏的。其代码被非常好地维护着,结构也简单易懂,可以作为一个非常好的C语言项目进行学习。

项目主页:http://sourceforge.net/projects/cjson/

阅读全文 »

Git配置和常用命令

发表于 2015-06-30 | 分类于 Tool

转自: http://hunng.com/2014/04/25/git-configs-and-cammands/

Git是一个分布式版本控制/软件配置管理软件,原来是linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为了更好地管理linux内核开发而创立的。

Git配置

1
2
3
4
5
6
7
8
git config --global user.name "hunng"
git config --global user.email "huangthink@gmail.com"
git config --global color.ui true
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config -l # 列举所有配置

用户的git配置文件在~/.gitconfig,我的配置:

阅读全文 »

1…345…12
Lu Xiaohua

Lu Xiaohua

116 日志
33 分类
86 标签
GitHub E-Mail
© 2019 Lu Xiaohua
由 Hexo 强力驱动 v3.8.0
|
主题 – NexT.Muse v7.0.0