Objective c 在复杂度为O(n)和时间为O(1)的数组中向左旋转元素
我试图解决数组中元素向左旋转的问题。示例:Objective c 在复杂度为O(n)和时间为O(1)的数组中向左旋转元素,objective-c,algorithm,data-structures,big-o,Objective C,Algorithm,Data Structures,Big O,我试图解决数组中元素向左旋转的问题。示例:array=[1,2,3,4,5,6,7]如果调用函数rotateToLeft(array[],int numberrelations,int count)其中:array是要旋转的数组,numberrelations是要向左旋转的元素数,count是数组的大小。我正在寻找一个O(n)复杂度和O(1)耗时的算法。我的第一个解决方案是使用双链接列表,但我想知道是否有更好的解决方案 linkedlist的类节点 @interface Node : N
array=[1,2,3,4,5,6,7]
如果调用函数rotateToLeft(array[],int numberrelations,int count)
其中:array
是要旋转的数组,numberrelations
是要向左旋转的元素数,count
是数组的大小。我正在寻找一个O(n)复杂度和O(1)耗时的算法。我的第一个解决方案是使用双链接列表,但我想知道是否有更好的解决方案
linkedlist的类节点
@interface Node : NSObject
@property (nonatomic,assign) int element;
@property (nonatomic,strong) Node* next;
@property (nonatomic,strong) Node* previous;
@end
@interface LinkedList : NSObject
@property (nonatomic,strong) Node* tail;
@property (nonatomic,strong) Node* root;
-(void)addNode:(int)value;
-(void)printList;
-(void)rotateLeftElementsOnList:(LinkedList*)list elements:(int)numElement;
@end
@implementation LinkedList
-(instancetype)init{
self = [super init];
if (self) {
self.tail = nil;
}
return self;
}
-(void)addNode:(int)value{
Node* newNode = [[Node alloc] init];
newNode.element = value;
newNode.previous = nil;
newNode.next = nil;
if (self.tail == nil) {
newNode.next = nil;
newNode.previous = nil;
self.tail = newNode;
self.root = newNode;
return;
}
self.tail.previous = newNode;
newNode.next = self.tail;
self.tail = newNode;
}
-(void)printList{
Node* header = self.root;
while(header.previous != nil){
NSLog(@"%d",header.element);
header = header.previous;
}
NSLog(@"%d",header.element);
}
类来管理linkedlist
@interface Node : NSObject
@property (nonatomic,assign) int element;
@property (nonatomic,strong) Node* next;
@property (nonatomic,strong) Node* previous;
@end
@interface LinkedList : NSObject
@property (nonatomic,strong) Node* tail;
@property (nonatomic,strong) Node* root;
-(void)addNode:(int)value;
-(void)printList;
-(void)rotateLeftElementsOnList:(LinkedList*)list elements:(int)numElement;
@end
@implementation LinkedList
-(instancetype)init{
self = [super init];
if (self) {
self.tail = nil;
}
return self;
}
-(void)addNode:(int)value{
Node* newNode = [[Node alloc] init];
newNode.element = value;
newNode.previous = nil;
newNode.next = nil;
if (self.tail == nil) {
newNode.next = nil;
newNode.previous = nil;
self.tail = newNode;
self.root = newNode;
return;
}
self.tail.previous = newNode;
newNode.next = self.tail;
self.tail = newNode;
}
-(void)printList{
Node* header = self.root;
while(header.previous != nil){
NSLog(@"%d",header.element);
header = header.previous;
}
NSLog(@"%d",header.element);
}
/////这是我旋转元素的功能,但我想知道是否有人知道更好的解决方案
-(void)rotateLeftElementsOnList:(LinkedList*)list elements:(int)numElement{
Node* header = self.root;
int index = 0;
while(index < numElement){
header = header.previous;
index++;
}
header.next.previous = nil;
header.next = nil;
self.root.next = self.tail;
self.tail.previous = self.root;
self.root = header;
}
-(void)rotateLeftElementsOnList:(LinkedList*)列表元素:(int)numElement{
节点*头=self.root;
int指数=0;
while(指数
我不知道objective-c,所以我无法为您提供代码。但这里有一个想法。您可以创建一个类,该类将具有两个属性,而不是移动数组:list/array
类型的a
,以及int
类型的shift
。之后,您将有方法get_元素(inti)
,该方法将使用shift
值获取i-th
元素。伪代码如下所示:
class ShiftedArray:
int[] a
int shift
void shift_array(int positions):
shift = positions
int get_element(int i):
return a[(n + i - shift % n) % n]
我不知道objective-c,所以我不能给你一个代码。但这里有一个想法。您可以创建一个类,该类将具有两个属性,而不是移动数组:
list/array
类型的a
,以及int
类型的shift
。之后,您将有方法get_元素(inti)
,该方法将使用shift
值获取i-th
元素。伪代码如下所示:
class ShiftedArray:
int[] a
int shift
void shift_array(int positions):
shift = positions
int get_element(int i):
return a[(n + i - shift % n) % n]
我不知道Objective-C,所以很遗憾,我不能向您提供代码片段;但我会向你解释逻辑:
计数
元素数李>
[1 2 3 4 5 6 7 8]
并且您希望移动,例如,3个位置:
[3 2 1 4 5 6 7 8]
[3 2 1 8 7 6 5 4]
[4 5 6 7 8 1 2 3]
时间复杂度为
O(n)
,因为您只需遍历元素一次即可反转它们,而空间复杂度为O(1)
,因为您没有使用任何额外的辅助存储空间。希望这是有帮助的 我不知道Objective-C,所以很遗憾,我无法向您提供代码片段;但我会向你解释逻辑:
计数
元素数李>
[1 2 3 4 5 6 7 8]
并且您希望移动,例如,3个位置:
[3 2 1 4 5 6 7 8]
[3 2 1 8 7 6 5 4]
[4 5 6 7 8 1 2 3]
时间复杂度为
O(n)
,因为您只需遍历元素一次即可反转它们,而空间复杂度为O(1)
,因为您没有使用任何额外的辅助存储空间。希望这是有帮助的 链表是数组的O(n)导数。这就是这个想法的基础。如果可以转换为列表,则转换为一个环,最后一个节点的下一个指针指向头部。只跟踪一个头部指针。这样,只需向前移动头指针即可完成移位。链表是数组的O(n)导数。这就是这个想法的基础。如果可以转换为列表,则转换为一个环,最后一个节点的下一个指针指向头部。只跟踪一个头部指针。这样,只需向前移动头部指针就可以完成移动。您是指O(n)时间和O(1)空间吗。在O(1)时间内移动普通数组中的所有元素是不可能的,“复杂度”可以指时间或空间。您是否可以在示例中包含函数的示例参数以及相应的输出?您可能指的是O(n)时间和O(1)空间。在O(1)时间内移动普通数组中的所有元素是不可能的,“复杂度”可以指时间或空间。您能否在示例中包含函数的示例参数以及相应的输出?