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缓存的最大数量.默 ...
随机推荐
- Struts2.1.8 + Spring3.0+ Hibernate3.2整合笔记
body, p, th, td, li, ul, ol, h1, h2, h3, h4, h5, h6, pre { font-family: simsun; line-height: 1.4; } ...
- mmap学习
mmap学习 内存页: Linux是以页为单位来管理物理内存的,一页大小一般等于4096字节.页容量越大,系统中可能存在的内存碎片就越多. mmap将一个磁盘上的文件或者对象映射进内存.文件被映射到多 ...
- hdu 1698 Just a Hook(线段树基础)
成段更新的线段树,加入了延时标记............ 线段树这种东西细节上的理解因人而异,还是要自己深入理解......慢慢来 #include <iostream> #include ...
- Linux C编程一站式学习读书笔记——socket编程
前言 研一的时候写过socket网络编程,研二这一年已经在用php写api都快把之前的基础知识忘干净了,这里回顾一下,主要也是项目里用到了,最近博客好杂乱啊,不过确实是到了关键时刻,各种复习加巩固准备 ...
- Windows系统架构
操作系统模型 大多数操作系统中,都会把应用程序和内核代码分离运行在不同的模式下.内核模式访问系统数据和硬件,应用程序运行在没有特权的模式下(用户模式),只能使用有限的API,且不能直接访问硬件.当 ...
- 按自己的想法去理解事件和泛型(C#)
上一篇那些年困扰我们的委托(C#)讲了委托,这一篇自然就轮到事件了. 不喜欢官方的表达方式,喜欢按照自己的想法去理解一些抽象的东西. 事件 考虑到委托使用的一些缺陷,就有了事件.委托是不安全的,打个比 ...
- C++11多线程std::thread的简单使用
在cocos2dx 2.0时代,我们使用的是pthread库,是一套用户级线程库,被广泛地使用在跨平台应用上.但在cocos2dx 3.0中并未发现有pthread的支持文件,原来c++11中已经拥有 ...
- 微信企业号 JS-SDK:上传图片
微信的JS-SDK提供了微信客户端相关的功能,如:拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享.扫一扫等微信特有的能力,为微信用户提供更优质的网页体验.这里将会介绍如何通过调用JS ...
- “System.FormatException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理 其他信息: 该字符串未被识别为有效的 DateTime。
前台用过jquery ajax将值传递到 后台的一般处理程序 并且报了这个异常 原因是:前台传递过来的时间格式不对 需要把 "/"换成 "-" 例如:20 ...
- angularjs 路由回退,返回到上一个路由
在现阶段比较流行的angularjs框架中:路由是一个比较重要的应用:angularjs的单页面是其强大功能之一: 所有的页面其实就只是在一个页面就实现的:angularjs通过对路由的控制来进行页面 ...