2组:

[抄题]:

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

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

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

[思维问题]:

想不到两个小人的partition

[一句话思路]:

两个小人的partition,不用排序

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 直接调换就行了,不用写swap函数
  2. 要判断left < right,防止left加过头了
  3. corner case别忘了

[总结]:

  1. 一直到left = right时都要一直处理,所以判断条件是left <= right。

[复杂度]:

Time complexity: 平均情况下,O(nlgn)每个数都放在一半中, Space complexity: O(lgn)放在一半中

[英文数据结构,为什么不用别的数据结构]:

降低复杂度

[其他解法]:

排序

[Follow Up]:

[题目变变变]:

/**
* 本参考程序来自九章算法,由 @九章算法 提供。版权所有,转发请注明出处。
* - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
* - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,Android 项目实战班,
* - Big Data 项目实战班,算法面试高频题班, 动态规划专题班
* - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code
*/ 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) {
if(nums == null || nums.length == 0){
return 0;
} int left = 0, right = nums.length - 1;
while (left <= right) { while (left <= right && nums[left] < k) {
left++;
} System.out.println("left++之后 = " + left); while (left <= right && nums[right] >= k) {
right--;
}
System.out.println("right--之后 = " + right); //左边的数字太大,右边的数字太小的时候就换一下
if (left <= right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp; // left++;
// right--;
System.out.println("left又变了 = " + left);
System.out.println("right又变了 = " + right);
}
}
return left;
}
}

3组:

[抄题]:

Given an array with n objects colored red, white, or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.You are not suppose to use the library's sort function for this problem.

[思维问题]:

[一句话思路]:

用partition,指定k,不用返回。结果就是小于k的数在左边,大于等于的在右边。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. nums[left] < k 时自加,因为前面有元素。会导致后面直接越出边界条件

[二刷]:

  1. while移动的时候也要在if里保证没有超出边界
  2. 比较是分为两边,左右都是和边界比较

[总结]:

不是左小右大就要交换。

交换后的加减只是促进往前走罢了,防止TLE。

第一次交换之后要reset右边界。

一些个等号实在是太恶心了,就算了吧

[复杂度]:

Time complexity: 平均情况下,O(nlgn)每个数都放在一半中, Space complexity: O(lgn)放在一半中

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

遍历+交换

[Follow Up]:

[题目变变变]:

class Solution {
public void sortColors(int[] nums) { int left = 0;
int right = nums.length - 1;
while(left <= right) {
//符合条件的
while(left < right && nums[left] < 1) {
left++;
}
while(left <= right && nums[right] >= 1) {
right--;
}
System.out.println("left = " + left);
System.out.println("right = " + right); //还是左边比右边小,说明符合条件的卡住了,没有完全进行。此时就需要交换
if (left <= right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp; left++;
right--;
}
} right = nums.length - 1;
while(left <= right) {
while(left <= right && nums[left] < 2) {
left++;
}
while(left <= right && nums[right] >= 2) {
right--;
}
if (left <= right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp; left++;
right--;
}
}
}
}
k组

[抄题]:

[思维问题]:

  1. 还是要用指针l,r ,指向开头和结尾
  2. k不知道取什么,就取中间的colormid
  3. 边界条件返回;

[一句话思路]:

把index和color分开排序,看作是最高版本

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. rainbowSort自定义函数中也有特殊情况:左指针比较小,左边颜色数量和右边一样
  2. colors[l] <= colorMid时要自己增加,因为有数值colorMid
  3. 第二次调用的颜色开头是mid + 1
  4. colors_mid是1-k之间的数值啊拜托

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构,为什么不用别的数据结构]:

就用array,可以partition

[其他解法]:

双指针 nk

[Follow Up]:

[题目变变变]:

public class Solution {
/*
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] colors, int k) {
// write your code here
if (colors == null || colors.length == 0) {
return;
} rainbowSort(colors, 0, colors.length - 1, 1, k);
} private void rainbowSort(int[] colors, int left, int right, int colors_left, int colors_right) {
int l = left;
int r = right;
int colors_mid = (colors_left + colors_right) / 2; if (left >= right) {
return ;
}
if (colors_left == colors_right) {
return ;
} while(l <= r) {
while(l <= r && colors[l] <= colors_mid) {
l++;
}
while(l <= r && colors[r] > colors_mid) {
r--;
}
if (l <= r) {
int temp = colors[r];
colors[r] = colors[l];
colors[l] = temp; l++;
r--;
}
rainbowSort(colors,left,r,colors_left,colors_mid);
rainbowSort(colors,l,right,colors_mid + 1,colors_right);
}
}
}
大小写排序:
用Character.isLowerCase 

基于快速排序的数组划分:2组 3组 K组(sort color)大小写排序 · Partition Array的更多相关文章

  1. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...

  2. 基于快速排序思想partition查找第K大的数或者第K小的数。

    快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...

  3. (转)基于快速排序的TOPK算法

    基于快速排序的TOPK算法 转自:http://blog.csdn.net/fanzitao/article/details/7617223 思想: 类似于快速排序,首先选择一个划分元,如果这个划分元 ...

  4. "《算法导论》之‘线性表’":基于动态分配的数组的顺序表

    我们利用静态分配的数组来实现的顺序表的局限还是挺大的,主要在于它的容量是预先定好的,用户不能根据自己的需要来改变.如果为了后续用户能够自己调整顺序表的大小,动态地分配数组空间还是很有必要的.基于动态分 ...

  5. 将数组划分成连续子序列 Split Array into Consecutive Subsequences

    2018-08-04 20:47:43 问题描述: 问题描述: 本题需要的是将一个数组划分成子序列,保证每个子序列是连续的,并且长度要大于等于3. 解题思路是使用贪心算法,首先对数组中的数字进行计数, ...

  6. 【算法30】从数组中选择k组长度为m的子数组,要求其和最小

    原题链接:codeforce 267 Div2 C 问题描述: 给定长度为n的数组a[],从中选择k个长度为m的子数组,要求和最大. 形式描述为:选择$k$个子数组[$l_1$, $r_1$], [$ ...

  7. CodeForces - 1175D Array Splitting(数组划分+后缀和+贪心)

    You are given an array a1,a2,…,ana1,a2,…,an and an integer kk. You are asked to divide this array in ...

  8. 坐标轴刻度取值算法-基于魔数数组-源于echarts的y轴刻度计算需求

    本文链接:https://blog.csdn.net/qq_26909801/article/details/96966372数值型坐标轴刻度计算算法前言算法描述上代码代码运行效果结语前言因实习的公司 ...

  9. 三 基于Java动态数组手写队列

    手写队列: package dataStucture2.stackandqueue; import com.lt.datastructure.MaxHeap.Queue; import dataStu ...

随机推荐

  1. PHP 序列化变量的 4 种方法

    摘自: PHP 序列化变量的 4 种方法 http://www.iteye.com/news/25668

  2. 不重启修改'log_slave_updates'变量

    Variable 'log_slave_updates' is a read only variable 不重启修改mysql变量 执行复制的时候遇到的问题 mysql> show variab ...

  3. 理解 tornado.gen

    转自:http://blog.xiaogaozi.org/2012/09/21/understanding-tornado-dot-gen/ 理解 tornado.gen SEP 21ST, 2012 ...

  4. Oauth2.0(六):Resource Owner Password Credentials 授权和 Client Credentials 授权

    这两种简称 Password 方式和 Client 方式吧,都只适用于应用是受信任的场景.一个典型的例子是同一个企业内部的不同产品要使用本企业的 Oauth2.0 体系.在有些情况下,产品希望能够定制 ...

  5. url的命名与反向解析

    url命名和反向解析  1. 命名   # url(r'^press_list/$', views.press_list,name='press_list'),     url(r'^pre/$', ...

  6. a标签解析url

    var url = 'http://127.0.0.1:8080/index.jsp?username=admin#name'; var aLink = document.createElement( ...

  7. python应用之爬虫实战1 爬虫基本原理

    知识内容: 1.爬虫是什么 2.爬虫的基本流程 3.request和response 4.python爬虫工具 参考:http://www.cnblogs.com/linhaifeng/article ...

  8. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165233

    Exp0 Kali安装 安装过程 1.首先我的Mac上已经安装好了VMware Fusion,所以直接下载对应的虚拟机版本的Kali即可. 2.进入Kali官网进行下载. 以下为下载链接: Kali ...

  9. UVA196

    #include<stdio.h> #include<iostream> #include <strstream> using namespace std; #de ...

  10. HAproxy使用

    参考官网 安装HAproxy/ pull 官方镜像 本地安装:本地安装路径:/usr/local/haproxy/配置: 添加:/usr/local/haproxy/conf/haproxy.cfg添 ...