文章 224
评论 6
浏览 177809
Array - 189. Rotate Array

Array - 189. Rotate Array

189、Rotate Array Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] 思路: 翻转数组有很多种做法,这里就只说一种 time:O(n) space:O(1),思路就是经过三次翻转数组,达到只翻转到k的目的。 代码: java: class Solution { public void rotate(int[] nums, int k) { k= k % nums.length; reve....

Array - 277 Find the Celebrity

Array - 277 Find the Celebrity

277、Find the Celebrity Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them. Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: “Hi, A. Do you know B?” to get information of whether A knows B. You n....

Array - 80. Remove Duplicates from Sorted Array II

Array - 80. Remove Duplicates from Sorted Array II

80、Remove Duplicates from Sorted Array II 相似题型: 26 Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. ....

hello,solo blog!

hello,solo blog!

博客搬家了

Nothing just happens, it's all part of a plan.