LeetCode算法代码笔记(26-30)

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

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

26. Remove Duplicates from Sorted Array

题目:给出一组有序的数组,移除重复的数字后,输出不重复数字的个数。不允许分配新的空间来存储数组。

Given input array nums = [1,1,2]
output:length = 2,nums = [1,2]

直接将重复的数字用之后不重复的数字覆盖过去,这样输出的数组就能够不存在重复的数字了。

public class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int res = 1;
        int temp = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != temp) {
                temp = nums[i];
                nums[res] = nums[i];
                res++;
            }
        }
        return res;
    }
}

27. Remove Element

题目:给出一组有序的数组和一个给定的值,从数组中移除给定的数字后,输出数组长度。不允许分配新的空间来存储数组

Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

解题与 # 26 思路一样,同样是碰到指定的值后使用后面的值来进行覆盖。

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums.length == 0) {
            return 0;
        }
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[res] = nums[i];
                res++;
            }
        }
        return res;
    }
}

28. Implement strStr()

题目:给出两个字符串,求子串在父串的的位置,若不匹配返回-1.

之前以为 indexOf() nm 的时间复杂度会导致TLE,便想先看看KMP的算法或者Sunday算法来缩减复杂度,后来试了一下发现nm的时间复杂度能够直接AC.

public class Solution {
    public int strStr(String haystack, String needle) {
        return haystack.indexOf(needle);
    }
}

29. Divide Two Integers

题目:给出两个数进行相除,要求程序不能使用乘法,除法和求模的运算。数值溢出使用最大值。

刚开始直接使用减法来求值,最后导致 TLE . 换一种思路,任何一个整数可以表示成以2的幂为底的一组基的线性组合,即num=a_02^0+a_121+a_2*22+...+a_n*2^n。时间复杂度为 O(logn).

63/4 = 15;
63 = 32+16+8+4+3 = 4*2^3+4*2^2+4*2^1+4*2^0+3=(8+4+2+1)*4+3 = 63

注意正负数和数值的溢出。

public class Solution {
    public int divide(int dividend, int divisor) {
        int sign = 1;
        if (dividend < 0) sign = -sign;
        if (divisor < 0) sign = -sign;
        long temp = Math.abs((long) dividend);
        long temp2 = Math.abs((long) divisor);
        long c = 1;
        while (temp > temp2) {
            temp2 = temp2 << 1;
            c = c << 1;
        }
        long res = 0;
        while (temp >= Math.abs((long) divisor)) {
            while (temp >= temp2) {
                temp -= temp2;
                res += c;
            }
            temp2 = temp2 >> 1;
            c = c >> 1;
        }
        if (sign > 0) {
            if (res > Integer.MAX_VALUE) {
                return Integer.MAX_VALUE;
            }
            return (int) res;
        } else return -(int) res;
    }
}

30. Substring with Concatenation of All Words TLE

题目:给定一个字符串S和一个字符串数组L,L中的字符串长度都相等,找出S中所有的子串恰好包含L中所有字符各一次,返回子串的起始位置。

s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9].

使用map将数组中的字符串记录下来,从字符串截取字符来进行匹配。但是在最后的 case 中会出现 TLE.在网上找到的资料是说可以使用 slide window 的算法来实现,等待以后来学习。

/**
* 这是 TLE 的代码
 *  */
public class Solution {
    public List findSubstring(String s, String[] words) {
        List res = new ArrayList<>();
        if (s.length() == 0 || words.length == 0) {
            return res;
        }
        Map map = new HashMap<>();
        for (String str : words) {
            map.put(str, map.containsKey(str) ? map.get(str) + 1 : 1);
        }
        int len = words[0].length();
        for (int i = 0; i <= s.length() - len * words.length; i++) {
            HashMap tempMap = new HashMap<>(map);
            int temp = i;
            int count = 0;
            String tempStr = s.substring(temp, temp + len);
            while (tempMap.containsKey(tempStr) && tempMap.get(tempStr) > 0) {
                tempMap.put(tempStr, tempMap.get(tempStr) - 1);
                temp = temp + len;
                count++;
                if (temp + len <= s.length()) {
                    tempStr = s.substring(temp, temp + len);
                } else {
                    break;
                }
            }
            if (count == words.length) {
                res.add(i);
            }
        }
        return res;
    }
}

你可能感兴趣的:(LeetCode算法代码笔记(26-30))