Base algorithm
今天介绍几种常见的算法,在面试中或许能派上用场
1.字符串倒转
//Reverse a string
public static string Reverse(string ori)
{
string MyOri = ori.Trim();
if(string.IsNullOrEmpty(MyOri))
{
Console.WriteLine("The string you input is null or empty");
return null;
}
try
{
StringBuilder sb = new StringBuilder(MyOri.Length);
if (MyOri.Contains(' '))
{
string[] Array = MyOri.Split(' '); for (int i = Array.Length - ; i >= ; i--)
{
sb.Append(Array[i]+' ');
}
}
else
{
for(int i=MyOri.Length-;i>=;i--)
{
sb.Append(MyOri[i]);
}
}
return sb.ToString();
}
catch (Exception ex)
{ throw new Exception(ex.ToString());
} }
2.获取数值中最大值
//Find the max value from int array
public static int FindMax(int[] A)
{
int max = A[];
for(int i=;i<=A.Length-;i++)
{
if(max<A[i])
{
max = A[i];
}
}
return max;
}
3.获取相同元素,从两个数组中.
//find common element from two int array
public static List<int> FindCommonElement(int[] A,int[] B)
{
int i = ;
int j = ;
List<int> a=new List<int> { };
while(i<A.Length&&j<B.Length)
{
if(A[i]<B[j])
{
i++;
}
else if(A[i]==B[j])
{
a.Add(A[i]);
i++;
j++; }
else if(A[i]>B[j])
{
j++;
}
}
return a.ToList();
}
4.移除两数组中相同元素
public static List<int> RemoveSameElement(List<int> A, List<int> B)
{
List<int> C1 = new List<int>();
List<int> C2 = new List<int>();
for (int i = 0; i <=A.Count()-1; i++)
{
for (int j = 0; j <=B.Count() - 1; j++)
{
if (A[i] == B[j])
{
C1.Add(A[i]);
} }
}
foreach(int item in C1)
{
A.Remove(item);
} C2 = A.Concat(B).ToList();
return C2;
}
5.找到出现次数最多的元素
//Find the element which appeard most time
public static int FindElement(int[] A)
{
//适用于重复元素连续一起的情况
//int currentValue = A[0];
//int counter = 1;
//for(int i=1;i<A.Length-1;i++)
//{
// if(currentValue==A[i])
// {
// counter++;
// }
// else
// {
// counter--;
// if(counter<0)
// {
// currentValue = A[i];
// }
// }
//}
//return currentValue; var res = from n in A
group n by n into g
orderby g.Count() descending
select g;
var gr = res.First();
return gr.First(); }
6.冒泡排序
//Bubble Sort
public static int[] BubbleSort(int[] A)
{
for(int i=;i<A.Length;i++)
{
for(int j=i+;j<A.Length;j++)
{
if(A[i]<A[j])
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
return A;
}
7.数组求和,一句话
public static int Sum(int[] a, int n)
{ return n == ? : Sum(a, n - ) + a[n - ]; }
Base algorithm的更多相关文章
- A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning
A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning by Jason Brownlee on S ...
- Mybatis 控制台打出Sql-Log的设置
首先工程中必须要有slf4j-log4j12-1.7.12.jar这个包,否则打不出来 而后工程中“log4j.properties”文件如下: log4j.appender.stdout=org.a ...
- Adaboost总结
一.简介 Boosting 是一类算法的总称,这类算法的特点是通过训练若干弱分类器,然后将弱分类器组合成强分类器进行分类.为什么要这样做呢?因为弱分类器训练起来很容易,将弱分类器集成起来,往往可以得到 ...
- Random Forest总结
一.简介 RF = Bagging + Decision Tree 随机:数据采样随机,特征选择随机 森林:多个决策树并行放在一起 几个误区: 不是每棵树随机选择特征,而是每一个结点都随机选择固定数目 ...
- ibatis的缓存机制
Cache 在特定硬件基础上(同时假设系统不存在设计上的缺漏和糟糕低效的SQL 语句)Cache往往是提升系统性能的最关键因素). 相对Hibernate 等封装较为严密的 ...
- 利用OsCache实现后端轮循
轮循随处可见,最常用的是APP首页的一些促销活动,一两秒切换一张图片,让前端实现起来也不难.这里说下后端的轮循,实现原理是数组+缓存.将数组放入缓存,指定缓存失效时间,如果是在失效前从缓存中取数据,那 ...
- OScached页面缓存的入门使用
OSCache的使用: 一,环境的搭建: 1,把oscache.jar file放在 /WEB-INF/lib 目录下(Put the oscache.jar file in the /WEB-INF ...
- oscache使用经历
oscache作为一款老的本地缓存,应用场景主要有页面缓存和对象缓存.这里拿在maven项目中使用oscache作为对象缓存举例说明下用法: 1.导入jar包 <dependency> & ...
- oscache.properties文件配置
1.cache.memory是否使用内存缓存:值为:true或false.默认为true:如设置为false,那cache只能缓存到数据库或硬盘中. 2.cache.capacity缓存的最大数量.默 ...
随机推荐
- vmstat命令查看系统资源占用情况
vmstat是一个十分有用的Linux系统监控工具,使用vmstat命令可以得到关于进程.内存.内存分页.堵塞IO.traps及CPU活动的信息.可用以下命令查看: # vmstat 2 直接查看系统 ...
- flask tutorial => make a blog :) flask 搭建博客系统从零开始!
please follow the tutorial from the official site :) http://flask.pocoo.org/docs/ You could download ...
- 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)
前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...
- 无线网卡连接internet,有线网卡向另一台电脑分享网络(笔记本当有线路由器)
有一台笔记本和一台台式机,都放在卧室内 笔记本能用无线网卡连接到客厅的路由器,台式机没有配备无线网卡,不能上网 要从客厅的路由器那边拉一条网线到卧室内连接台式机显然很蠢,在卧室内购置一台中继路由器也不 ...
- hdu1043
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#inclu ...
- shell 读取文件
#!/bin/bash content=`cat test.txt` echo "begin" for i in $content do echo $i done 读取前10行 t ...
- AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转
http://blog.csdn.net/zhangh8627/article/details/51752872 AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖 标签: ...
- CentOS 6.5安装之后的网络配置
CentOS 6.5安装之后的网络配置 1.查看IP地址,得到只有一个回环地址 127.0.0.1 2.进行网络测试,现在来测试下,看能不能ping通外网www.baidu.com 下面的是,关于pi ...
- js实现浏览器通知功能
概述 Notification API是浏览器的通知接口,用于在用户的桌面(而不是网页上)显示通知信息,桌面电脑和手机都适用,比如通知用户收到了一封Email.具体的实现形式由浏览器自行部署,对于手机 ...
- python 数据聚合与分组
前面讲完了字符处理,但对数据进行整体性的聚合运算以及分组操作也是数据分析的重要内容. 通过数据的聚合与分组,我们能更容易的发现隐藏在数据中的规律. 数据分组 数据的分组核心思想是:拆分-组织-合并 首 ...