小编Evg*_*eny的帖子

在Swift中旋转数组

在Swift中探索算法时,如果不使用funcs shiftLeft/ ,则无法在swift中找到用于数组旋转的算法shiftRight.

C有这个优雅的算法,时间复杂度为O(N):

/* Function to left rotate arr[] of size n by d */
void leftRotate(int arr[], int d, int n)
{
    rvereseArray(arr, 0, d-1);
    rvereseArray(arr, d, n-1);
    rvereseArray(arr, 0, n-1);
}

/*Function to reverse arr[] from index start to end*/
void rvereseArray(int arr[], int start, int end)
{
    int temp;
    while (start < end)
    {
        temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在努力将其转换为swift:

func rotate(array:[Int], positions:Int, arSize:Int) { …
Run Code Online (Sandbox Code Playgroud)

algorithm swift

4
推荐指数
3
解决办法
2886
查看次数

打印带有缩进的树。迅速

使用 swift 实现树数据结构:

class Node {

    var value: String
    var children: [Node] = []
    weak var parent: Node?

    init(_ value: String) {
        self.value = value
    }

    func add(_ child: Node){
        children.append(child)
        child.parent = self
    }

    func printTree() {
        var text = self.value
        if !self.children.isEmpty {
            text += "\n  " + self.children.map{$0.printTree()}.joined(separator: ", ")
        }
        print(text)
    }

}
Run Code Online (Sandbox Code Playgroud)

我的目标是看到这样的东西:

A1
    B2
    C3
        G6
    K0
        H7
            L8
        L9
Run Code Online (Sandbox Code Playgroud)

我知道应该有一些聪明的方法来插入缩进,但我也为“地图”而苦苦挣扎。编译器给了我“对成员‘map’的模糊引用”。

tree data-structures swift

4
推荐指数
1
解决办法
2444
查看次数

点击手势不起作用。启用用户交互。

具有以下视图结构:

在此处输入图片说明

以编程方式将点击手势 rec 添加到 Temp lbl:

    let tempLblTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainFeedVC.convertDegrees))
    tempLblTap.delegate = self
    tempLblTap.numberOfTapsRequired = 1
    tempLblTap.numberOfTouchesRequired = 1
    tempLblTap.cancelsTouchesInView = false
    self.tempLbl.isUserInteractionEnabled = true
    self.tempLbl.addGestureRecognizer(tempLblTap)
Run Code Online (Sandbox Code Playgroud)

但该方法convertDegrees未触发。

还有 2 个滑动手势识别器添加到同一视图中:

let leftSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showPostPicVC))
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(leftSwipeGestureRecognizer)

    let rightSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showUserVC))
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(rightSwipeGestureRecognizer)
Run Code Online (Sandbox Code Playgroud)

也许他们是原因?

uigesturerecognizer swift

1
推荐指数
1
解决办法
980
查看次数