课程: C# Algorithms
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: rotate an array
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 contain 2 through 6 and then 1. Let's implement it. The first step is to create a new array with the same size as the input array. This will create a new array, the same size as numbers, but with each item set to 0. 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 0 of the rotated array to be the item at index 1 of the numbers array. We'll also want to set the item at index 1 of the rotated array to be the item at index 2 of the numbers array. And this pattern continues on with the next set until the end of the array. Seeing this pattern, we can set the item at index i-1 of the rotated array to be the item at index i of the numbers array. This shifts all the contents to the left and it'll work for most of our array elements…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。