您的位置:首页 > 其它

[leetCode刷题笔记]2017.02.05

2017-02-06 11:16 106 查看
今天早上上了一门课,然后出了除了买菜吃饭之外一直看书看到现在。。。。。然后继续刷题。。。。啊啊啊

18. 4Sum

方法见下链接:
http://www.cnblogs.com/yrbbest/p/4434188.html
和3sum一样。时间复杂度是O(n3)

26. Remove Duplicates from Sorted Array

这道题用到一个删除数组元素操作。另外,原位置删除以后,要减少循环的次数。。。个人觉得建一个新的array可能比这方便的多。

public class Solution {

    public int removeDuplicates(int[] nums) {

        if (nums.length == 0) {

            return 0;

        }

        int output = 0;

        int curr = 0;

        int i = 0;

        int len = nums.length;

        while (i < len) {

            if (i == 0) {

                curr = nums[i];

                output++;

                i++;

            }

            else {

                if (nums[i] != curr) {

                    output++;

                    curr = nums[i];

                    i++;

                }

                else {

                    delete(i, nums);

                    len--;

                }

            }

            

        }

        return output;

    }

    private void delete(int k, int[] nums) {

        for (int i = k; i < nums.length - 1; i++) {

            nums[i] = nums[i + 1];

        }

    }

}

27. Remove Element

思路和前面的一样,用上first point 和last point

public class Solution {

    public int removeElement(int[] nums, int val) {

        int last = nums.length - 1;

        int first = 0;

        while (first <= last) {

            if (val != nums[first]) {

                first++;

            }

            else {

                delete(first, nums);

                last--;

            }

        }

        return last + 1;

    }

    private void delete(int k, int[] nums) {

        for (int i = k; i < nums.length - 1; i++) {

            nums[i] = nums[i + 1];

        }

    }

}

31. Next Permutation

这道题目死活都看不懂。。。。所以找了一个讲的比骄清晰的。

其实不难http://blog.csdn.net/happyaaaaaaaaaaa/article/details/50973026
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: