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

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. 探讨C++ 变量生命周期、栈分配方式、类内存布局、Debug和Release程序的区别

    探讨C++ 变量生命周期.栈分配方式.类内存布局.Debug和Release程序的区别(一) 今天看博客园的文章,发现博问栏目中有一个网友的问题挺有趣的,就点进去看了下,标题是“C++生存期问题”,给 ...

  2. iphone/ipad/iOS on Linux Debian7/ubuntu12.04/linuxmint13/ubuntu14.04 compiling from source

    The packages we need for ubuntu12.04 and its derived destros are: libimobiledevices, libplist, libus ...

  3. function返回值Python特殊语法:filter、map、reduce、lambda

    废话就不多说了,开始... Python内置了一些非常有趣但非常有用的函数,充分体现了Python的语言魅力! filter(function, sequence):对sequence中的item顺次 ...

  4. 使用php完成常见的"文件上传"功能

    文件上传现在都是很常见的了,可以上传文件,上传头像等,不同的浏览器"文件上传"有不同的效果 先看下火狐浏览器的效果是这样的: 再看下IE浏览器是这样的: 还有很多其他的浏览器,就不 ...

  5. 【C语言】字符串模块

    一.字符串简介 * 在Java中,一个字符串可以用String类型来存储 String s = "MJ"; C语言中没有String这种类型.其实字符串就是字符序列,由多个字符组成 ...

  6. css透明度(兼容所有浏览器)

    .transparent{ background: rgba(, , , 0.3) !important; /* IE无效,FF有效 */ filter: alpha(opacity=); -moz- ...

  7. Netty 5.0源码分析-ByteBuf

    1. 概念 Java NIO API自带的缓冲区类功能相当有限,没有经过优化,使用JDK的ByteBuffer操作更复杂.故而Netty的作者Trustin Lee为了实现高效率的网络传输,重新造轮子 ...

  8. CentOS 7安装nginx

    CentOS 7安装nginx 参考网上其他文章做的 安装Nginx 我们从nginx官方的RPM源来安装一个预构建的稳定版本的nginx包. rpm --import http://nginx.or ...

  9. 关于web多标签多条件筛选的思考以及缓存的正确使用方法(上)

    做项目的过程中,发现一次远程链接数据库的耗时大概是300ms~400ms,切身体会到了前辈们经常说的减少链接的重要性,用了缓存后页面的打开时间从1.5s减少到400ms 前提: 那么来说一说正题,we ...

  10. Listview右侧 IndexBar

    qq 好友聊天界面,右侧 IndexBar  A B C D ,点击跳转到相应的联系人名字 import android.content.Context; import android.graphic ...