Rearrange an array of positive and negative integers

2024-02-18 00:48

本文主要是介绍Rearrange an array of positive and negative integers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Given an array of positive and negative integers, re-arrange it 
so that you have postives on one end and negatives on the other, 
BUT retain the original order of appearance. 

For eg. 1, 7, -5, 9, -12, 15 => -5, -12, 1, 7, 9, 15


可以使用块交换技术实现题目要求,

1,7,-5 交换为 -5,1,7

然后

-5,1,7,9,-12 交换为 -5,-12,1,7,9


目测时间复杂度是o(n^2)

但是可以用递归将时间复杂度降维nlogn

http://haixiaoyang.wordpress.com/?s=Rearrange

有空可以研究下

//Given an array of positive and negative integers, 
//re-arrange it so that you have postives on one end 
//and negatives on the other, BUT retain the original order of appearance. do it in-place
//e.g. 1, 7, -5, 9, -12, 15 => -5, -12, 1, 7, 9, 15//When considering optimization from O(n^2) to O(nlogn),use divided & conquer
void swapRange(int a[], int nBeg, int nEnd)
{assert(a && nBeg <= nEnd);while (nBeg < nEnd)swap(a[nBeg++], a[nEnd--]);
}//solution:
//e.g. in any stage: ---- "+++ --" ++++, reverse middle part
// ==> ---- "--" "+++" ++++, reverse middle 2 parts seperatly to keep "stable"
void ReArrange(int a[], int n)
{assert(a && n > 0);if (n <= 1) return;ReArrange(a, n/2);ReArrange(a + n/2, n - n/2); //pitfall, notice both parametersint nLft = 0;while (a[nLft] < 0) nLft++;int nRgt = n-1;while (a[nRgt] >= 0)nRgt--;//Very important, no need to swap under this situation, returnif (nRgt <= nLft) return; swapRange(a, nLft, nRgt);int nBegRgt = nLft;while (a[nBegRgt] < 0) //no need to use "&& nBegRgt < n"nBegRgt++;swapRange(a, nLft, nBegRgt-1);swapRange(a, nBegRgt, nRgt);
}


这篇关于Rearrange an array of positive and negative integers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/719522

相关文章

array_walk()使用

bool  array_walk (  array &$array ,  callable $callback [,  mixed $userdata = NULL ] ) 将用户自定义函数 funcname 应用到 array 数组中的每个单元。 array_walk() 不会受到 array 内部数组指针的影响。array_walk() 会遍历整个数组而不管指针的位置。 array

【C++11 之新增容器 array、foward_list、tuple、unordered_(multi)map/set】应知应会

C++11 标准中新增了多个容器,这些容器为 C++ 程序员提供了更多的选择,以满足不同的编程需求。以下是对这些新容器的介绍和使用案例: std::array 介绍: std::array 是一个固定大小的数组容器,它在栈上分配内存,并提供了类似于标准库容器的接口。它提供了更好的类型安全性和范围检查,同时保持了与原生数组相似的性能。std::array 的大小必须在编译时确定,并且不能更改。

Python中的range()与array()函数

我们在Python中存在一个非常好用的range()与array()函数,下面作用法简要介绍。 一、range()函数   >>> range(1,10)   ——>不包括10[1, 2, 3, 4, 5, 6, 7, 8, 9]>>>range(1,10,2)  ——>1到10,间隔为2(不包括10)[1, 3, 5, 7, 9]>>>range(10)    ——>0到10,不

array_key_exists() expects parameter 2 to be array, null given

公众号获取微信服务器IP地址 错误代码如下 public function getwxIP(){//获取微信服务器IP地址$accessToken = $this->getwxoaiAccessToken();$userToken = new UserToken();$result = $userToken->curl_get("https: //api.weixin.qq.com/cgi-

C#中的数组Array和List集合区别

在C#中,数组(Array)和List集合(List<T>)是两种不同的数据结构,它们有一些区别,主要包括以下几点: 固定长度 vs 动态长度: 数组是固定长度的数据结构,一旦创建后,其长度无法改变。 List集合是动态长度的数据结构,可以根据需要动态增加或减少元素。 类型限制: 数组可以存储任意类型的元素,包括值类型和引用类型。 List集合是泛型集合,可以指定存储的元素类型,例如 Li

442. Find All Duplicates in an Array 找数组中重复的数

https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/description/ 题意:找出数组中所有重复的元素 思路1:先将数组排序,然后判断是否重复,即若nums[i] != nums[i+1],即说明nums[i]非重复元素,可以删去. 重点在于指针(迭代器)的操作,由于vector的erase(删除)操作需要通

【GNU笔记】【C扩展系列】双字整数 Double-Word Integers

【GNU笔记】【C扩展系列】双字整数 Double-Word Integers 双字整数 Double-Word Integers ISO C99和ISO C++11支持至少64位宽的整数的数据类型,作为扩展,GCC在C90和C++98模式下支持它们。对于有符号的整数,只需写long long int,对于无符号的整数,只需写unsigned long long int。要使一个整数常量成为l

【GNU笔记】【C扩展系列】128位整数 128-bit Integers

【GNU笔记】【C扩展系列】128位整数 128-bit Integers 128位整数 128-bit Integers 作为扩展,整数标量类型__int128支持用于整数模式宽度足以容纳 128 位的目标。对于有符号的128位整数,只需写__int128;对于无符号的128位整数,只需写无符号unsigned __int128。对于宽度小于128位的long long整数的目标,GCC不支

[Codeforces 451B] Sort the Array (实现)

Codeforces - 451B 给定一个序列,其中每个数都不相同 问是否能在翻转其中一段后,整个序列变得单调递增 实现题 首先设一个 B B数组为 AA数组排序后的结果 由于只能翻转一个区间,那么我假装 A是满足要求的 找到最小的 A[l]≠B[l] A[l] \ne B[l],最大的 A[r]≠B[r] A[r] \ne B[r], 翻转的区间将会是 [l,r

巧用php中的array_filter()函数去掉多维空值的代码分享

在我们开发过程中,判断数组为空时你会想到什么方法呢?首先想到的应该是empty函数,不过直接用empty函数判断为空是不对的,因为当这个值是多维数的时候,empty结果是有值的 其实我们可以利用array_filter函数轻松去掉多维空值,而数组的下标没有改变,下面是举例用法:  <?php $array = array( 0 => 'nicegy', 1 => false, 2 =