d2025329

发布于:2025-03-30 ⋅ 阅读:(51) ⋅ 点赞:(0)

目录

一、修复表中名字

二、患某种疾病的患者

三、最长连续子序列

四、二叉树的层序遍历

一、修复表中名字

1667. 修复表中的名字 - 力扣(LeetCode)

  1. concat(A,B),将字符串A和B拼接
  2. left(str,len),从字符串左边开始截取len个字符
  3. right(str,len),从字符串右边开始截取len个字符
  4. upper(str),将字符串转成大写
  5. lower(str),将字符串转出小写

select user_id,
concat(upper(left(name,1)),lower(right(name,char_length(name)-1))) as 'name'
from Users
order by user_id asc;

二、患某种疾病的患者

两张情况,第一张情况是以‘DIAB1’开头,第二种情况是以空格+‘DIAB1’开头

select patient_id,patient_name,conditions
from Patients where conditions like 'DIAB1%' or conditions like '% DIAB1%'

三、最长连续子序列

先排序,然后有三种情况 == 1,count++, == 0,count不需要加,其他情况直接break,

public int longestConsecutive(int[] nums) {
        Arrays.sort(nums);
        int i = 0;
        int ret = 0;
        while(i < nums.length){
            int count = 1;
            while(i+1 < nums.length){
                if(nums[i+1] - nums[i] == 1){
                    count++;
                    i++;
                } else if(nums[i+1] - nums[i] == 0){
                    i++;
                } else {
                    break;
                }
            }
            ret = Math.max(ret,count);
            i++;
        }
        return ret;
    }

四、二叉树的层序遍历

每次记录一下每层有几个节点就行

public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> st = new LinkedList<>();
        st.offer(root);
        List<List<Integer>> ret = new ArrayList<>();
        if(root == null) return ret;
        while(st.size() != 0){
            int sz = st.size();
            List<Integer> temp1 = new ArrayList<>();
            while(sz-- != 0){
                TreeNode temp = st.poll();
                temp1.add(temp.val);
                if(temp.left != null){
                    st.offer(temp.left);
                }
                if(temp.right != null){
                    st.offer(temp.right);
                }
            }
            ret.add(temp1);
        }
        return ret;
    }