最大相同子串

不知道为什么测试用例idea能通过,leecode通过不了,烦

class Solution {

    public String longestCommonPrefix(String[] strs) {

        if(strs.length==0||strs[0].toString()=="") {
            return "";
        }else {
            String record = "";
            String shortest = strs[0];
            /**
             *找到最短的那个单词
             */
            for (int i = 1; i < strs.length; i++) {
                if (strs[i].length() < shortest.length()) {
                    shortest = strs[i];
                }
            }

//        System.out.println(shortest);
            int i = 0;
            for (; i < strs.length ; i++) {//i表示第i个单词
                if(strs[i].length()>0)
                for (int j = 0; j < strs.length; j++) {//j表示单词的第j个字母
                    if (shortest.charAt(i) == strs[j].charAt(i)) {
                        if (shortest.charAt(i) == strs[strs.length - 1].charAt(j)) {
                            record += strs[strs.length - 1].charAt(j);
                        }

                        continue;
                    } else
                        break;
                }

            }
            return record;
        }


//            record = shortest.substring(0, i-1);
//        System.out.println(record);

    }


    public int[] twoSum(int[] nums, int target) {
        int[] num = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (target == nums[i] + nums[j]) {
                    if (i != j) {
                        num = new int[]{i, j};
                        return num;
                    } else if (i == j)
                        break;
                } else {
                    continue;
                }
            }
        }
        return num;
    }

}

 

posted @ 2021-12-30 14:59  myyssfb  阅读(33)  评论(0)    收藏  举报