图解LeetCode——1470. 重新排列数组(难度:简单)

一、题目

给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。

请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。

二、示例

2.1> 示例 1:

【输入】nums = [2,5,1,3,4,7], n = 3
【输出】[2,3,5,4,1,7]
【解释】由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案为 [2,3,5,4,1,7]

2.2> 示例 2:

【输入】nums = [1,2,3,4,4,3,2,1], n = 4
【输出】[1,4,2,3,3,2,4,1]

2.3> 示例 3:

【输入】nums = [1,1,2,2], n = 2
【输出】[1,2,1,2]

提示:

  • 1 <= n <= 500
  • nums.length == 2n
  • 1 <= nums[i] <= 10^3

三、解题思路

3.1> 思路1:赋值新数组

根据题目描述,我们很容易想到通过创建一个新的数组,然后再将旧的数组中的元素,按照一定的规律,迁移到新的数组即可。那么题目中给出的数组长度是2 * n,那么要求最终的数据是nums[0],nums[n],nums[1],nums[n+1],……,所以我们只需要遍历数组nums长度的一半,即:n的长度就可以了。如下图中的红蓝两个指针,红色指针为i,那么蓝色指针为i+n,然后依次将获取到的数据保存到新的数组即可。具体操作如下图所示:

思路1的逻辑比较简单,具体实现请参照:4.1> 实现1:赋值新数组

3.2> 思路2:原数组内修改

上面思路1中我们创建了一个新的数组,所以它的空间复杂度是O(n),那我们有没有一种方式,不去创建这个新的数组呢?使得空间复杂度为O(1)呢?其实是可以的。通过题目描述,我们可以知道数组中元素值的范围是:1 <= nums[i] <= 10^3。 那么最大值也就是1000了,将其转换为2进制为1111101000,只占了总共32位中的低10位。那么理论上,低10位如果都是1的话,值为1023,我们要是想获得某32位中低10位的值,就可以通过按位与(&)1023来获得了。那既然旧值只使用了低10位,还有22位是空闲的,所以,我们就将第19~10位作为新值的存储位置(下图黄色部分)。具体逻辑,如下图所示:

我们举例,要将nums[3]的值赋值到nums[1]的位置上。那么,首先获得nums[3]的值,因为该值要作为新的值,后续要放到nums[1]的第19至第10位上,所以,我们先将其向左移动10位,那么,它就处于第19位至第10位了。然后,我们再将其与nums[1]的值进行按位或(|)操作。那么最终nums[1]位置上的结果就是一个包含了新值和旧值的结果了。当所有元素都迁移完毕后,我们最终需要的还是新值,那怎么获取到呢?其实方法很简单,只需要将nums数组中每个元素都向右移动10位就可以了。具体操作如下图所示:

细心的同学会发现,上面的逻辑好像有问题吧,如果我们要将num[1]的值赋值给别的位置呢?此时的num[1]可是包含了新值和旧值了,那么这样不就有问题了吗?是这样的。所以,此时我们就需要1023这个值(低10位都是1)来获得nums[1]中的旧值了,具体来说,可以采用按位与(&)的方式,就可以了。具体如下图所示:

 思路大致就是这些了,具体的实现,请参照:4.2> 实现2:原数组内修改

四、代码实现

4.1> 实现1:赋值新数组

class Solution {
    public int[] shuffle(int[] nums, int n) {
        int[] result = new int[2*n];
        int index = 0;
        for (int i = 0; i < n; i++) {
            result[index++] = nums[i];
            result[index++] = nums[i + n];
        }
        return result;
    }
}

4.2> 实现2:原数组内修改

class Solution {
    public int[] shuffle(int[] nums, int n) {
        int index = 0, mask = (1 << 10) - 1;
        for (int i = 0; i < n; i++) {
            nums[index++] = (nums[i] & mask) << 10 | (nums[index - 1] & mask);
            nums[index++] = (nums[i + n] & mask) << 10 | (nums[index - 1] & mask);
        }
        for (int i = 0; i < 2*n; i++)  nums[i] >>= 10;
        return nums;
    }
}

今天的文章内容就这些了:

写作不易,笔者几个小时甚至数天完成的一篇文章,只愿换来您几秒钟的 点赞 & 分享 。

更多技术干货,欢迎大家关注公众号“爪哇缪斯” ~ \(^o^)/ ~ 「干货分享,每天更新」

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.elasticsearch.client.ResponseException: method [PUT], host [http://192.168.93.132:9200], URI [/TestIndex], status line [HTTP/1.1 406 Not Acceptable] {"error":"Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported","status":406} at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:347) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:313) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:288) at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:146) at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:266) at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:282) at com.sora.leetcode_notebook.ElasticSearch.ElasticsearchJavaApi.createIndex(ElasticsearchJavaApi.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
最新发布
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值