1. Quick Sort:

int partition(int A[], int p, int r)
{
int x = A[r];  // Pivot element
int i = p - 1;  // Index of last element that not larger than pivot element
for(int j = p; j < r; ++j)
{
if(A[j] <= x)
{
++i;
exchange(A[i], A[j]);
}
} exchange(A[i+1], A[r]);
return i+1;
} void quickSort(int A[], int p, int r)
{
if(p >= r) return;
int q = partition(A, p, r);
quickSort(A, p, q - 1);
quickSort(A, q + 1, r);
}

命名良好的Java版本:

public class Solution {

	public static void exchange(int[] nums, int a, int b) {
if (a < 0 || b < 0 || a >= nums.length || b >= nums.length) {
return;
}
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
} public static int partition(int[] nums, int left_pos, int right_pos) { int sentinel = nums[right_pos];
int lst_less = left_pos - 1; for (int i = left_pos; i < right_pos; i++) {
if (nums[i] < sentinel) {
exchange(nums, ++lst_less, i);
}
}
exchange(nums, ++lst_less, right_pos); return lst_less;
} public static void quickSort(int[] nums, int left_pos, int right_pos) {
if (null == nums || nums.length < 2 ||
left_pos >= right_pos ||
left_pos < 0 || right_pos >= nums.length) {
return;
} int check_point = partition(nums, left_pos, right_pos);
quickSort(nums, left_pos, check_point - 1);
quickSort(nums, check_point + 1, right_pos);
} public static void main(String[] args) { int[] res = {41, 12, 55, 7, 12, 13, 57};
quickSort(res, 0, res.length - 1); for (int i : res) {
System.out.println(i);
} } }

  

2. Search in Rotated Array:

class Solution {
int comp(int A[], int s, int e, int target){
if(s > e) return -1;
if(s == e) return (A[s] == target ? s : -1); int mid = s + (e - s) / 2; if(A[mid] == target)
return mid;
else if(A[mid] > target){
// if first part is not rotated
if(A[mid] >= A[s]){
if(target >= A[s])
return comp(A, s, mid-1, target);
else
return comp(A, mid+1, e, target);
}else{
return comp(A, s, mid-1, target);
}
}else{
// if first part is not rotated
if(A[mid] >= A[s]){
return comp(A, mid+1, e, target);
}else{
if(target <= A[e])
return comp(A, mid+1, e, target);
else
return comp(A, s, mid-1, target);
}
}
} public:
int search(int A[], int n, int target) {
return comp(A, 0, n - 1, target);
}
};

3. Maximum Subarray:

class Solution {
public:
int maxSubArray(int A[], int n) {
int dp = A[0];
int end = dp; for(int i = 1; i < n; ++i){
end = end > 0 ? end + A[i] : A[i];
dp = dp > end ? dp : end;
} return dp;
}
};

Interview Common Sample Codes的更多相关文章

  1. Sample Codes之Query features from a FeatureLayer

    除了地图基本的放大缩小等功能,在webgis上的二次开发中,查询功能 通常作为需求的一部分需要我们去实现,今天就给大家详细的分析实例代码中的查询功能:Query features from a Fea ...

  2. iOS苹果官方Demo合集

    Mirror of Apple’s iOS samples This repository mirrors Apple’s iOS samples. Name Topic Framework Desc ...

  3. Demystifying iOS Application Crash Logs

    http://www.raywenderlich.com/23704/demystifying-ios-application-crash-logs This is a blog post by So ...

  4. OGRE启动过程详解(OGRE HelloWorld程序原理解析)

    本文介绍 OGRE 3D 1.9 程序的启动过程,即从程序启动到3D图形呈现,背后有哪些OGRE相关的代码被执行.会涉及的OGRE类包括: Root RenderSystem RenderWindow ...

  5. Ogre 1.9 Android移植

    Ogre 1.9 Android移植 分类: 图形渲染2013-02-04 16:47 3860人阅读 评论(14) 收藏 举报 Android Ogre C++linuxLinuxLINUX 上一篇 ...

  6. Book Review: PowerShell 3.0 Advanced Administration Handbook

    Recently I read a book, PowerShell 3.0 Advanced Administration Handbook, which I found really worthy ...

  7. (C/C++) 算法,编程题

    注: 如下的题目皆来自互联网,答案是结合了自己的习惯稍作了修改. 1. 求一个数的二进制中的1的个数. int func(int x) { ; while (x) { count++; x = x&a ...

  8. Citect:How do I translate Citect error messages?

    http://www.opcsupport.com/link/portal/4164/4590/ArticleFolder/51/Citect   To decode the error messag ...

  9. FBX SDK 从2012.1 到 2013.3 变化

    ==================================================== ============================== 译文               ...

随机推荐

  1. centos 7设置本地yum资源库

    前言 同样的,是在这两天安装ambari的时候遇到的问题之一,那就是关于centos的本地yum源的制作,当时是一种是制作iso镜像的yum源,还有一种将rpm软件包打成压缩包上传到centos的某一 ...

  2. ubuntu16下用QT5实现对话框应用

    ubuntu16下用QT5,实现对话框程序,步骤:生成界面Dialog.ui,将它应用到主程序,通过主程序显示. 一 界面练习 1 Dialog.ui界面生成 在命令行输入:designer 进入界面 ...

  3. firewalld 操作

    https://blog.csdn.net/s_p_j/article/details/80979450 firewall-cmd --permanent --add-rich-rule=" ...

  4. python装饰器补漏

    以前写过一篇装饰器文章,觉得少了点东西,今天特来补上,也就是带参数的装饰器,上篇文章写的不严谨 def logger(logs=""): def outer(f): def inn ...

  5. 30个php操作redis常用方法代码例子(转载)

    1.connect 描述:实例连接到一个Redis.参数:host: string,port: int返回值:BOOL 成功返回:TRUE;失败返回:FALSE示例: $redis = new red ...

  6. 安装SourceTree工具,无需注册就可以正常使用SourceTree

    1. 下载SourceTree安装包 2. 双击安装包进行安装,默认会直接安装在系统盘,此时桌面就会SourceTree的快捷键 3. 双击打开桌面的SourceTree,就会提示让你安装授权,那么接 ...

  7. autoperfixer 版本配置

    { "private": true, "dependencies": { "autoprefixer": "^9.3.1" ...

  8. python学习笔记(三)- 字典、集合

    字典:key-value形式 1)取数据方便   #字典里面没有重复的key 2)查询速度快 #字典是无序的 一.定义一个字典 infos = { 'name':'王小明', 'sex':'male' ...

  9. step_by_step_webapi执行时间

    做开发没多久,这次单位让我做对TB 的机票运价直连接口,其实主要是去sabre gds带上相应的参数去做查询,验仓,下单操作,这次用到asp.net boilerplate 项目模板搭建,用它的动态w ...

  10. pushf和popf

    pushf的功能是将标志寄存器的值压栈,而popf是从栈中弹出数据,送入标志寄存器中.