题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

程序分析:可填在百位、十位、个位的数字都是1、2、3、4。这里要用3个for循环

public class test {
    public static void main(String[] args) {

        int sum=0;
        for (int bite = 1; bite < 5;bite++) {
            for (int ten = 1; ten < 5; ten++) {
                for (int hundred = 1; hundred < 5; hundred++) {
                    if (bite != ten && bite != hundred && ten != hundred) {//符合条件的数字
                        System.out.print((hundred*100+ten*10+bite)+"  ");
                        sum++;//计算个数
                        if (sum % 10 == 0) {//十个一行
                            System.out.println();
                        }
                    }
                }
            }

        }
        System.out.println("\n总共有:"+sum+"个这样的数");
    }
}

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

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

 

示例 1:

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

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

提示:

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

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0)
            return "";
        String ans = strs[0];
        for(int i =1;i<strs.length;i++) {
            int j=0;
            for(;j<ans.length() && j < strs[i].length();j++) {
                if(ans.charAt(j) != strs[i].charAt(j))
                    break;
            }
            ans = ans.substring(0, j);
            if(ans.equals(""))
                return ans;
        }
        return ans;
    }
}
Last modification:April 13th, 2021 at 03:27 pm
如果觉得我的文章对你有用,请随意赞赏