美文网首页
289. 生命游戏/539. 最小时间差

289. 生命游戏/539. 最小时间差

作者: Kevifunau | 来源:发表于2020-03-20 18:02 被阅读0次

    289. 生命游戏

    • 相关标签 : 数组
    
    
    /*
    1. 照着规则写   +  新开辟一个数组 
     (i -1, j-1)   (i-1,j)    (i-1, j + 1)
     (i, j-1)      (i,j)      (i, j + 1)
     (i +1, j-1)   (i + 1,j)  (i + 1, j + 1)
     
    规则: 死 和 活 拉齐 
    
    活  : lives_nb [2,3]   -》 lives [3, 4]  & cell == 1 
    死 : lives_nb [3]  -> lives[3] 活 
    合并 : lives == 3  || lives == 4 && cell == 1
    
    
    inplace ?? 
    只有 0 和 1  可以吧 变化值 存在 第二个比特位 
    
    0x1
    
    0x2
    
    
    */
    
    #define FMASK(x) (x & 0x1)
    
    
    int helper(int** board, int boardSize, int* boardColSize, int i, int j)
    {
        int lives = 0;
        int cell = board[i][j];
        
        if (i > 0) {
            if (j > 0) {
                lives += FMASK(board[i-1][j-1]);
            }
            lives += FMASK(board[i-1][j]);
            if (j < *boardColSize - 1) {
                lives += FMASK(board[i-1][j+1]);
            }
        }
        if (j > 0) {
            lives += FMASK(board[i][j-1]);
        }
        lives += FMASK(board[i][j]);
        if (j < *boardColSize - 1) {
            lives += FMASK(board[i][j+1]);
        }
        if (i < boardSize - 1) {
            if (j > 0) {
                lives += FMASK(board[i+1][j-1]);
            }
            lives += FMASK(board[i+1][j]);
            if (j < *boardColSize - 1) {
                lives += FMASK(board[i+1][j+1]);
            }
        }
        
        if (lives == 3 || (lives == 4 && cell == 1)) {
            return 1;
        }
        return 0;
    }
    
    void gameOfLife(int** board, int boardSize, int* boardColSize){
        if (board ==NULL || boardSize == 0 || boardColSize == NULL || *boardColSize == 0) {
            return;
        }
        
        int live = 0;
        
        for (int i = 0; i < boardSize; i++) {
            for (int j = 0; j < *boardColSize; j++) {
                live = helper(board, boardSize, boardColSize, i, j);
                // printf("%d ", live);
                board[i][j] += (live << 1);
            }
            // printf("\n");
        }
        
        for (int i = 0; i < boardSize; i++) {
            for (int j = 0; j < *boardColSize; j++) {
                board[i][j] >>= 1;
            }
        }
    
    }
    

    539. 最小时间差

    • 相关标签: 字符串
    
    
    
    /*
    
    qsort 可以自定义排序 n logn 
    
    O(n) 遍历一遍 找最小 
    
    ["23:59","00:04", "01:59", "05:30"]
    
    00:04
    01:59
    05:30
    23:59
    
    1. 头 和 尾巴 要单独算  h2, m2 -> 23.60   : (60 - m2) + 60 * (23 - h2)
                          00,00 -> h1, m1  :  (m1 - 0)  +  60 * (h1 - 0) 
    
    (h1 == 59 && h2 == 00 && (m2 - m1 == 1) {
        res = 1;
    } else  res = (m2 - m1) + 60 * (h2 - h1)
    
    */
    
    typedef struct {
        int h1;
        int h2;
        int m1;
        int m2;
    } Time;
    
    void parse(Time *t, char *p1, char *p2)
    {
        char temp[3] = {0}; // 一定要 有 '\0' 如果 不补的话 atoi 就会扫到 非法区域 在 leetcode 上直接报错
        strncpy(temp,p1, sizeof(temp));
        t->h1 = atoi(temp);
        
        strncpy(temp,p2, sizeof(temp));
        t->h2 = atoi(temp);
        
        strncpy(temp,p1 + 3, sizeof(temp));
        t->m1 = atoi(temp);
        
        strncpy(temp,p2 + 3, sizeof(temp));
        t->m2 = atoi(temp);
    }
    
    int mycmp(const void *a, const void *b)
    {
        Time t = {0};
        
        const char *p1 = *(const char **)a;
        const char *p2 = *(const char **)b;
        parse(&t, p1, p2);
        return (t.h1 == t.h2) ? t.m1 - t.m2 : t.h1 - t.h2;
          
    }
    
    
    int findMinDifference(char ** timePoints, int timePointsSize){
        
        qsort(timePoints, timePointsSize, sizeof(timePoints[0]), mycmp);
        
        // for (int i = 0; i < timePointsSize; i++) {
        //     printf("%s\n", timePoints[i]);
        // }
        
    
        int min = 0xffff;
        Time t = {0};
        int tt = 0;
        
        parse(&t, timePoints[0], timePoints[timePointsSize -1]);
        min = ((60 - t.m2) + 60 * (23 - t.h2) + (t.m1 - 0)  +  60 * (t.h1 - 0) );
        
        for (int i = 1 ; i < timePointsSize; i++) {
            parse(&t, timePoints[i - 1], timePoints[i]);
            if (t.h1 == 59 && t.h2 == 0 && t.m2 - t.m1 == 1) {
                min = (min ==0) ? 0 : 1;
            } else {
                tt = (t.m2 - t.m1) + 60 * (t.h2 - t.h1);
                min = (tt < min) ? tt : min;
            }
        }
        
        return min;
    }
    

    相关文章

      网友评论

          本文标题:289. 生命游戏/539. 最小时间差

          本文链接:https://www.haomeiwen.com/subject/muqsyhtx.html