首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在迭代数组时检查某些“分隔符元素”是否有规律的间距?

在迭代数组时检查某些“分隔符元素”是否有规律的间距?
EN

Stack Overflow用户
提问于 2015-04-20 00:57:50
回答 2查看 54关注 0票数 1

我在想出解决这个问题的有效算法时遇到了麻烦:

遍历数组,检查作为“标记”的元素。如果我注意到任何标记没有将其余元素划分为相同长度的游程,则设置一个标记。除了最后一次运行,这是剩余的。

不应设置标志的示例:

代码语言:javascript
运行
复制
*....*....*..*

应设置该标志的示例:

代码语言:javascript
运行
复制
*....*...*...*
*....*....**

直觉告诉我们,在网上做一些微不足道的事情应该是可能的,而且它可能等同于一些众所周知的问题,而我不知道这些问题的通常名称。

EN

Stack Overflow用户

发布于 2015-04-20 21:47:46

诀窍是首先根据第一次出现的*xyz*来计算预期长度。一旦知道了这一点,我们就知道在哪里可以得到剩余的分隔符(*)。如果遇到移位的除法器,它是非法的,除非它是余数的一部分。

它本质上与Riko的答案中的逻辑相同,但代码更多,因为计算段大小是内联完成的,而不是使用string.split。

下面是一个用JavaScript编写的示例。我尽量避免使用JavaScript的功能更多的方面,以使其尽可能简单。不幸的是,这让它变得有点像一堵代码墙。

代码语言:javascript
运行
复制
var isCorrect = function( str, divider ) {

    process.stdout.write( str + ': ' );

    // First check the obvious cases. These allow us to skip these checks
    // within the loop.
    if( str[0] !== divider )
        return "Doesn't start with divider";
    if( str[ str.length - 1 ] !== divider )
        return "Doesnt' end with divider";

    // Two variables to hold the state.
    // The second variable (divisions) is required only if we want to make
    // sure that the last segment is "optimal".
    var division = null;
    var divisions = 0;

    // First find the first divider.
    var i = 1;
    for( ; i < str.length; i++ ) {
        if( str[i] === divider ) {
            division = i;
            divisions++;
            break;
        }
    }

    // Now that we know the division length, make sure the dividers
    // are in expected positions.
    for( ; i < str.length; i++ ) {

        var expectedDivider = ( (i) % division === 0 );

        // See if we are expecting a divider.
        if( expectedDivider ) {
            if( str[i] !== divider )
                return "Expected divider at position " + i;
            divisions++;
            continue;
        }

        // Since we are not expecting a divider, make sure we don't have one.
        if( str[i] === divider ) {

            // We had a divider in an unexpected place. This is only allowed for
            // the last segment.
            if( i < str.length - 1 )
                return "Divider not expected at position " + i;

            // This is last segment. We could return 'ok' here unless we want
            // the optimal segments.

            // For optimal segments we want to know whether the last segment
            // could have "borrowed" items from the previous ones while still
            // remaining smaller than the rest.

            // Calculate the bits missing from the last segment.
            var offset = ( (i-1) % division );
            var missing = division - offset - 1;

            if( offset === 0 )
                return "Empty division at the end";

            // Could the missing bits be taken from the previous divisions?
            if( missing > divisions )
                return "Last division too short";

            // Last segment was OK.
            return "ok";
        }
    }

    // All divisions were in expected places:
    // Last segment was as long as the rest.
    return "ok";
};

我使用的测试用例:

代码语言:javascript
运行
复制
// Simple cases

// OK
console.log( isCorrect( '*--*--*--*', '*' ) );
console.log( isCorrect( '*---*---*---*', '*' ) );
console.log( isCorrect( '*---*---*--*', '*' ) );

// Middle segment too short.
console.log( isCorrect( '*-----*----*-----*', '*' ) );

// First segment too short
console.log( isCorrect( '*----*-----*-----*', '*' ) );

// "Optimality" tests

// In "optimal" division the segments could be divided to three with
// *----*----*---* so *-----*-----*-* is "unoptimal"
console.log( isCorrect( '*-----*-----*-*', '*' ) );

// These are "optimal"
console.log( isCorrect( '*-----*-----*--*', '*' ) );
console.log( isCorrect( '*-----*-----*---*', '*' ) );
console.log( isCorrect( '*-----*-----*----*', '*' ) );
console.log( isCorrect( '*-----*-----*-----*', '*' ) );

// Last segment too long
console.log( isCorrect( '*-----*-----*------*', '*' ) );

// Last segment empty
console.log( isCorrect( '*--*--*--*--*--**', '*' ) );
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29732927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档