Interview Common Sample Codes
1. Quick Sort:
int partition(int A[], int p, int r)
{int x = A[r]; // Pivot elementint i = p - 1; // Index of last element that not larger than pivot elementfor(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 rotatedif(A[mid] >= A[s]){if(target >= A[s])return comp(A, s, mid-1, target);elsereturn comp(A, mid+1, e, target); }else{return comp(A, s, mid-1, target);}}else{// if first part is not rotatedif(A[mid] >= A[s]){return comp(A, mid+1, e, target);}else{if(target <= A[e])return comp(A, mid+1, e, target);elsereturn 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;}
};
转载于:https://www.cnblogs.com/kid551/p/4307322.html
Interview Common Sample Codes相关推荐
- Sample Codes之Query features from a FeatureLayer
除了地图基本的放大缩小等功能,在webgis上的二次开发中,查询功能 通常作为需求的一部分需要我们去实现,今天就给大家详细的分析实例代码中的查询功能:Query features from a Fea ...
- Interview QA-COBOL COBOL II
Q1) Name the divisions in a COBOL program ?. A1) IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA ...
- Understanding Docker Container Exit Codes
The most common exit codes, what they mean, and what causes them Sandeep Madamanchi Follow Oct 21, 2 ...
- Oracle Business Intelligence Foundation 12c sample app虚拟机下载和使用
https://blogs.perficient.com/2016/09/02/how-to-install-and-configure-obiee-12c-sample-app-607-part-1 ...
- Mastering Android NDK Build System - Part 1: Techniques with ndk-build
This article is not a "Hello world!"-type tutorial for NDK. Although I will still provide ...
- ios pusher使用_如何使用JavaScript和Pusher实时更新用户状态
ios pusher使用 by Rahat Khanna 通过拉哈特·汉娜 如何使用JavaScript和Pusher实时更新用户状态 (How to update a User's Status i ...
- 如何优化Jupyter Notebook
介绍 (Introduction) Jupyter Notebook is nowadays probably the most used environment for solving Machin ...
- nginx documentation | Development guide
Table of Contents nginx documentation Development guide Nginx distributions ▪️ OpenResty ▪️ The Te ...
- iOS 苹果官方Demo
GitHub 文章链接地址, 欢迎Star+Fork Mirror of Apple's iOS samples This repository mirrors Apple's iOS samples ...
最新文章
- 这边有本书:中国社会生存的必备常识(修订版)下载
- 计算机科学与技术 单片机,单片机-兰州交通大学计算机科学与技术实验教学中心...
- 【一分钟了解】1em等于多少px,由什么决定?
- 攻防世界-web-bug-从0到1的解题历程writeup
- 水仙花python代码_「每日一练」巧用python打印出所有的水仙花数
- Rails 应用使用 Capistrano2 部署流程
- java 性能 优化_Java十大简单性能优化
- 12.混淆——数字音频采样,混淆,反混淆,脉冲序列_2
- MyBatis基础:MyBatis数据基本操作(2)
- scrapy Request
- mfc遍历指定文件夹下的所有文件并排序
- Linux中grep命令详解
- 神经网络模糊pid控制算法,模糊神经网络应用实例
- matlab编译桁架有限元计算(附有完整代码)
- foxmail删除服务器文件,无法真正删除?Foxmail文件中转站BUG
- 在哪里能找到各行业的分析研究报告?
- leetcode 之 Merge k Sorted Lists
- 社会统计分析—基础知识
- 农产品商铺商城小程序(JavaSSM+微信小程序)
- Chrome安装zotero connector 插件
热门文章
- [深度学习]BatchNormalization、LayerNormalization、InstanceNorm、GroupNorm、SwitchableNorm个人总结
- PCA和线性回归之间的关系如何?
- 去哪儿-14-vuex_localstorage
- mysql field flags_PHPmysql_field_flags()实例教程_PHP教程
- Java类初始化及实例化过程
- 【源码分析】微信小程序 - 01表单组件 - 01button - 实践
- 93没有了_93平旧两居大变身,二手房这样装修10万块搞定
- python赋值浅拷贝和深拷贝的区别_浅拷贝、深拷贝和普通赋值操作有什么区别?...
- c# npoi 2.5版本设置字体加粗_Python帮你做Excel——格式设置与画图
- windows文件读取 xxe_java xxe漏洞