leetcode [384] 打乱数组

编程入门 行业动态 更新时间:2024-10-26 12:23:46

leetcode [384] 打乱<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组"/>

leetcode [384] 打乱数组

[384] 打乱数组

给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。

实现 Solution class:

  • Solution(int[] nums) 使用整数数组 nums 初始化对象
  • int[] reset() 重设数组到它的初始状态并返回
  • int[] shuffle() 返回数组随机打乱后的结果

打乱的时候clone一个新的数组,然后打乱这个新的数组即可。

class Solution {private int[] nums;private Random random;public Solution(int[] nums) {this.nums = nums;random = new Random();}//重置数组,就是返回之前的数组public int[] reset() {return nums;}//打乱数组public int[] shuffle() {if (nums == null) {return null;}int[] clone = nums.clone();for (int i = 0; i < clone.length; i++) {int i1 = random.nextInt(i + 1);swap(clone, i, i1);}}public static void swap(int[] a, int i, int j) {int temp = a[i];a[i] = a[j];a[j] = temp;}
}

水仙花

public class Shuixianghua {List f(int start, int end) {List<Integer> list = new ArrayList<>();int j = 0;for(int i = start; i <= end; i++) {if(i == Math.pow(Math.floor(i / 100), 3) + Math.pow(Math.floor(i % 100 / 10), 3)+ Math.pow(i % 100 % 10, 3)){list.add(i);}}return list;}
}

更多推荐

leetcode [384] 打乱数组

本文发布于:2023-07-28 18:02:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   leetcode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!