06、字节跳动-拓展练习

1、x 的平方根

class Solution {
    /**
     * @param x: An integer
     * @return: The sqrt of x
     */
    public int sqrt(int x) {
        // write your code here
        if (x < 0) 
        {
           return 0; 
        }
    	double err = 1e-15;    
    	double t = x;    
    	while (Math.abs(t - x/t) > err * t)        
    		t = (x/t + t) / 2.0;   
    	return (int)t; 
    }
}

原文:https://blog.csdn.net/a1123612483/article/details/76229224

 

2、UTF-8 编码验证

 

	public int judge(int n) {
		if ((128 & n) == 0)
			return -1;
		if ((192 & n) == 128)//10
			return 0;
		if ((224 & n) == 192)
			return 1;
		if ((240 & n) == 224)
			return 2;
		if ((248 & n) == 240)
			return 3;
		return -2;
	}
 
	public boolean validUtf8(int[] data) {
		boolean isUtf8 = false;// 当前num是否在一个验证的字符中
		int times = 0;
		for (int i = 0; i < data.length; i++) {
			if(isUtf8==false){//当前数不在某字符编码内
				times = judge(data[i]);//当前编码需要多少个10
				if (times == -2 || times == 0)//非编码
					return false;
				if(times>0)//后面需要跟多少个10编码
					isUtf8=true;
			}else{
				if(times>0&&judge(data[i])==0)//是10编码
					times--;
				else
					return false;
				if(times==0)
					isUtf8=false;
			}
		}
		return times>0?false:true;
	}

参考:https://blog.csdn.net/yeqiuzs/article/details/52462554

 

3、第二高的薪水

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN

set N = N - 1;
  
  RETURN (
      # Write your MySQL query statement below.
      select 
        (select distinct
            Salary
         from
            Employee
         order by Salary desc
         limit 1 offset N)
  );
END

链接:https://www.jianshu.com/p/fa18ca54225a

解答2:

SELECT
    (
        SELECT
            salary
        FROM
            employee
        ORDER BY
            salary
        LIMIT 1,
        1
    ) AS SecondHighestSalary;

链接:https://www.jianshu.com/p/66ddae52d788

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值