题解4

14. 最长公共前缀

解法一:

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0)
            return "";
        if(strs.length == 1)
            return strs[0];
        //先找出前两个元素的公共前缀
        String p = "";
        p = judge(strs[0], strs[1]);
        //遍历后面的元素,将公共前缀与后面的元素进行对比,更新公共前缀
        for(int i = 2; i < strs.length; i ++){
            p = judge(p, strs[i]);
        }
        return p;
    }
    //判断公共前缀
    public String judge(String s1, String s2) {        
        int len = Math.min(s1.length(),s2.length());
        //因为公共前缀的长度只能变短或者不变不能增加,所以只需判断前len个字母即可
        for (int i = len; i > 0; i--) {
        /*
        stringObject.substring(start,stop)
        start:必需,一个非负的整数,规定要提取的子串的第一个字符在 stringObject 中的位置。
        stop: 可选,一个非负的整数,比要提取的子串的最后一个字符在 stringObject 中的后一个位置。如果省略该参数,那么返回的子串会一直到字符串的结尾。
        */
            String t = s1.substring(0,i);
            if (t.equals(s2.substring(0,i))){
                return t;
            }
        }
        return "";
    }
}

解法二:

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        //遍历strs[0]字符串的每一个元素,依次判断其余字符串相同位置的元素是否与之相同
        for(int i = 0; i < strs[0].length(); i ++) {
            char c = strs[0].charAt(i);
            for(int j = 1; j < strs.length; j ++){
                if(i >= strs[j].length() || strs[j].charAt(i) != c)
                    return strs[0].substring(0, i);
            }
        }
        return strs[0];
    }
}

15. 两数之和

解法一:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        //排序
        Arrays.sort(nums);
        int len = nums.length;
        //取第一个元素为下标0到(len-2),另外两个元素分别在第一个元素的右边取
        for(int i = 0; i < len - 2; i ++) {
            //如果该数组最小的元素大于零,那么不可能存在三树之和等于零
            if(nums[i] > 0)
                return lists;
            //跳过重复元素(若有连续多个重复元素,真正使用的是第一个重复元素)
            if(i > 0 && nums[i] == nums[i-1])
                continue;
            int curr = nums[i];
            int left = i + 1, right = len-1;
            //在第一个元素的右边找到所有满足条件的组合
            while (left < right) {
                if(curr + nums[left] + nums[right] == 0) {
                    List<Integer> list = new ArrayList<>();
                    list.add(curr);
                    list.add(nums[left]);
                    list.add(nums[right]);
                    lists.add(list);
                    //跳过重复元素
                    while (left < right && nums[left+1] == nums[left]) 
                        left ++;
                    while (left < right && nums[right-1] == nums[right])
                        right --;
                    //判断下一组元素
                    left ++;
                    right --;
                }
                //三数之和小于0,则取一个大一点的left
                else if(curr + nums[left] + nums[right] < 0) {
                    left ++;
                }
                //三数之和大于0,则取一个小一点的right
                else {
                    right --;
                }
            }
        }
        return lists;
    }
}

解法二:

class Solution {
    //与上一个方法思路相同,不过是用哈希表来解决重复答案的问题
    public List<List<Integer>> threeSum(int[] nums) {
        Set result = new HashSet();
        Arrays.sort(nums);
        int len = nums.length;
        for(int i = 0; i < len - 2; i ++) {
            if(nums[i] > 0)
                return new ArrayList<>(result);
            int curr = nums[i];
            int left = i + 1, right = len-1;
            while (left < right) {
                if(curr + nums[left] + nums[right] == 0) {
                    List result2 = new ArrayList();
                    result2.add(curr);
                    result2.add(nums[left]);
                    result2.add(nums[right]);
                    result.add(result2);
                    left ++;
                    right --;
                }
                else if(curr + nums[left] + nums[right] < 0) {
                    left ++;
                }
                else {
                    right --;
                }
            }
        }
        return new ArrayList<>(result);
    }
}

8. 字符串转换整数

public class Solution {
    public int myAtoi(String str) {
        char[] chars = str.toCharArray();
        int n = chars.length;
        int index = 0;
        //去掉空格
        while (index < n && chars[index] == ' ')
            index ++;
        if (index == n)
            return 0;
        //是否为负数
        boolean negative = false;
        //有负号
        if (chars[index] == '-') {
            negative = true;
            index++;
        }
        //有正号
        else if (chars[index] == '+') {
            index++;
        }
        //遇到其他字符,Character.isDigit()方法用于判断指定字符是否为数字
        else if (!Character.isDigit(chars[index])) {
            return 0;
        }
        int ans = 0;
        while (index < n && Character.isDigit(chars[index])) {
            int digit = chars[index] - '0';
            //即ans * 10 + digit > Integer.MAX_VALUE,判断是否越界
            if (ans > (Integer.MAX_VALUE - digit) / 10) {
                //如果数据越界,有负号则返回最小值,无负号则返回最大值
                return negative? Integer.MIN_VALUE : Integer.MAX_VALUE;
            }
            ans = ans * 10 + digit;
            index ++;
        }
        return negative? -ans : ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值