Left Shift Array with O(l) time complexity and O(1) space complexity

Algorithm 1:

public static void main(String[] args)
    {
        int[] a = new int[]{1,2,3,4,5,6,7,8,9,10,11,12};
        shiftN2(a, 1);
        System.out.println(Arrays.toString(a));
    }

    public static void shiftN2(int[] a, int n) {
        n = n%a.length;
        swapArray(a, 0, n-1);
        swapArray(a, n, a.length-1);
        swapArray(a, 0, a.length-1);
    }

    private static void swapArray(int[] a, int from, int to) //[from, to]
    {
        int temp;
        while(from<to)
        {
            temp = a[from];
            a[from++] = a[to];
            a[to--] = temp;
        }
    }

Algorithm 2:

public static void main(String[] args)
    {
        int[] a = new int[]{1,2,3,4,5,6,7,8,9,10,11,12};
        for(int i = 0;i<a.length; ++i)
        {
            int[] aCopy = Arrays.copyOf(a, a.length);
            shiftN(aCopy, i);
            System.out.println(Arrays.toString(aCopy));
        }
    }

    public static void shiftN(int[] a, int n) {
        int l = a.length;
        n = n%l;
        if(n==0)
            return;
        int changed = 0;
        int i = 0;
        while(changed<l)
        {
            int start = 0 -n + l + i;
            int from = i;
            int to = start;
            int temp = a[to];
            while(from != start)
            {
                a[to]=a[from];
                ++changed;
                to = from;
                from = (from + n)%l;
            }
            a[to]=temp;
            ++i;
            ++changed;
        }
    }
时间: 2024-08-17 13:25:36

Left Shift Array with O(l) time complexity and O(1) space complexity的相关文章

空间复杂度是什么?What does ‘Space Complexity’ mean? ------geeksforgeeks 翻译

这一章比较短! 空间复杂度(space complexity)和辅助空间(auxiliary space)经常混用,下面是正确的辅助空间和空间复杂度的定义 辅助空间:算法需要用到的额外或者暂时的存储空间. 空间复杂度:是指算法所需要的所有存储空间,这是跟输入数据的大小决定的.空间复杂度包括辅助空间和保存输入的存储空间. 如果我们想比较几个标准的排序算法所需要的空间,那么用辅助空间来分析会比空间复杂度好.归并排序用了O(n)的辅助空间.而插入排序和堆排序用的O(1)的辅助空间.但是他们这些算法的空

quickSort in-place version whose space complexity is O(log(N))

1 public void quickSortSwapping(int data[]){ 2 //call this method 3 quickSortSwapping(data,0,data.length); 4 } 5 6 7 public void quickSortSwapping(int data[],int start,int len){ 8 if(len<2)return; 9 int pivotIndex=start+len/2; 10 int pivotValue=data[

Sort a linked list in O(n log n) time using constant space complexity.

1 利用归并的方法进行排序 2 完成两个主要功能即可:拆分和合并 3 拆分用到了比较好的方法就是利用快慢指针进行,每次找到链表的中间部分进行拆解 4 合并可以利用两种方式:一种是非递归的方法另一种是递归的方法,都是可以做的 5 个人比较喜欢递归的方法,理解起来比较简单 6 /** 7 * Definition for singly-linked list. 8 * struct ListNode { 9 * int val; 10 * ListNode *next; 11 * ListNode(

565. Array Nesting

Problem statement: A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1]. Sets S[K] for 0 <= K < N are defined as follows: S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }. Sets S[K] are

Split Array Largest Sum LT410

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Examples: Input: nums = [7,2,5,10,8] m = 2

334. Increasing Triplet Subsequence

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k  such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else retur

LeetCode-334. Increasing Triplet Subsequence

Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-

[LeetCode] Increasing Triplet Subsequence

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return

242. Valid Anagram

Problem statement Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the s