Interview Common Sample Codes
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的更多相关文章
- Sample Codes之Query features from a FeatureLayer
除了地图基本的放大缩小等功能,在webgis上的二次开发中,查询功能 通常作为需求的一部分需要我们去实现,今天就给大家详细的分析实例代码中的查询功能:Query features from a Fea ...
- iOS苹果官方Demo合集
Mirror of Apple’s iOS samples This repository mirrors Apple’s iOS samples. Name Topic Framework Desc ...
- Demystifying iOS Application Crash Logs
http://www.raywenderlich.com/23704/demystifying-ios-application-crash-logs This is a blog post by So ...
- OGRE启动过程详解(OGRE HelloWorld程序原理解析)
本文介绍 OGRE 3D 1.9 程序的启动过程,即从程序启动到3D图形呈现,背后有哪些OGRE相关的代码被执行.会涉及的OGRE类包括: Root RenderSystem RenderWindow ...
- Ogre 1.9 Android移植
Ogre 1.9 Android移植 分类: 图形渲染2013-02-04 16:47 3860人阅读 评论(14) 收藏 举报 Android Ogre C++linuxLinuxLINUX 上一篇 ...
- Book Review: PowerShell 3.0 Advanced Administration Handbook
Recently I read a book, PowerShell 3.0 Advanced Administration Handbook, which I found really worthy ...
- (C/C++) 算法,编程题
注: 如下的题目皆来自互联网,答案是结合了自己的习惯稍作了修改. 1. 求一个数的二进制中的1的个数. int func(int x) { ; while (x) { count++; x = x&a ...
- Citect:How do I translate Citect error messages?
http://www.opcsupport.com/link/portal/4164/4590/ArticleFolder/51/Citect To decode the error messag ...
- FBX SDK 从2012.1 到 2013.3 变化
==================================================== ============================== 译文 ...
随机推荐
- c#中使用excel
在做一个小项目,需要把一些查询结果导出到Excel,找了一些资料,自己也总结出了一点方法,与大家共享. 一.首先简要描述一下如何操作Excel表 先要添加对Excel的引用.选择项目-〉添加引用-〉C ...
- Django之路由系统 Dj
Django之路由系统 Django的路由系统 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调 ...
- R语言-增加图例
legend()函数 > plot(rain$Tokyo,type="l",col="red", + ylim=c(0,300), + main=&quo ...
- Java的学习05
今天学习了,Java中的LinkedList类.这个类需要用到链表的知识,以前一直以为,只有c/c++有链表.今天才知道,原来其他语言.也有链表,而且还是双向链表. /** * 自定义一个链表 * @ ...
- css之标签选择器
标签(空格分隔): 标签选择器 选择器定义: 在一个HTML页面中会有很多很多的元素,不同的元素可能会有不同的样式,某些元素又需要设置相同的样式,选择器就是用来从HTML页面中查找特定元素的,找到元素 ...
- easyui多图片上传+预览切换+支持IE8
引入css和js: <link href="${pageContext.request.contextPath}/plugin/dialog/dialog.css" rel= ...
- 解决yum安装ftp提示仓库 的 GPG 密钥已安装,但是不适用于此软件包。
遇到的问题: 在linux系统使用yum install ftp安装ftp,报以下错误 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature ...
- 考研结束,重返python
因为考研的原因,python的学习告一段落,现在考验终于结束了,也抓眼又到了新的一年.新的一年里也要继续加油啊.python学习之路还要继续下去,但是毕竟有将近半年没有鹏编程了,首先我还是需要好好的复 ...
- [剑指Offer]38-字符串的全排列
链接 https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=13&tqId=11180&tPa ...
- 776. Split BST 按大小拆分二叉树
[抄题]: Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree int ...