一、每日一题
387. 字符串中的第一个唯一字符 - 力扣(LeetCode) (leetcode-cn.com)
思路:双层for循环遍历,要注意的是两层循环遍历到相同位置时要进行跳过,如果遇到不同的元素,就记录遍历到最后元素不同的个数,如果长度等于字符串的长度,且标记为0,则直接返回这个时候外层循环的索引,如果全部遍历完都没有找到,就直接返回-1。
代码:
class Solution {
public:
int firstUniqChar(string s) {
for(int i=0;i<s.length();i++)
{
int count=0,flag=0;
for(int j=0;j<s.length();j++)
{
if(s[i]==s[j]&&i!=j)
{
flag=1;
break;
}
else
{
count++;
}
}
if(count==s.length()&&flag==0)
{
return i;
}
}
return -1;
}
};
二、Java学习
异常
异常处理之try...catch...
try{
可能出现异常的代码;
}catch(异常类名 变量名){
异常的处理代码
}
执行流程
程序从try里面的代码开始执行,出现异常,会自动生成一个异常类对象,然后异常对象被交给Java运行时系统。当Java运行时系统接收到异常类对象时,会去catch中找匹配的异常类,找到后进行异常的处理。然后程序就能继续往下执行。
public class ExceptionDemo01 {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try{
int[] arr={1,2,3};
System.out.println(arr[3]);
}catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
Throwable的成员方法
getMessage方法
import java.lang.reflect.Method;
public class ExceptionDemo01 {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try{
int[] arr={1,2,3};
System.out.println(arr[3]);
}catch(ArrayIndexOutOfBoundsException e)
{
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
toString 方法
import java.lang.reflect.Method;
public class ExceptionDemo01 {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try{
int[] arr={1,2,3};
System.out.println(arr[3]);
}catch(ArrayIndexOutOfBoundsException e)
{
//e.printStackTrace();
//System.out.println(e.getMessage());
System.out.println(e.toString());
}
}
}
printStackTrace方法,输出的信息最全,代码在上面。
编译时异常和运行异常
所以的RuntimeException类及其子类被称为运行时异常,其余都被称为编译时异常。
编译时异常必须显示处理,否则程序报红,就会无法通过编译,利用上面那些方法。运行时异常无需显示处理,也可以和编译时异常一样利用上面那些方法进行处理。
异常处理之throws
有些异常我们是没有权限去进行处理,这时需要用到throws处理方案。
格式:(跟在方法的括号后面)
throws 异常类名
这是运行时异常使用throws方法:
public class ExceptionDemo01 {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() throws ArrayIndexOutOfBoundsException{
int[] arr={1,2,3};
System.out.println(arr[3]);
}
}
从运行结果可以看到,程序并没有完全运行,因此throws方法只是将错误抛出 ,如果要将程序完成运行的话,还得用到try...catch...方法,在调用该方法的地方。
自定义异常
格式:
public class 异常类名 extends Exception{
无参构造
带参构造
}
实践:
package itheima_04;
public class ScoreException extends Exception{
public ScoreException(){}
public ScoreException(String message)
{
super(message);
}
}
package itheima_04;
public class Teacher {
public void checkScore(int score) throws ScoreException {
if(score<0||score>100)
{
//throw new ScoreException();
throw new ScoreException("分数不符合常规");
}
else
{
System.out.println("分数正常");
}
}
}
package itheima_04;
import java.util.Scanner;
public class TeacherTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int score=sc.nextInt();
Teacher t=new Teacher();
try {
t.checkScore(score);
} catch (ScoreException e) {
e.printStackTrace();
}
}
}
throw和throws的区别
throw用在方法体内,跟的是异常对象名。throws 用在方法声明后面,跟的是异常类名。
throw表示抛出异常,由方法体内的语句进行处理。throws表示抛出异常,由该方法调用者进行处理。
执行throw是一定发生了异常。throws表示出现异常的一种可能性,但并不一定会出现异常。
三、SQL学习
计算用户8月每天的练题数量_牛客题霸_牛客网 (nowcoder.com)
SELECT day(date) AS day,COUNT(*) AS question_cnt
FROM question_practice_detail
WHERE year(date)=2021 AND month(date)=08
GROUP BY day
查找山东大学或者性别为男生的信息_牛客题霸_牛客网 (nowcoder.com)
SELECT device_id,gender,age,gpa
FROM user_profile
WHERE university="山东大学"
UNION ALL
SELECT device_id,gender,age,gpa
FROM user_profile
WHERE gender="male"
21年8月份练题总数_牛客题霸_牛客网 (nowcoder.com)
SELECT
COUNT(DISTINCT device_id) AS did_cnt,
COUNT(question_id) AS question_cnt
FROM question_practice_detail
WHERE year(date)=2021 AND month(date)=08
提取博客URL中的用户名_牛客题霸_牛客网 (nowcoder.com)
SELECT device_id,SUBSTRING(blog_url,11) as user_name
from user_submit