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. node.js获取本机Ip, hostName, mac

    //获取ip地址 getIPAdress() { let interfaces = require('os').networkInterfaces(); for (var devName in int ...

  2. 解决:win10在空白处右键资源管理器重启的故障

    一,查看windows日志 win+R,输入eventvwr,打开事件查看器,展开左边Windows日志,双击圆形红底白色感叹号图标所在的行. 查找错误模块名称. 对比图标和名称,这是"飞鸽传书"的安 ...

  3. Python3 复制和深浅copy

    赋值: 列表的赋值: list1 = ['peter','sam'] list2 = list1 print(list1,id(list1)) print(list2,id(list2)) list1 ...

  4. Halcon示例:bottlet.hdev 光学字符识别(创建OCR)

    * * Training of the OCR* The font is used in "bottle.hdev"* * * Step 0: PreparationsFontNa ...

  5. SpringCloud-day02-服务消费者项目建立

    4.4microservice-ticket-consumer-80服务消费者项目建立 我们新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 microservice- ...

  6. mongodb文件损坏的恢复--无可恢复数据

    1.mongodb 启动异常error code 100,检查日志,数据文件损坏 2 检查collection-15-6548623434943640018.wt 可恢复数据,为空,不存在恢复的数据 ...

  7. 使用 Ansible 统计服务器资源利用率

    使用 Ansible 统计服务器资源利用率: 3 条 shell 脚本实现统计: CPU 利用率统计: top -bn1 | grep load | awk '{printf "CPU Lo ...

  8. Python+Selenium学习--控制浏览器控制条

    场景 有时候web 页面上的元素并非直接可见的,就算把浏览器最大化,我们依然需要拖动滚动条才能看到想要操作的元素,这个时候就要控制页面滚动条的拖动,但滚动条并非页面上的元素,可以借助JavaScrip ...

  9. [leetcode]22. Generate Parentheses生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  10. Python开发——数据类型【列表】

    列表的定义 中括号[]内以逗号分隔开,按照索引,存放各种数据类型,每个位置代表一个元素 list_t = ['张三','Lucy',123] print(list_t) # ['张三', 'Lucy' ...