We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Arrays: Left Rotation
- Discussions
Arrays: Left Rotation
Arrays: Left Rotation
+ 0 comments Simple Java Code
public static List<Integer> rotLeft(List<Integer> a, int d) { List<Integer> returnList = new ArrayList<>(); int max = a.size(); for(int i = d; i < max; i++){ returnList.add(a.get(i)); if(i + 1 == a.size()){ i = -1; max = d; } } return returnList; }
+ 0 comments C++
vector<int> rotLeft(vector<int> a, int d) { int n = a.size(); vector<int> arr(a.size(), 0); for(int i = 0; i < n; ++i) { int index = i - (n - d); if(index < 0) index += n; arr[i] = a[index]; } return arr; }
+ 0 comments C#
public static List<int> rotLeft(List<int> a, int d) { var count = a.Count; var newA = new int[count]; for (var i = 0; i < count; i++) { var newIndex = count + i - d; newIndex = newIndex >= count ? newIndex - count : newIndex; newA[newIndex] = a[i]; } return newA.ToList(); }
+ 0 comments Here's my solution:
vector rotateLeft(int d, vector arr) { vector a; int j=0;
a=arr; for(int i=0;i<arr.size();i++) { if((i+d)<(arr.size())) { arr[i]=a[i+d]; } else{ arr[i]=a[j]; j++; } }
return arr; }
+ 0 comments Please someone help me
public static List rotationLeft(List a, int b) { List newList = new ArrayList<>(); int size = a.size(); if(b == size) return a; else if (b > size) b = b -size; else if(b < 0) b = size + b; int [] tab = new int[size]; int [] tab2 = new int[size]; int i = 0; for(int item: a) { tab[i] = item; i++; }
int j = 0; for(int e = size -b ; e < size; e++) { tab2[e] = tab[j]; j++; } int indice = b; for(int n = size -b -1; n >=0; n--){ tab2[n] = tab[indice]; indice++; } tab2[size - b] = tab[0]; for (int entier: tab2) { newList.add(entier); } return newList; }
Load more conversations
Sort 4869 Discussions, By:
Please Login in order to post a comment