Easiest way to Rotate a List in c#

后端 未结 16 3134
鱼传尺愫
鱼传尺愫 2020-12-01 07:06

Lists say I have a list List {1,2,3,4,5}

Rotate means:

=> {2,3,4,5,1} => {3,4,5,1,2} => {4,5,1,2,3}
相关标签:
16条回答
  • 2020-12-01 07:40

    How about this:

    var output = input.Skip(rot)
                      .Take(input.Count - rot)
                      .Concat(input.Take(rot))
                      .ToList();
    

    Where rot is the number of spots to rotate - which must be less than the number of elements in the input list.

    As @cadrell0 answer shows if this is all you do with your list, you should use a queue instead of a list.

    0 讨论(0)
  • 2020-12-01 07:43

    List<T>

    The simplest way (for a List<T>) is to use:

    int first = list[0];
    list.RemoveAt(0);
    list.Add(first);
    

    Performance is nasty though - O(n).

    Array

    This is basically equivalent to the List<T> version, but more manual:

    int first = array[0];
    Array.Copy(array, 1, array, 0, array.Length - 1);
    array[array.Length - 1] = first;
    

    LinkedList<T>

    If you could use a LinkedList<T> instead, that would be much simpler:

    int first = linkedList.First;
    linkedList.RemoveFirst();
    linkedList.AddLast(first);
    

    This is O(1) as each operation is constant time.

    Queue<T>

    cadrell0's solution of using a queue is a single statement, as Dequeue removes the element and returns it:

    queue.Enqueue(queue.Dequeue());
    

    While I can't find any documentation of the performance characteristic of this, I'd expect Queue<T> to be implemented using an array and an index as the "virtual starting point" - in which case this is another O(1) solution.

    Note that in all of these cases you'd want to check for the list being empty first. (You could deem that to be an error, or a no-op.)

    0 讨论(0)
  • 2020-12-01 07:43

    I was asked to reverse a character array with minimal memory usage.

    char[] charArray = new char[]{'C','o','w','b','o','y'};

    Method:

    static void Reverse(ref char[] s)
    {
        for (int i=0; i < (s.Length-i); i++)
        {
            char leftMost = s[i];
            char rightMost = s[s.Length - i - 1];
    
            s[i] = rightMost;
            s[s.Length - i - 1] = leftMost;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:45

    Try

    List<int> nums = new List<int> {1,2,3,4,5};
    var newNums = nums.Skip(1).Take(nums.Count() - 1).ToList();
    newNums.Add(nums[0]);
    

    Although, I like Jon Skeet's answer better.

    0 讨论(0)
  • 2020-12-01 07:46

    My solution maybe too basic (I wouldn't like to say it's lame...) and not LINQ'ish.
    However, it has a pretty good performance.

    int max = 5; //the fixed size of your array.
    int[] inArray = new int[5] {0,0,0,0,0}; //initial values only.
    
    void putValueToArray(int thisData)
    {
      //let's do the magic here...
      Array.Copy(inArray, 1, inArray, 0, max-1);
      inArray[max-1] = thisData;
    }
    
    0 讨论(0)
  • 2020-12-01 07:46

    below is my approach. Thank you

    public static int[] RotationOfArray(int[] A, int k)
      {
          if (A == null || A.Length==0)
              return null;
          int[] result =new int[A.Length];
          int arrayLength=A.Length;
          int moveBy = k % arrayLength;
          for (int i = 0; i < arrayLength; i++)
          {
              int tmp = i + moveBy;
              if (tmp > arrayLength-1)
              {
                  tmp =  + (tmp - arrayLength);
              }
              result[tmp] = A[i];             
          }        
          return result;
      }
    
    0 讨论(0)
提交回复
热议问题