Easiest way to Rotate a List in c#

后端 未结 16 3133
鱼传尺愫
鱼传尺愫 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:33

    Using Linq,

    List<int> temp = new List<int>();     
    
     public int[] solution(int[] array, int range)
        {
            int tempLength = array.Length - range;
    
            temp = array.Skip(tempLength).ToList();
    
            temp.AddRange(array.Take(array.Length - range).ToList());
    
            return temp.ToArray();
        }
    
    0 讨论(0)
  • 2020-12-01 07:34

    How about using modular arithmetic :

    public void UsingModularArithmetic()
    { 
      string[] tokens_n = Console.ReadLine().Split(' ');
      int n = Convert.ToInt32(tokens_n[0]);
      int k = Convert.ToInt32(tokens_n[1]);
      int[] a = new int[n];
    
      for(int i = 0; i < n; i++)
      {
        int newLocation = (i + (n - k)) % n;
        a[newLocation] = Convert.ToInt32(Console.ReadLine());
      }
    
      foreach (int i in a)
        Console.Write("{0} ", i);
    }
    

    So basically adding the values to the array when I am reading from console.

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

    My solution for Arrays:

        public static void ArrayRotate(Array data, int index)
        {
            if (index > data.Length)
                throw new ArgumentException("Invalid index");
            else if (index == data.Length || index == 0)
                return;
    
            var copy = (Array)data.Clone();
    
            int part1Length = data.Length - index;
    
            //Part1
            Array.Copy(copy, 0, data, index, part1Length);
            //Part2
            Array.Copy(copy, part1Length, data, 0, index);
        }
    
    0 讨论(0)
  • 2020-12-01 07:36
    public static int[] RightShiftRotation(int[] a, int times) {
      int[] demo = new int[a.Length];
      int d = times,i=0;
      while(d>0) {
        demo[d-1] = a[a.Length - 1 - i]; d = d - 1; i = i + 1;
      }
      for(int j=a.Length-1-times;j>=0;j--) { demo[j + times] = a[j]; }
      return demo;
    }
    
    0 讨论(0)
  • 2020-12-01 07:39

    You could implement it as a queue. Dequeue and Enqueue the same value.

    **I wasn't sure about performance in converting a List to a Queue, but people upvoted my comment, so I'm posting this as an answer.

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

    It seems like some answerers have treated this as a chance to explore data structures. While those answers are informative and useful, they are not very Linq'ish.

    The Linq'ish approach is: You get an extension method which returns a lazy IEnumerable that knows how to build what you want. This method doesn't modify the source and should only allocate a copy of the source if necessary.

    public static IEnumerable<IEnumerable<T>> Rotate<T>(this List<T> source)
    {
      for(int i = 0; i < source.Count; i++)
      {
        yield return source.TakeFrom(i).Concat(source.TakeUntil(i));
      }
    }
    
      //similar to list.Skip(i-1), but using list's indexer access to reduce iterations
    public static IEnumerable<T> TakeFrom<T>(this List<T> source, int index)
    {
      for(int i = index; i < source.Count; i++)
      {
        yield return source[i];
      }
    }
    
      //similar to list.Take(i), but using list's indexer access to reduce iterations    
    public static IEnumerable<T> TakeUntil<T>(this List<T> source, int index)
    {
      for(int i = 0; i < index; i++)
      {
        yield return source[i];
      }
    }
    

    Used as:

    List<int> myList = new List<int>(){1, 2, 3, 4, 5};
    foreach(IEnumerable<int> rotation in myList.Rotate())
    {
      //do something with that rotation
    }
    
    0 讨论(0)
提交回复
热议问题