leetcode连接
https://leetcode.cn/problems/validate-ip-address/description/
题目描述

解题思路
如果一个字符串是ipv4 则必须包含 . , 如果一个字符串是ipv6则必须包含 :, 因此可以将题目转换为判断是一个字符串是ipv4,以及判断一个字符串是ipv6两个部分。
判断一个字符串是否是16进制可以使用如下方法进行判断,如果err为nil则标识这个字符串是一个正确的16进制,否则不是16进制
_,err := strconv.ParseInt(ip,16,64)
判断一个字符串是否是十进制可以使用如下方式判断,如果err为nil则标识这个字符串是一个正确的十进制。
i,err := strconv.Atoi(ip)
题目描述
买下所有产品的客户
func validIPAddress(queryIP string) string {if strings.Index(queryIP,".") >0 {// 验证是否是ipv4if isIpv4(queryIP){return "IPv4"}return "Neither"}if strings.Index(queryIP,":") > 0 {// 验证是否是ipv6if isIpv6(queryIP){return "IPv6"}return "Neither"}return "Neither"}func isIpv6(queryIP string) bool{var arr []string = strings.Split(queryIP,":")if len(arr)!=8 {return false}for i:=0;i<len(arr);i++ {if !ipv6Helper(arr[i]){return false}}return true}func ipv6Helper(ip string) bool{if len(ip)==0 || len(ip)>4{return false}// 将16进制的ip转为int64看是否可以转 ,如果不能转,则返回err!=nil_,err := strconv.ParseInt(ip,16,64)return err == nil}func isIpv4(queryIP string) bool{var arr []string = strings.Split(queryIP,".")if len(arr)!=4 {return false}for i:=0;i<len(arr);i++ {if !ipv4Helper(arr[i]){return false}}return true}func ipv4Helper(ip string) bool{if len(ip)==0 || len(ip)>3 {return false}if ip[0]=='0' && len(ip)!=1{return false}// 将ip转为十进制的inti,err := strconv.Atoi(ip)if err != nil {return false}return i>=0 && i<=255}

解题思路
通过left join + where is not null 过滤出在product表中的数据
通过group by + count(distinct(xxx)) 来过滤出所有商品
select c.customer_idfrom customer c left join product p on c.product_key = p.product_keywhere p.product_key is not null # 排除用户购买了一些不在product表中的商品group by c.customer_id# 同一个用户可能买了一个商品多次,因此通过distinct去重having count(distinct(p.product_key)) = (# 所有产品的数量select count(*)from product)
题目描述
合作过至少三次的演员和导演
解题思路
按照演员id和导演id进行分组,多字段分组必须要所有字段一致才会分到同一组
select actor_id,director_idfrom actordirectorgroup by actor_id,director_idhaving count(*) >=3
题目描述
体育馆的人流量

解题思路
需要连续自增的三个id的时候,可以通过id = id-1,id=id-2进行leftjoin
union 具有去重功能, union all 不会进行去重
当我们查询的时候需要一些中间表的时候,这个中间表必须要起一个别名
select *from(select s1.*from stadium s1 left join stadium s2 on s1.id = s2.id-1left join stadium s3 on s1.id = s3.id-2 # 这个from语句体现了连续的idwhere s2.id is not null and s3.id is not null and s1.people>=100 and s2.people>=100 and s3.people>=100unionselect s2.*from stadium s1 left join stadium s2 on s1.id = s2.id-1left join stadium s3 on s1.id = s3.id-2where s2.id is not null and s3.id is not null and s1.people>=100 and s2.people>=100 and s3.people>=100unionselect s3.*from stadium s1 left join stadium s2 on s1.id = s2.id-1left join stadium s3 on s1.id = s3.id-2where s2.id is not null and s3.id is not null and s1.people>=100 and s2.people>=100 and s3.people>=100) tmp order by id
文章转载自梁霖编程工具库,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




