- 浏览: 131657 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
[分析] 用到的技巧,全局reverse再局部reverse完成偏移若干位操作。
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
[分析] 用到的技巧,全局reverse再局部reverse完成偏移若干位操作。
public class Solution { // method 1: time O(N), space O(N) public void rotate1(int[] nums, int k) { if (nums == null || nums.length == 0 || k % nums.length == 0) return; int n = nums.length; k = k % n; int[] tmp = new int[k]; for (int i = 0; i < k; i++) { tmp[i] = nums[n - k + i]; } for (int i = n - k - 1; i >= 0; i--) { nums[i + k] = nums[i]; } for (int i = 0; i < k; i++) { nums[i] = tmp[i]; } } // method 2: time O(k * n), space O(1), timeout public void rotate2(int[] nums, int k) { if (nums == null || nums.length == 0 || k % nums.length == 0) return; int n = nums.length; k = k % n; if (k < n / 2) { for (int i = 0; i < k; i++) { int tmp = nums[n - 1]; for (int j = n - 1; j > 0; j--) { nums[j] = nums[j - 1]; } nums[0] = tmp; } } else { for (int i = 0; i < k; i++) { int tmp = nums[0]; for (int j = 0; j < n - 1; j++) { nums[j] = nums[j + 1]; } nums[n - 1] = tmp; } } } // method 3: time O(n), space O(1) public void rotate(int[] nums, int k) { if (nums == null || nums.length == 0 || k % nums.length == 0) return; int n = nums.length; k = k % n; reverse(nums, 0, n - 1); reverse(nums, 0, k - 1); reverse(nums, k, n - 1); } public void reverse(int[] nums, int left, int right) { while (left < right) { int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; left++; right--; } } }
发表评论
-
Leetcode - H-Index
2015-09-06 09:08 1094Given an array of citations (ea ... -
Leetcode - Add Binary
2015-08-21 09:28 438[分析] 从低位往高位逐位相加,就是这么一个简单的题却花了我一 ... -
Leetcode - Longest Consecutive Sequence
2015-08-20 21:20 461[分析] base version说几句: 数组题一定要考虑重 ... -
Leetcode - First Missing Positive
2015-08-20 07:45 612[分析] 将各个正数放入相应下标处,使之满足nums[nums ... -
Leetcode - Set Matrix Zeros
2015-08-19 20:55 522Given a m x n matrix, if an ele ... -
Leetcode - Rotate Image
2015-08-19 19:51 470[分析] 自己的思路:从外到内一圈圈顺时针旋转90度,坐标映射 ... -
Missing Ranges
2015-08-19 09:48 490[分析] 此题若不考虑极大值极小值相关的corner case ... -
Leetcode - 3Sum Smaller
2015-08-18 22:12 1454Given an array of n integers nu ... -
Leetcode - Spiral Matrix II
2015-08-18 19:49 467Given an integer n, generate a ... -
Leetcode - Spiral Matrix
2015-08-18 09:50 397Given a matrix of m x n element ... -
Leetcode - Contains Duplicate II
2015-08-18 07:57 531Given an array of integers and ... -
Leetcode - Shortest Word Distance II
2015-08-18 07:25 1323This is a follow up of Shortest ... -
Leetcode - Shortest Word Distance III
2015-08-17 22:05 1635This is a follow up of Shortest ... -
Leetcode - Shortest Word Distance
2015-08-17 22:01 888Given a list of words and two w ... -
Leetcode - Insert Interval
2015-08-14 10:12 594[分析] 这道题思路直接,但bug free需要费些心力。第一 ... -
Leetcode - Valid Sudoku
2015-07-30 21:57 454Determine if a Sudoku is valid, ... -
Leetcode - House Robber II
2015-05-20 22:34 736Note: This is an extension of ... -
Leetcode - Largest Rectangle in Histogram
2015-05-20 07:53 465Given n non-negative integers ... -
MockInterview-IPRangeData Find&Insert
2015-04-12 08:09 0[题目] Finding the IP address w ... -
Leetcode - MissingRange
2015-03-31 09:25 427Given a sorted integer array ...
相关推荐
Rotate Array (旋转数组) #数组 33. Search in Rotated Sorted Array(搜索旋转排序数组)#数组 2020/12/08 19. Remove Nth Node From End of List(删除链表的倒数第N个节点) 153. Find Minimum in Rotated Sorted...
189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life Find All Numbers Disappeared in an Array ...
rotate: A Linear Time Majority Vote Algorithm 题解: Maximum Gap: Something about largest-rectangle-in-histogram: 最长递增子序列: 最长公共自序列: Something about: best time to buy and sell stack: ...
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...
https://leetcode.com/problems/rotate-array/description/ * * algorithms * Easy (28.74%) * Total Accepted: 262.7K * Total Submissions: 913.5K * Testcase Example: '[1,2,3,4,5,6,7]\n3' * * Given an array,...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 ...Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 ...
462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...
Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -> 2 26. Remove Duplicates from Sorted Array (E) 27. ...
旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/...
leetcode中国Final450_数据结构 实际上,这个存储库包含成为优秀竞争程序员最重要的数据结构和算法。 他们的名字是: Questions ----------- *Reverse the array *Find the maximum and minimum element in an array...
leetcode中国大批 0. Count Prime 1. Reverse the array 2. Find the maximum and minimum element in an array 3. Find the "Kth" max and min element of an array 4. Given an array which consists of only 0, 1...
PS:初学算法,开始刷leetcode,Rotate array的预备知识(写的代码Time Limit Exceed难过)于是百度高效算法,本篇作为预备知识。 1、strcpy和strncpy函数 这个不陌生,大一学C语言讲过,其一般形式为strcpy(字符...
189_Rotate_array 41_First_Missing_Positive 299_Bulls_and_Cows 134_Gas_Station 118_Pascal's_Triangle_I 119_Pascal's_Triangle_II 169_Majority_Element 229_Majority_Element_II 274_H_索引 275_H_Index_II ...
Rotate Image 344. Reverse String 414. Third Maximum Number 448. Find All Numbers Disappeared in an Array 66. Plus One 238. Product of Array Except Self 697. Degree of an Array 849. Maximize ...
leetcode添加元素使和等于 LeetCodeNotes Array: 4. Median of Two Sorted Arrays 思路: 基于二分查找的思想 88. Merge Sorted Array 描述:合并两个有序数组,将B合并入A,A长度刚好为A.length + B.length nums1 =...
1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth ...
Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self Game of Life Increasing Triplet Subsequence 单链表 Reverse Linked List Odd Even Linked List ...
leetcode SDE-问题 标准 SDE 问题列表 第一天:(数组) 日 问题陈述 解决方案 困难 使用的数据结构 使用的算法 时间复杂度 空间复杂度 补充阅读 在 N 个整数的数组中查找重复项 中等的 大批 不适用 上) O(1) 在不...