查看: 2341| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[数组] Remove Duplicates from Sorted Array/Remove Element模板

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 oio14644 于 2015-8-30 14:52 编辑

Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Array II
Remove Element
发现者三个题基本可以共用一个模板,局部微调就可以
Remove Duplicates from Sorted Array
public int removeDuplicates(int[] nums) {
                if (nums.length == 0) {
                        return 0;
                }
                int appearTime = 1;
                int slow = appearTime;
                for (int i = appearTime; i < nums.length; i++) {
                        if (nums[slow - appearTime] != nums) {
[i]                                nums[slow] = nums[i];
                                slow++;
                        }
                }
                return slow;
        }


Remove Duplicates from Sorted Array II
public int removeDuplicates(int[] nums) {
                if (nums.length == 0) {
                        return 0;
                }
                int appearTime = 2; //微调在这里
                int slow = appearTime;
                for (int i = appearTime; i < nums.length; i++) {
                        if (nums[slow - appearTime] != nums[i]) {
                                nums[slow] = nums[i];
                                slow++;
                        }
                }
                return slow;
        }


Remove Element

public int removeElement(int[] nums, int elem) {
                if (nums.length == 0) {
                        return 0;
                }
                int appearTime = 0; //微调在这里
                int slow = appearTime;
                for (int i = appearTime; i < nums.length; i++) {
                        if (elem != nums[i]) {//微调在这里
                                nums[slow] = nums[i];
                                slow++;
                        }
                }
                return slow;
        }













[/i][/i][/i][/i][/i][/i]

上一篇:求大神给个最近的付费题目
下一篇:(新手求助)Implement Queue using Stacks
🔗
水逼一枚 2015-8-31 00:20:46 | 只看该作者
全局:
楼主现在是在做同类题目的解题总结工作是吗?
回复

使用道具 举报

🔗
sugar 2017-9-6 02:38:01 | 只看该作者
全局:
但是你这模板可能不够简洁?我觉得简单的题目直接写比较好
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表