轮转数组[脑筋急转弯|发现规律]

2023-10-29 09:10

本文主要是介绍轮转数组[脑筋急转弯|发现规律],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

轮转数组

  • 前言
  • 一、轮转数组
  • 二、题解
    • 1、空间快解
    • 2、找规律
    • 3、找路径
  • 总结
  • 参考文献

前言

通过轮转数组,考察找规律的能力,以及递归找路径。

一、轮转数组

在这里插入图片描述

二、题解

1、空间快解

//用空间快解
class Rotate2 {public void rotate(int[] nums, int k) {int len = nums.length;int[] arr = new int[len];System.arraycopy(nums, 0, arr, 0, nums.length);for (int i = 0; i < nums.length; i++) nums[(i + k) % nums.length] = arr[i];}}

2、找规律

/*
len = 6; k = 4
cur : -->---->  target: ---->-->
reverse cur : <----<--
reverse sub : ---->-->*/class Rotate3 {//观其移动前、移动、移动后的联系和规律。/*len = 6; k = 4; > 表示元素呈现的方向。cur : -->---->  target: ---->-->reverse cur : <----<--reverse sub : ---->-->*/public void rotate(int[] nums, int k) {//获取有效的kint len = nums.length;k %= len;reverse(nums, 0, len - 1);reverse(nums, 0, k - 1);reverse(nums, k, len - 1);}private void reverse(int[] nums, int begin, int end) {for (int i = 0; i < end - begin + 1 >>> 1; i++) {int temp = nums[begin + i];nums[begin + i] = nums[end - i];nums[end - i] = temp;}}}

3、找路径

//轮转数组
public class Rotate {/*target:将数组元素整体向前移动k位。1-将元素向前移动k位,移动nums.len位就回到了原来的地方,所以应该移动k = k % nums.len位。2-将元素向前移动k位,就会占到别人的位置,必须先把别人的位置移走,直至回到起始位置。所以这是一个找路径问题,找打最后一个位置位起始位置,记录起始值,然后依次回溯覆盖。bug1:可能有些数字不会被遍历到,所以可采用count计数来统计已经交换了多少次,若一轮下来,count<len,则从第二个数开始交换,直至count = len*/public void rotate(int[] nums, int k) {//获取有效移动步数int len = nums.length;k %= len;//用递归栈找路径int beginVal = nums[begin];while (count < len) {//先把初始值取出来,让该值被覆盖。nums[findPath(nums, (begin + k) % nums.length, k)] = beginVal;beginVal = nums[(++begin) % nums.length];count++;}}int begin = 0, count = 1;private int findPath(int[] nums, int cur, int k) {if (begin == cur || count == nums.length) return cur;count++;nums[findPath(nums, (cur + k) % nums.length, k)] = nums[cur];return cur;}
}

总结

1)多观察,多思考,找到问题中的规律。

参考文献

[1] LeetCode 翻转数组
[2] LeetCode 官方题解

这篇关于轮转数组[脑筋急转弯|发现规律]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

C语言:柔性数组

数组定义 柔性数组 err int arr[0] = {0}; // ERROR 柔性数组 // 常见struct Test{int len;char arr[1024];} // 柔性数组struct Test{int len;char arr[0];}struct Test *t;t = malloc(sizeof(Test) + 11);strcpy(t->arr,

C 语言基础之数组

文章目录 什么是数组数组变量的声明多维数组 什么是数组 数组,顾名思义,就是一组数。 假如班上有 30 个同学,让你编程统计每个人的分数,求最高分、最低分、平均分等。如果不知道数组,你只能这样写代码: int ZhangSan_score = 95;int LiSi_score = 90;......int LiuDong_score = 100;int Zhou

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };

PHP7扩展开发之数组处理

前言 这次,我们将演示如何在PHP扩展中如何对数组进行处理。要实现的PHP代码如下: <?phpfunction array_concat ($arr, $prefix) {foreach($arr as $key => $val) {if (isset($prefix[$key]) && is_string($val) && is_string($prefix[$key])) {$arr[

Go 数组赋值问题

package mainimport "fmt"type Student struct {Name stringAge int}func main() {data := make(map[string]*Student)list := []Student{{Name:"a",Age:1},{Name:"b",Age:2},{Name:"c",Age:3},}// 错误 都指向了最后一个v// a