LeetCode第六周周赛

404. Sum of Left Leaves

不解释

func isLeaf(root * TreeNode) bool{
    if root == nil || root.Left!=nil || root.Right!=nil{
        return false
    }
    return true;
}
func sumOfLeftLeaves(root *TreeNode) int {
    res := 0
    if root != nil{
        if isLeaf(root.Left){
            res += root.Left.Val
        }else{
            res += sumOfLeftLeaves(root.Left)
        }
        res += sumOfLeftLeaves(root.Right)
    }
    return res
}

405. Convert a Number to Hexadecimal

常规的10进制转16进制。
负数的转换需要先转为补码再计算。注意用long long int 防止溢出。

class Solution {
public:
    string toHex(int num) {
        string a[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
        string res;
        if (num == 0){
            return "0";
        }
        long long int t;
        if (num <0){
            t = unsigned(num);
        }
        else{
            t = num;
        }
        while (t>0){
            res = a[t % 16] + res;
            t /= 16;
        }
        return res;
    }
};

406. Queue Reconstruction by Height

o(n*n)的复杂度。
1.将人排序,以前置比他高的人数大小排列,相等的时候按照高度从低到高。
2.取排列完第一位,将其取出队列。然后更新下排列中的人情况。即将比他矮的人中,front的人数-1。
3.重复操作1,2直到队列中的人都被取出。
4.还原队列中的front的情况。因为在第二步中,front的情况会被改写,所以需要恢复。

type P [][]int 
func(p P) Len()int{
    return len(p)
}
func(p P)Swap(i,j int){
    p[i][0],p[i][1],p[j][0],p[j][1] = p[j][0],p[j][1],p[i][0],p[i][1]
}
func (p P)Less(i,j int)bool{
    if p[i][1]!=p[j][1]{
        return p[i][1] < p[j][1]
    }
    return p[i][0] < p[j][0]
}
//1.sort
//2.取当前队列
func reconstructQueue(people [][]int) [][]int {
    res := make([][]int,len(people))
    for i:=0;i<len(res);i++{
        res[i]  = make([]int,2)
    }
    n := len(people)
    for  i:=0;i<n;i++{

        sort.Sort(P(people))
        res[i][0],res[i][1] = people[0][0],people[0][1]
        for j:=1;j<len(people);j++{
            if people[0][0] >=people[j][0]{
                people[j][1] --;
            }
        }
        people = people[1:]
    }
    for i :=1;i<len(res);i++ {
        count :=0
        for j :=0;j<i;j++ {
            if res[i][0] <= res[j][0]{
                count ++;
            }
        }
        res[i][1] = count
    }
    return res;
}

407. Trapping Rain Water II

下面的做法超时了。正解网上找了一个。1

func dfs(heightMap [][]int, x,y int){
    if x < 0 || x >= len(heightMap) || y <0 || y >= len(heightMap[0]){
        return
    }
    if heightMap[x][y] != 0{
        return
    }
    heightMap[x][y] = 65535
    dfs(heightMap,x+1,y)
    dfs(heightMap,x-1,y)
    dfs(heightMap,x,y+1)
    dfs(heightMap,x,y-1)
}

func getArea(heightMap [][]int) int{
    for i:=0;i<len(heightMap[0]);i++{
        dfs(heightMap,0,i)
        dfs(heightMap,len(heightMap)-1,i)
    }
    for i:=0;i<len(heightMap);i++{
        dfs(heightMap,i,0)
        dfs(heightMap,i,len(heightMap[0])-1)
    }
    count :=0
    for i :=0;i<len(heightMap);i++{
        for j:=0;j<len(heightMap[0]);j++{
            if heightMap[i][j] == 0{
                count ++
            }else if heightMap[i][j] ==65535{
                heightMap[i][j] = 0;
            }
        }
    }
    return count
}
/*
func getMin(heightMap [][]int) int{
    min := 65535
    m,n := len(heightMap),len(heightMap[0])
    for i:=1;i<m-1;i++{
        for j:=1;j<n-1;j++{
            if heightMap[i][j] == 0{
                continue;
            }
            if min > heightMap[i][j]{
                min = heightMap[i][j]
            }
        }
    }
    return min
}*/

func getMin2(heightMap [][]int) int{
    min := 65535
    m,n := len(heightMap),len(heightMap[0])
    for i:=0;i<m;i++{
        for j:=0;j<n;j++{
            if heightMap[i][j] == 0{
                continue;
            }
            if min > heightMap[i][j]{
                min = heightMap[i][j]
            }
        }
    }
    return min
}

func trapRainWater(heightMap [][]int) int {
    res ,tmp:= 0,1
    if len(heightMap) == 0 || len(heightMap[0]) == 0{
        return 0
    }

    for true{
        m,n := len(heightMap),len(heightMap[0])
        min := getMin2(heightMap)
        for i:=0;i<m;i++{
            for j:=0;j<n;j++{
                if heightMap[i][j] < min{
                    heightMap[i][j] = 0
                    continue;
                }
                heightMap[i][j] -= min
            }
        }
        min = getMin2(heightMap)
        if min == 65535{
            break
        }
        tmp  = min* getArea(heightMap)
        res += tmp
    }
    return res
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值