就这么一道tag为Easy的题,对自己来说也不简单呐,犯了各种错误,死循环,数组越界,逻辑错误…,先是用js在页面上试验,结果死循环浏览器卡死,只好先换成java在eclipse上调试。按理这道题用到的就是冒泡排序、选择排序里的操作吧。
自己只会用各种库与框架,如果要从头写,看来连对数组操作也不熟悉,google关键字java array shift elements left
,找到:
- How do I move the elements of an array by one cell towards left?
- Java, Shifting Elements in an Array
最后总算在leetcode上跑通了,不过还是稀里糊涂的。性能不行,不知道人家高手是怎么做的?难道用快速排序的思路?
java
1 | class Solution { |
Submission Detail
- 21 / 21 test cases passed.
- Your runtime beats 11.98 % of java submissions.
javascript
1 | /** |
Submission Detail
- 21 / 21 test cases passed.
- Your runtime beats 18.25 % of javascript submissions.