LeetCode第六周周赛
来源:互联网 发布:淘宝二手苹果能买吗 编辑:程序博客网 时间:2024/06/05 12:50
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}
0 0
- LeetCode第六周周赛
- Leetcode第四周周赛
- Leetcode第五周周赛
- 实验室第六周周工作总结
- 第六周周总结
- 第六周周记
- 第六周周总结
- 第六周周总结
- LeetCode Weekly Contest 8 第八周周赛
- LeetCode Weekly Contest 9 第九周周赛
- Leetcode第十周周赛Smarking Algorithm Contest
- AYIT2016省赛集训第六周周一赛 H -高桥和低桥
- AYIT2016省赛集训第六周周一赛 A近似回文词(字符串)
- 第六周周赛 lca 强连通 最小生成树 最短路
- AYIT606第六周周六赛 (多重背包) E - Cash Machine 自动取款机
- 第六周周工作总结及计划表
- 第一周周赛
- SDUT 第九周周赛
- 什么是RAID?RAID有什么用?RAID原理
- 数据库--MySQL体系结构
- 回调函数和发消息的作用比较
- 16-任务门实验
- 数据库--使用索引的注意事项及常见场景
- LeetCode第六周周赛
- gradle 编译打包并使用 aar
- java多线程之线程间通信
- Java的NIO及与IO区别
- 数据库--SQL索引基础
- 接口和抽象类的区别
- 引用strings.xml的文字
- 什么是机器学习
- Android中SQLite应用详解