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 变化
==================================================== ============================== 译文 ...
随机推荐
- awk选取制定行数,条件判断等
awk '{if(NR%5==0){print}}' your_file 取出可以被5整除的数awk '{if(NR<=300){print}}' your_file 取出行数小于300的数据a ...
- PHP 调试打印输出变量
var_dump ($row); echo "hello"; echo "\n"; print_r ($arr); php 数组 对象 $arr = json_ ...
- 为datagrid、treegrid增加右键表头菜单,用于显示或隐藏列,注意:冻结列不在此菜单中
var createGridHeaderContextMenu = function(e, field) { e.preventDefault(); var grid = $(this);/* gri ...
- SQL Server中有关约束(constraint)的一些细节
本文出处:http://www.cnblogs.com/wy123/p/7350265.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...
- ArcPy开发教程1-面向ArcGIS的Python语言基础
ArcPy开发教程1-面向ArcGIS的Python语言基础 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 第一节课 时间2019年2月26日 上午第一节 讲解:A ...
- ArcGIS自定义工具箱-字段值部分替换
ArcGIS自定义工具箱-字段值部分替换 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:替换某个字段中的字符串 用例:湖南省长沙市=>湖南/长沙:临湘县 ...
- SVN中英文菜单对照
TortoiseSVN英文版菜单中文翻译01.SVN Checkout(SVN取出) 点击SVN Checkout,弹出检出提示框,在URL of repository输入框中输入服务器仓库地址,在C ...
- C++成员函数在内存中的存储方式
用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间.按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码分 ...
- 34 【kubernetes】安装手册
全文参考了两篇中文文档: 1,https://www.cnblogs.com/RainingNight/p/using-kubeadm-to-create-a-cluster.html 2,http: ...
- Python开发——解释器安装
Python(解释器)安装 Windows 1.Python(解释器)下载链接 2.选择好安装路径,点击安装即可 3.环境变量配置 [右键计算机]-->[属性]-->[高级系统设置]--& ...