V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nagato
V2EX  ›  问与答

考道前端 iOS 算法题( Easy)

  •  1
     
  •   nagato · 2016-11-27 14:02:39 +08:00 · 2520 次点击
    这是一个创建于 2699 天前的主题,其中的信息可能已经有所发展或是发生改变。

    给你两个 UIView 或者其子类的对象 viewAviewB.

    如果它们同属一个父view的话, 请找到并返回这个父 view, 否则请返回nil.

    - (UIView *) findCommonSuperViewBetween: (UIView *)viewA
                                        and: (UIView *)viewB 
    {
    
    }
    

    大家先写,一会儿贴下我写的答案。

    第 1 条附言  ·  2016-11-27 18:04:59 +08:00
    要求返回的是最低的父 view
    第 2 条附言  ·  2016-11-27 18:11:09 +08:00

    贴下我的代码:

    - (UIView *) findCommonSuperViewBetween: (UIView *)viewA
                                        and: (UIView *)viewB
    {
        if (viewA == nil || viewB == nil) return nil;
        UIView *tempA = viewA;
        UIView *tempB = viewB;
        while (tempA != tempB) {
            tempA = tempA == nil? viewB: [tempA superview];
            tempB = tempB == nil? viewA: [tempB superview];
        }
        return tempA;
    }
    
    21 条回复    2016-12-01 02:25:38 +08:00
    xieguobihaha
        1
    xieguobihaha  
       2016-11-27 17:15:32 +08:00
    我的思路是将 viewA 和 viewB 的所有父 view 分别放入两个数组,数组数量少的那个说明位于视图上层,取它的第一个父视图即可,代码如下
    https://gist.github.com/hiXgb/6cf33f97fa2439a44d23be3e4691b16e
    starqoq
        2
    starqoq  
       2016-11-27 17:29:19 +08:00 via Android
    求 lca ?
    xieguobihaha
        3
    xieguobihaha  
       2016-11-27 17:30:18 +08:00
    nagato
        4
    nagato  
    OP
       2016-11-27 17:31:17 +08:00
    @starqoq 哈哈哈,比 LCA 还要简单好多
    nagato
        5
    nagato  
    OP
       2016-11-27 17:35:49 +08:00
    @xieguobihaha 说实话有点惨不忍睹,面试应该是挂的
    xieguobihaha
        6
    xieguobihaha  
       2016-11-27 17:39:43 +08:00
    @nagato 求指教
    kimown
        7
    kimown  
       2016-11-27 17:41:30 +08:00 via Android
    题目真没看懂, uiview ,这是前端?
    xieguobihaha
        8
    xieguobihaha  
       2016-11-27 17:51:47 +08:00
    @nagato 我自己发现一个错误了, while 循环里最后跳出条件得改一下,不然死循环了。
    nagato
        9
    nagato  
    OP
       2016-11-27 17:57:52 +08:00
    @xieguobihaha
    且不说你做的是不是正确。单从你的代码就能看出你的编码能力还挺一般的😂. 比如你想把所有的 superview 全加数组里,为啥可以写出这么多代码和变量,不是两个 while loop 就好了吗
    不过我挺喜欢你的态度👍
    nagato
        10
    nagato  
    OP
       2016-11-27 18:00:30 +08:00
    @kimown iOS

    其实换个讲法。 给 classA, classB ,求 lowest common super class , 应该对哪个语言都适用
    wy315700
        11
    wy315700  
       2016-11-27 18:04:47 +08:00
    @nagato 其实就是 求两个单链表是否相交的问题吧。。。。
    binux
        12
    binux  
       2016-11-27 18:06:45 +08:00 via Android
    多重继承怎么办
    nagato
        13
    nagato  
    OP
       2016-11-27 18:07:12 +08:00
    @wy315700 哈哈对的,想到这个就差不多了
    nagato
        14
    nagato  
    OP
       2016-11-27 18:09:37 +08:00
    @binux 你厉害
    fengyunSmlie
        15
    fengyunSmlie  
       2016-11-27 18:14:51 +08:00
    菜鸟前来膜拜大佬
    a412739861
        16
    a412739861  
       2016-11-27 18:14:52 +08:00
    贴个现成的吧。源代码在 Masonry 中,查看 View+MASAdditions.h/m 文件即可。

    这是个实例方法,类方法也一样;只是 self 变成 view1 罢了。

    遍历 self 的 superview ,去匹配 view 。
    然后取 view 得 superview ,再遍历一遍 self 的 superview 。循环不到,也就返回 nil 了。
    记得回复不能 markdown 的样子,就直接复制下代码,不会太好看。

    - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view {
    MAS_VIEW *closestCommonSuperview = nil;

    MAS_VIEW *secondViewSuperview = view;
    while (!closestCommonSuperview && secondViewSuperview) {
    MAS_VIEW *firstViewSuperview = self;
    while (!closestCommonSuperview && firstViewSuperview) {
    if (secondViewSuperview == firstViewSuperview) {
    closestCommonSuperview = secondViewSuperview;
    }
    firstViewSuperview = firstViewSuperview.superview;
    }
    secondViewSuperview = secondViewSuperview.superview;
    }
    return closestCommonSuperview;
    }
    xieguobihaha
        17
    xieguobihaha  
       2016-11-27 18:38:50 +08:00
    @nagato ![]( ) 我用你的方法代入上面那张图貌似有问题,假设 A 是 2 , B 是 9 ,你试一下。
    nagato
        18
    nagato  
    OP
       2016-11-27 18:44:47 +08:00 via iPad
    @xieguobihaha 你再仔细看看
    binux
        19
    binux  
       2016-11-27 18:55:10 +08:00   ❤️ 1
    @nagato 假如 A 是 3 , B 是 6
    itqls
        20
    itqls  
       2016-11-27 22:39:11 +08:00
    @binux hhh 打脸~
    Biscuits
        21
    Biscuits  
       2016-12-01 02:25:38 +08:00
    可以 hitTest 试试吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5420 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 01:35 · PVG 09:35 · LAX 18:35 · JFK 21:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.