一.Using c++ to write a queue. Need support function push(), pop()
答案:
#include <iostream>
#include <string>
using namespace std;
template <typename T>
struct Node{
T value;
Node<T> *next;
Node<T> () {next =NULL;}
};
class MyQueue{
private:
unsigned int num;
Node<T> * first;
Node<T> * last;
public:
MyQueue();
~MyQueue();
unsigned int size();
void push();
void pop();
bool empty();
T back();
T front();
};
template <typename T>
MyQueue<T>::MyQueue()
{
num =0 ;
first = NULL;
last = NULL;
}
template <typename T>
MyQueue<T>::~MyQueue()
{
while(!empty())
{
pop();
}
}
template <typename T>
unsigned int MyQueue<T>::size()
{
return num;
}
template <typename T>
void MyQueue<T>::push(T ele)
{
Node<T> * tmp = new Node<T>;
tmp->next = NULL;
tmp->value = ele;
if(0==this->num)
{
first = tmp;
last = tmp;
}
else
{
last ->next = tmp;
last = tmp;
}
(this->num)++;
}
template <typename T>
void MyQueue<T>::pop()
{
if(0==this->num)
{
cout <<"empty queue"<<endl;
}
else
{
Node<T> * tmp =first;
first = first->next;
delete tmp;
(this->num)--;
}
}
template <typename T>
void MyQueue<T>::back()
{
if(0==this->num)
{
cout <<"empty queue"<<endl;
return NULL;
}
else
{
return last->value;
}
}
template <typename T>
void MyQueue<T>::front()
{
if(0==this->num)
{
cout <<"empty queue"<<endl;
return NULL;
}
else
{
return first->value;
}
}
二. Given a non-empty binary tree, find the minimum path sum.
For this problem, a path is defined as any sequence of nodes
The path must contain at least one node and "does not need to go through the root".
Input: [-10,9,20,null,null,15,7]
-10
/ \
9 -20
/ \
-15 7
Output: -45
----------
/*
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
答案:
void minPathSum(TreeNode* root,int& left, int&right,int &minsum);
int minPathSum(TreeNode* root) {
int minsum = 0;
int left,right;
minPathSum(root, left, right, minsum);
return minsum;
}
void minPathSum(TreeNode* root,int& left, int&right,int &minsum)
{
if(root == NULL)
{
left = 0;
right = 0;
return;
}
int left1,right1,left2,right2;
minPathSum(root->left,left1,right1,minsum);//left subtree
minPathSum(root->right,left2,right2,minsum);//right subtree;
left = root->val;
right = root->val;
if(max(left1,right1)>=0 &&max(left2,right2)>=0)
{
//start a new path
minsum = min(root->val,minsum);
}
if(max(left1,right1)<0)
{
left = left + min(left1,right1);
}
if(max(left2,right2)<0)
{
right = right + min(left2,right2);
}
minsum = min(left+right+root->val,minsum);
}
/**
*部分边界处理错误了...
*/
-----------
三. Compare ORB feature to FAST
答案:
ORB = FAST + BRIEF
ORB算法提取特征是在FAST算法上进行改进的.
在用FAST提取出特征点后,再加一个特征点方向,来实现了算法特征点的旋转不变性。
FAST本质是一种检测特征点的算法,主要检测局部灰度变化明显的地方,
其主要思想是:如果某个像素点与邻域像素差别较大,那么该点更有可能是特征点。
检测基本流程如下:
1. 在图像中选取某个像素点p,假设它的亮度为`lp`;
2.设定某个像素亮度的阈值T,例如`0.3*lp`;
3.以像素p为中心点,遍历其周围半径为3的圆上的16个像素点的亮度值;
4.如果检测出来该圆连续N个像素点亮度大于`lp+T`,或者连续N个像素点亮度小于`lp-T`,则像素点p可认为是特征点。
5.循环以上步骤,对每个像素执行相同操作。
FAST仅比较像素间的亮度差异,并且不具备方向和尺度信息,在实际视觉应用中存在缺陷。
鉴于此,ORB增加了尺度和方向的描述。
ORB算法使用矩来确定FAST特征点的方向,也就是:
通过矩来计算特征点以半径R范围内的质心,特征点坐标到质心形成一个向量来作为该特征点的方向。
BRIEF是一个二进制描述子,该算法计算粗一个二进制的特征描述符。它的具体思想如下:
1.在一个特征点的邻域内,选择n对像素点`p(i),q(i)(i=1,2...,n)`;
2.比较每个点对的灰度值大小l,如果`l[p(i)]>l[q(i)]`,生成二进制串中的1,反之生成0;
3.循环以上步骤,对每个像素对执行相同操作。
生成的n一般取128,256或512.
ORB算法对原始BRIEF进行了改进,使用了高斯函数进行平滑,并加入了抗噪,考虑到了旋转和缩放。
在加入抗噪后ORB在旋转和缩放中仍有较好表现。
同时FAST和BRIEF组合也非常高效,使得ORB特征在视觉SLAM很受欢迎。
题目1:用二分法求解对于输入的浮点数x,计算√x的值。
#DEFINE PREC 0.0001
float func1(float x)
{
if(x<=0)
{
return 0;
}
if(x=1)
{
return 1;
}
float begin = 0;
float end = x;
float middle =x/2;
while(middle*middle>x+PREC ||middle*middle<x-PREC)
{
middle = (begin+end)/2;
if(middle*middle<x+PREC )
{
begin = middle;//如果偏小,升高下边界
}
if(middle*middle>x-PREC )
{
end = middle;//如果偏大,降低上边界
}
}
return middle;
}
题目2:一个环上有N个人,编号为0,1,2,3…N-1。从0开始报数,当报到K的人出局,剩下的人接着报。直到所有人出局,对于给定的N和K,输出依次出局的编号。
//这道题可以用递归,我先定义int函数,然后print
int getnum(int N, int K)
{
if(N ==1)
{
return 1;
}
int out = (N+K-1)%N-1;
return (getnum(N-1,K)+K-1)%N+1;
}
int answer(int N, int K)
{
List<int> lst;
for(int i = 0; i<N;i++)
{
lst.push_back(i);
}
List<int>::iterator iter = lst.begin();
while(lst.size()>1)
{
for(int i = 0 ; i<K-1; i++)
{
if(++iter==lst.end())
{
iter = lst.begin();
}
}
List<int>::iterator iterdel = iter ;
if(++iter==lst.end())
{
iter = lst.begin();
}
lst.erase(iterdel);
}
return *iter;
}
void func2(int N, int K)
{
if(N ==1)
{
cout<<"1"<<endl;
}
for(int i = 1; i<N ; i++)
{
cout << getnum(N,K)<<endl;
}
//cout << getnum(N,K)<<endl; //这只有一个输出吧
}
题目3:有三个正整数,判断它们之间能否通过加减乘除得到0,可以输出YES,否则输出NO。
//四则运算判断可否得到0,枚举??可能要用递归,不过没思路了…
void func3(int a, int b, int c)
{
if(a==b||a==c||b==c)
{
cout<<"YES"<<endl;
}
if((a=b+c)||(b=a+c)||(c=a+b))
{
cout<<"YES"<<endl;
}
if((a=b-c)||(b=a-c)||(c=a-b))
{
cout<<"YES"<<endl;
}
if((a=b*c)||(b=a*c)||(c=a*b))
{
cout<<"YES"<<endl;
}
if((a=b/c)||(b=a/c)||(c=a/b))
{
cout<<"YES"<<endl;
}
}