Java每日一练(20230331)

时间:2022-10-04 01:18:16

Java每日一练(20230331)

目录

1. 最长公共前缀  ????

2. 改写字符串  ????

3. 不同的二叉搜索树 II  ????????

???? 每日一练刷题专栏 ????

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 最长公共前缀

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

示例 1:

输入:strs = ["flower","flow","flight"]
输出:"fl"

示例 2:

输入:strs = ["dog","racecar","car"]
输出:""
解释:输入不存在公共前缀。

提示:

  • 0 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] 仅由小写英文字母组成

出处:

https://edu.csdn.net/practice/24313068

代码:

class longestCommonPrefix {
    public static class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs.length == 0) {
                return "";
            }
            int i = 0;
            StringBuilder lcp = new StringBuilder();
            while (true) {
                boolean done = false;
                if (i >= strs[0].length()) {
                    break;
                }
                for (int j = 0; j < strs.length; j++) {
                    if (i < strs[j].length()) {
                        if (strs[j].charAt(i) != strs[0].charAt(i)) {
                            done = true;
                            break;
                        }
                    } else {
                        done = true;
                        break;
                    }
                }
                if (done) {
                    break;
                } else {
                    lcp.append(strs[0].charAt(i));
                    i++;
                }
            }
            return lcp.toString();
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        String[] nums = {"flower","flow","flight"};
        System.out.println(s.longestCommonPrefix(nums));
   }
}

输出:

fl

其它写法:

public String longestCommonPrefix(String[] strs) {
    if (strs == null || strs.length == 0) {
        return "";
    }
    int minLen = Integer.MAX_VALUE;
    for (String str : strs) {
        minLen = Math.min(minLen, str.length());
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < minLen; i++) {
        char c = strs[0].charAt(i);
        for (int j = 1; j < strs.length; j++) {
            if (strs[j].charAt(i) != c) {
                return sb.toString();
            }
        }
        sb.append(c);
    }
    return sb.toString();
}


2. 改写字符串

键盘录入一个字符串,将字符串中的大写改成小写,小写改成大写,数字改成

例如heLLO123,输出后为HEllo**。

出处:

https://edu.csdn.net/practice/24313069

代码:

import java.util.Scanner;
public class Transfer {
    public static void main(String[] args) {
    String str = "";
    Scanner s = new Scanner(System.in);
    System.out.println("请输入您想输入的字符串:");
    str = s.next();
    StringBuffer sb = new StringBuffer();
    int i;
    for (i = 0; i <= str.length() - 1; i++) {
        char ch;
        if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
            ch = (char) (str.charAt(i) - 32); 
        } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
            ch = (char) (str.charAt(i) + 32); 
        } else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
            ch = '*'; 
        } else {
            ch = str.charAt(i); 
        }
        sb.append(ch); 
    }
    String trStr = sb.toString(); 
    System.out.println(sb.toString());
  }
}

输出:


3. 不同的二叉搜索树 II

给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1 到 n 互不相同的不同 二叉搜索树 。可以按 任意顺序 返回答案。

示例 1:

Java每日一练(20230331)

输入:n = 3
输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]

示例 2:

输入:n = 1
输出:[[1]]

提示:

  • 1 <= n <= 8

出处:

https://edu.csdn.net/practice/24313070

代码:

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) {
        val = x;
    }
}
class Solution {
    public List<TreeNode> generateTrees(int n) {
        if (n == 0)
            return new LinkedList<TreeNode>();
        return generate_trees(1, n);
    }
    private LinkedList<TreeNode> generate_trees(int start, int end) {
        LinkedList<TreeNode> all_trees = new LinkedList<TreeNode>();
        if (start > end) {
            all_trees.add(null);
            return all_trees;
        }
        for (int i = start; i <= end; i++) {
            LinkedList<TreeNode> left_trees = generate_trees(start, i - 1);
            LinkedList<TreeNode> right_trees = generate_trees(i + 1, end);
            for (TreeNode l : left_trees)
                for (TreeNode r : right_trees) {
                    TreeNode current_tree = new TreeNode(i);
                    current_tree.left = l;
                    current_tree.right = r;
                    all_trees.add(current_tree);
                }
        }
        return all_trees;
    }
}

输出:


???? 每日一练刷题专栏 ????

持续,努力奋斗做强刷题搬运工!

???? 点赞,你的认可是我坚持的动力! 

???? 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

Java每日一练(20230331)

Golang每日一练 专栏

Java每日一练(20230331)

Python每日一练 专栏

Java每日一练(20230331)

C/C++每日一练 专栏

Java每日一练(20230331)

Java每日一练 专栏