课程: Java Algorithms
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: Rotate an array
- [Instructor] Let's create an algorithm that rotates the contents of an array to the left by one. We start off with an input array. After the rotation, the new array should have two through six and then one. Let's implement the algorithm. The first step is to create a new array with the same size as the input array. This'll be the array we return. Here, we create a new array, the same size as numbers, but with each item in the array set to zero. Next, we'll copy the elements from the numbers array to the rotated array, but at one index to the left. This means we'll want to set the item at index zero of the rotated array to be the item at index one of the numbers array. We'll also want to set the item at index one of the rotated array to be the item at index two of the numbers array. The pattern continues with the next set until the end of the array is reached. Seeing this pattern, we can set the item at index I minus…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。