美文网首页Leetcode程序员
LeetCode算法代码笔记(11-15)

LeetCode算法代码笔记(11-15)

作者: cpacm | 来源:发表于2017-05-25 14:44 被阅读39次

    给自己的目标:[LeetCode](https://leetcode.com/ "Online Judge Platform") 上每日一题

    在做题的过程中记录下解题的思路或者重要的代码碎片以便后来翻阅。
    项目源码:github上的Leetcode

    11. Container With Most Water

    题目:以X轴为底,给出一组高度不同的值,求其中的两个值能够容纳最多水的值。

    常规思路是输入一个值然后跟前面一一比较,这样导致时间复杂度为 nlogn 。但因为值是一口气给你的,所有可以从两边开始缩进,毕竟面积取决于短板。

    public class Solution {
        public int maxArea(int[] height) {
            int maxArea = 0;
            int start = 0;
            int end = height.length - 1;
            while (start < end) {
                int area = Math.min(height[start], height[end]) * (end - start);
                if (height[start] <= height[end]) {
                    start++;
                } else {
                    end--;
                }
                maxArea = Math.max(maxArea, area);
            }
            return maxArea;
        }
    }
    

    12. Integer to Roman

    题目: 数字转罗马字,范围为1-3999.

    将千百十个的值用数组保存,然后整除求余获得相应值。

    public class Solution {
        
        String[] M = {"", "M", "MM", "MMM"};
        String[] D = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
        String[] X = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
        String[] I = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
        
        public String intToRoman(int num) {
            String str = M[num / 1000] + D[num % 1000 / 100] + X[num % 100 / 10] + I[num % 10];
            return str;
        }
    }
    

    13. Roman to Integer

    题目:将罗马字转为数字

    public class Solution {
        public int romanToInt(String s) {
            int value = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'M') {
                    value += 1000;
                }
                if (s.charAt(i) == 'D') {
                    value += 500;
                }
                if (s.charAt(i) == 'C') {
                    if (i + 1 < s.length() && (s.charAt(i + 1) == 'M' || s.charAt(i + 1) == 'D')) {
                        value -= 100;
                    } else {
                        value += 100;
                    }
                }
                if (s.charAt(i) == 'L') {
                    value += 50;
                }
                if (s.charAt(i) == 'X') {
                    if (i + 1 < s.length() && (s.charAt(i + 1) == 'L' || s.charAt(i + 1) == 'C')) {
                        value -= 10;
                    } else {
                        value += 10;
                    }
                }
                if (s.charAt(i) == 'V') {
                    value += 5;
                }
                if (s.charAt(i) == 'I') {
                    if (i + 1 < s.length() && (s.charAt(i + 1) == 'X' || s.charAt(i + 1) == 'V')) {
                        value -= 1;
                    } else {
                        value += 1;
                    }
                }
    
            }
            return value;
        }
    }
    

    14. Longest Common Prefix

    题目: 输入一组字符串,求最长相同前缀。

    public class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs.length == 0) {
                return "";
            }
            String commonStr = strs[0];
            for (int i = 1; i < strs.length; i++) {
                String str = strs[i];
                int j = 0;
                while (j < str.length() && j < commonStr.length()) {
                    if (str.charAt(j) == commonStr.charAt(j)) {
                        j++;
                    }else{
                        break;
                    }
                }
                commonStr = commonStr.substring(0, j);
            }
            return commonStr;
        }
    }
    

    15. 3Sum

    题目:输入一行数字,求出所有三个数相加为0的所有组合,不允许重复。

    For example, given array S = [-1, 0, 1, 2, -1, -4],
    
    A solution set is:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]
    

    首先要给数组排序,依次取出一个值作为目标值,剩下的值依次取和来匹配。同时当值相同时要去重复。利用大小比来避免没必要的重复且能保证不会错过组合。

    public class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            Arrays.sort(nums);
            List<List<Integer>> result = new ArrayList<>();
    
            for (int i = 0; i < nums.length - 2; i++) {
                if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
                    int start = i + 1, end = nums.length - 1, target = 0 - nums[i];
                    while (start < end) {
                        if (nums[start] + nums[end] == target) {
                            result.add(Arrays.asList(nums[start], nums[end], nums[i]));
                            while (start < end && nums[end] == nums[end - 1]) end--;
                            while (start < end && nums[start + 1] == nums[start]) start++;
                            end--;
                            start++;
                        } else if (nums[start] + nums[end] < target) {
                            start++;
                        } else {
                            end--;
                        }
                    }
                }
            }
    
            return result;
        }
    }
    

    相关文章

      网友评论

        本文标题:LeetCode算法代码笔记(11-15)

        本文链接:https://www.haomeiwen.com/subject/feczxxtx.html