题目

https://leetcode.com/problems/beautiful-arrangement-ii/

题解

偏智力题,评论区有人说,A typical question testing whether you’re “smart” rather than “taking algorithm class”.

官方 O(n) 题解没看懂(没仔细看),评论区 看到一个 O(n^2) 的解法,比较清晰。

数据量 n=10^4,O(n^2) 的解能通过,说明在力扣上指令条数 1*10^8 是可以通过的。

class Solution {public int[] constructArray(int n, int k) {// 初始化为1到n,此时差值为1种int[] arr = new int[n];for (int i = 0; i < n; i++) {arr[i] = i + 1;}// 然后从第二位到末位开始不停翻转,每翻转一次增加一种差值for (int i = 1; i < k; i++) {swap(arr, i, n - 1);}return arr;}public void swap(int[] arr, int L, int R) {for (int i = 0; i <= (R - L) / 2; i++) {int t = arr[L + i];arr[L + i] = arr[R - i];arr[R - i] = t;}}
}

效率很低,意料之中

leetcode 667. Beautiful Arrangement II | 667. 优美的排列 II(Java)相关推荐

  1. Java实现 LeetCode 667 优美的排列 II(暴力)

    667. 优美的排列 II 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件: ① 如果这个数组是 [a1, a2, a3, - , an ...

  2. LeetCode 526. Beautiful Arrangement

    526. Beautiful Arrangement Suppose you have N integers from 1 to N. We define a beautiful arrangemen ...

  3. 【leetcode】667. Beautiful Arrangement II

    题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...

  4. 667. Beautiful Arrangement II

    找规律 1,2,... , n 乱序排列,相邻数据的绝对差最多有n-1种 比如1,2,3,4,5对应于 1 5 2 4 3 class Solution { public:vector<int& ...

  5. LeetCode 667. 优美的排列 II(找规律)

    1. 题目 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件: ① 如果这个数组是 [a1, a2, a3, ... , an] ,那么数 ...

  6. leetcode 526. Beautiful Arrangement | 526. 优美的排列(回溯)

    题目 https://leetcode.com/problems/beautiful-arrangement/ 题解 首先分析,全排列一个一个试的话(如下图),时间复杂度O(n^2),当n=15时,总 ...

  7. LeetCode笔记:526. Beautiful Arrangement

    问题: Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is c ...

  8. 【Leetcode】526. Beautiful Arrangement

    Description: Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array ...

  9. leetcode初级算法4.两个数组的交集 II

    leetcode初级算法4.两个数组的交集 II 仅为个人刷题记录,不提供解题思路 题解与收获 我的解法:(总结在代码中) public int[] intersect(int[] nums1, in ...

最新文章

  1. Springboot+JPA 对应关系查询时导致的堆栈溢出 :java.lang.StackOverflowError:
  2. 创建对象并且使用对象的属性和方法
  3. Java面向对象部分小结
  4. 海岸鸿蒙慕建洲,用于校准液体自动颗粒计数器的标准物质及其制备方法.pdf
  5. flink 任务执行类的加载
  6. 泡泡玛特就“女性面试需填生育计划”一事致歉 称将积极改进
  7. C++ 类的前向声明
  8. [渝粤教育] 云南大学 高等数学B(2) 参考 资料
  9. Linux下MySQL的基础(一)
  10. C#经纬度坐标算距离
  11. Type mismatch: inferred type is T but ViewModel was expected
  12. django xadmin修改“管理”“认证和授权”的菜单名
  13. 类似京东淘宝历史搜索自适应长度搜索项超两行折叠功能实现
  14. iscsi没有可用于使用快速连接登陆的目标_【解密】5G商用在即 OPPO快速网络切换方法;高管宣布华为河图商标注册成功;小米折叠屏专利曝光 类似于摩托罗拉Razr...
  15. 累死你的不是工作方式
  16. Photoshop支持ICO图片格式
  17. html div背景图片路径,css如何设置背景图片位置?
  18. 改编名著成风 麒麟游戏将研发《红楼梦》网游
  19. 实现页面方块跟随鼠标移动而移动
  20. 【单片机毕业设计】【mcuclub-202】大棚环境检测(土壤温湿度) | 大棚温湿度检测 | 农田灌溉系统 | 智能灌溉系统 | 智慧农业灌溉

热门文章

  1. CodeForces - 1354E Graph Coloring(dfs判断二分图+dp)
  2. HDU - 5978 To begin or not to begin(简单博弈)
  3. 计算机桌面ie图标无法删除,win7系统桌面ie图标无法删除怎么办
  4. 20000字节的包算大吗_20000的包和200的包区别,戳中了万千女人的内心!
  5. SPOJ4487(Splay树)
  6. 9.OD-断点、命令
  7. 对现有的所能找到的DDOS代码(攻击模块)做出一次分析----ICMP篇
  8. 网狐棋牌(十) 成也萧何败也肖何? IUnknowEx
  9. WinSock嗅探虚拟主机拿站取webshell
  10. Ubuntu 16.04 安装 Gazebo