其他分享
首页 > 其他分享> > # 356. 直线镜像

# 356. 直线镜像

作者:互联网

bool isReflected(int** points, int pointsSize, int* pointsColSize){
    int i, j;
    for(i = 0; i<pointsSize-1; i++)
    {
        for(j = i+1; j<pointsSize; j++)
        {
            if(points[j][0] < points[i][0])
            {
                int *tmp = points[i];
                points[i] = points[j];
                points[j] = tmp;
            }
        }
    }
    float line  = (points[0][0] + points[pointsSize-1][0]) / 2.;
    i = 0, j = pointsSize-1; 
    while(i < pointsSize && points[i][0] < line) i++;
    while(j >= 0 && points[j][0] > line) j--;
    if(i == 0 && j == pointsSize-1) return true;
    i--, j++;
    int left = i, right = j;
    bool *flag = (bool *)malloc(sizeof(bool) * pointsSize);
    memset(flag, false, sizeof(bool) * pointsSize);
    while(left >= 0)
    {
        right = j;
        while(right < pointsSize)
        {
            if((points[left][0] + points[right][0] == 2*line) && points[left][1] == points[right][1])
            {
                flag[left] = true;
                flag[right] = true;
                break; 
            }
            right++;
        }
        if(right == pointsSize) return false;
        left--;
    }
    right = j;
    while(right < pointsSize)
    {
        if(flag[right])
        {
            right++;
            continue;
        }
        left = i;
        while(left >= 0)
        {
            if((points[left][0] + points[right][0] == 2*line) && points[right][1] == points[left][1]) break;
            left--;
        }
        if(left < 0) return false;
        right++;
    }
    return true;
}

标签:直线,right,int,356,points,bool,pointsSize,镜像,left
来源: https://blog.csdn.net/Haku_yyf/article/details/120578383