网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

[LeetCode]27.RemoveElement-创新互联

27. Remove Element

创新互联建站是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,小程序设计,十年建站对成都小搅拌车等多个领域,拥有丰富的网站制作经验。

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
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.

题意:

根据给定一个数组和一个关键值,删除数组所有与关键值一样的元素,并返回新的数组长度。


解题:

逐一遍历数组元素,逐个比较,进行操作。

1)如果数组中元素值一致时,只需要将数组长度减一;否则,把cnt中下标元素赋值给index的位置。因为如果中间出现给定值时,覆盖掉该位置值。

int removeElement(int* nums, int numsSize, int val)
{
    int cnt   = 0;
    int size  = numsSize;
    int index = 0;
    for ( cnt = 0; cnt < numsSize; cnt++ )
    {
        if ( *(nums + cnt) != val )                                               {
            *(nums + index) = *(nums + cnt);
            index++;
        }
        else
        {
            size -= 1;
        }
    }
    
    if ( index != numsSize )
    {   
        *(nums + index) = '\0';
    }
    
    return size;
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:[LeetCode]27.RemoveElement-创新互联
本文来源:http://bjjierui.cn/article/dgijhg.html

其他资讯