题目

数组划分

给出一个整数数组nums和一个整数k。划分数组(即移动数组nums中的元素),使得:

  • 所有小于k的元素移到左边
  • 所有大于等于k的元素移到右边

返回数组划分的位置,即数组中第一个位置i,满足nums[i]大于等于k。

您在真实的面试中是否遇到过这个题?

Yes
样例

给出数组nums=[3,2,2,1]和 k=2,返回 1

注意

你应该真正的划分数组nums,而不仅仅只是计算比k小的整数数,如果数组nums中的所有元素都比k小,则返回nums.length。

挑战

要求在原地使用O(n)的时间复杂度来划分数组

解题

快速排序,搞了好久,中间的那个值一直找不到,然后就遍历数组找了。

public class Solution {
/**
*@param nums: The integer array you should partition
*@param k: As description
*return: The index after partition
*/
public int partitionArray(int[] nums, int k) {
//write your code here
if(nums.length==0)
return 0;
int p = partition(nums,0,nums.length-1,k);
if(p==nums.length - 1)
return nums.length;
return p; }
public int partition(int[] nums,int left,int right,int k){
int i = left;
int j = right;
while(i<j){
while(i<j&&nums[j] >=k) j--; while(i<j&& nums[i]<k) i++;
if(nums[i]>=k && nums[j]<k){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
j--;
i++;
}
}
for(i=0;i<right;i++)
if(nums[i]>=k)
return i;
return right;
}
}

Java Code

九章算法中,i<j 换成i<=j 最后输出的i就是所求的答案

public class Solution {
/**
*@param nums: The integer array you should partition
*@param k: As description
*return: The index after partition
*/
public int partitionArray(int[] nums, int k) {
//write your code here
if(nums.length==0)
return 0;
int p = partition(nums,0,nums.length-1,k);
return p; }
public int partition(int[] nums,int left,int right,int k){
int i = left;
int j = right;
while(i<=j){
while(i<=j&&nums[j] >=k) j--;
while(i<=j&& nums[i]<k) i++;
if(i<j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
j--;
i++;
}
}
return i;
}
}

Java Code

class Solution:
"""
@param nums: The integer array you should partition
@param k: As description
@return: The index after partition
"""
def partitionArray(self, nums, k):
# write your code here
# you should partition the nums by k
# and return the partition index as description
l = len(nums)
if l == 0:
return 0
i = 0
j = l-1
while i<j:
while i<=j and nums[j]>=k:
j-=1
while i<=j and nums[i]<k:
i+=1
if i<j:
tmp = nums[i]
nums[i]=nums[j]
nums[j]=tmp
i+=1
j-=1
return i

Python Code

lintcode 中等题:partition array 数组划分的更多相关文章

  1. lintcode 中等题:搜索旋转排序数组II

    题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...

  2. lintcode 中等题:和大于S的最小子数组

    题目 和大于S的最小子数组 给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 样例 给定数组 [2,3,1,2,4,3]  ...

  3. lintcode 中等题:kth-largest-element 第k大元素

    题目 第k大元素 在数组中找到第k大的元素 样例 给出数组[9,3,2,4,8],第三大的元素是4 给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推 注 ...

  4. lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

    题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题  法一:直接暴力求解 时 ...

  5. lintcode 中等题:find the missing number 寻找缺失的数

    题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 注意 可以改变序 ...

  6. lintcode 中等题:Single number III 落单的数III

    题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...

  7. lintcode 中等题:Singleton number II 落单的数 II

    题目 落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级 ...

  8. lintcode 中等题:majority number III主元素III

    题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...

  9. lintcode 中等题:k Sum ii k数和 II

    题目: k数和 II 给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字. 在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案. 样例 ...

随机推荐

  1. 自己手写简约实用的Jquery tabs插件(基于bootstrap环境)

    一直想改版网站首页的图书展示部分,以前的展示是使用BootStrap的传统的collapse,网页篇幅占用大,也不够美观,操作也相对来说比较麻烦.于是有了自己利用Jquery来做一个图书展示的tabs ...

  2. 转 XMLHttpRequest().readyState的五种状态详解

    转 http://javathinker.blog.ccidnet.com/blog-htm-itemid-1262479-do-showone-type-blog-uid-36384.html 在& ...

  3. js实现雪花飘落效果的代码

    使用js实现雪花飘落的效果,用html5绘布加js写的雪花飘效果 . 代码: <html> <head> <script> /** * js与html5实现的雪花飘 ...

  4. 过拟合(Overfitting)和正规化(Regularization)

    过拟合: Overfitting就是指Ein(在训练集上的错误率)变小,Eout(在整个数据集上的错误率)变大的过程 Underfitting是指Ein和Eout都变大的过程 从上边这个图中,虚线的左 ...

  5. 【nodejs】关于 alert 和 document

    Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Windows\system32>n ...

  6. .NET基础:C#静态构造函数、静态方法、静态属性

    用一个题目带大家走进静态函数,先看题目 class Program    {        public static int Count = 0;        static Program()   ...

  7. iTween基础之Audio(音量和音调的变化)

    一.基础介绍:二.基础属性 原文地址 : http://blog.csdn.net/dingkun520wy/article/details/50826033 一.基础介绍 AudioTo:改变声音的 ...

  8. mysql存储过程出现OUT or INOUT argument 10 for routine

    OUT or INOUT argument 10 for routine * is not a variable or NEW pseudo-variable 我查网上很多出现在call的时候没有添加 ...

  9. 增强学习(Reinforcement Learning and Control)

    增强学习(Reinforcement Learning and Control)  [pdf版本]增强学习.pdf 在之前的讨论中,我们总是给定一个样本x,然后给或者不给label y.之后对样本进行 ...

  10. PHP开发框架[国内框架]

    1.Thinkphp  http://thinkphp.cn/ 2.Brophp   http://www.brophp.com/zf/ 由LAMP兄弟连打造 3.WindFramework http ...