今天介绍几种常见的算法,在面试中或许能派上用场

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的更多相关文章

  1. 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 ...

  2. Mybatis 控制台打出Sql-Log的设置

    首先工程中必须要有slf4j-log4j12-1.7.12.jar这个包,否则打不出来 而后工程中“log4j.properties”文件如下: log4j.appender.stdout=org.a ...

  3. Adaboost总结

    一.简介 Boosting 是一类算法的总称,这类算法的特点是通过训练若干弱分类器,然后将弱分类器组合成强分类器进行分类.为什么要这样做呢?因为弱分类器训练起来很容易,将弱分类器集成起来,往往可以得到 ...

  4. Random Forest总结

    一.简介 RF = Bagging + Decision Tree 随机:数据采样随机,特征选择随机 森林:多个决策树并行放在一起 几个误区: 不是每棵树随机选择特征,而是每一个结点都随机选择固定数目 ...

  5. ibatis的缓存机制

    Cache        在特定硬件基础上(同时假设系统不存在设计上的缺漏和糟糕低效的SQL 语句)Cache往往是提升系统性能的最关键因素).        相对Hibernate 等封装较为严密的 ...

  6. 利用OsCache实现后端轮循

    轮循随处可见,最常用的是APP首页的一些促销活动,一两秒切换一张图片,让前端实现起来也不难.这里说下后端的轮循,实现原理是数组+缓存.将数组放入缓存,指定缓存失效时间,如果是在失效前从缓存中取数据,那 ...

  7. OScached页面缓存的入门使用

    OSCache的使用: 一,环境的搭建: 1,把oscache.jar file放在 /WEB-INF/lib 目录下(Put the oscache.jar file in the /WEB-INF ...

  8. oscache使用经历

    oscache作为一款老的本地缓存,应用场景主要有页面缓存和对象缓存.这里拿在maven项目中使用oscache作为对象缓存举例说明下用法: 1.导入jar包 <dependency> & ...

  9. oscache.properties文件配置

    1.cache.memory是否使用内存缓存:值为:true或false.默认为true:如设置为false,那cache只能缓存到数据库或硬盘中. 2.cache.capacity缓存的最大数量.默 ...

随机推荐

  1. Caffe Ubuntu14.04 64位 的最快安装 (cuda7.5 + cudnn7.0 2016最新)

    最近因为各种原因,装过不少次Caffe,安装过程很多坑,为节省新手的时间,特此总结整个安装流程. 关于Ubuntu 版本的选择,建议用14.04这个比较稳定的版本,但是千万不要用麒麟版!!!比原版体验 ...

  2. avalon - 初步接触

    avalon - 初步接触 avalon的介绍http://rubylouvre.github.io/mvvm/ 按照作者的介绍,在HTML中添加绑定,在JS中用avalon.define定义View ...

  3. Magnum Kuernetes源码分析(一)

    Magnum版本说明 本文以magnum的mitaka版本代码为基础进行分析. Magnum Kubernetes Magnum主要支持的概念有bay,baymodel,node,pod,rc,ser ...

  4. Python基础1-变量、运算符、表达式

    一.Python的安装 1.下载python安装包https://www.python.org/ 2.选择对应的Python版本(Windows下) 3.装完之后打开电脑的cmd,验证一下安装是否成功 ...

  5. C语言之分支结构 if(二)

    If的第三种和第四种形式(tips:也是比较常用的形式) 3).if语句第三种形式: 简单来说就是任意的if或者else里面还可以嵌套任意的if-else语句 语法: if(表达式){ if(表达式2 ...

  6. Mybatis原理图

    Mybatis原理图 MyBatis 是一个基于Java的持久层框架.它提供的持久层框架包括SQL Maps和Data Access Objects(DAO). MyBatis 是支持普通 SQL查询 ...

  7. javascript构造函数以及原型对象的理解

    以下是一个构造函数的例子 如果是实例方法,不同的实例化,它们引用的地址是不一样的,是唯一的. //定义一个构造函数 function People(name,age){ this.name=name; ...

  8. D3.js

    html代码: <div id="id"> <p>Apple</p> <p id="aa">Pear</p ...

  9. 同时操作两个数据库:报错Illegal attempt to associate a collection with two open sessions

    今天我在一个操作两个数据库的SSH里 同时插入1条数据 报错 Illegal attempt to associate a collection with two open sessions 在这里有 ...

  10. Salesforce自主学习(一)

    Salesforce学习--接触Apex: 学习目标: 1.描述出Apex程序语言的关键特点: 2.保存一个Apex类并用另一个Apex类来调用它的方法: 3.使用Developer Console检 ...