C# 在stringbuider-C中查看项目的时间复杂性#

C# 在stringbuider-C中查看项目的时间复杂性#,c#,time-complexity,stringbuilder,C#,Time Complexity,Stringbuilder,我在StringBuilder中保存了一些长文本,我想获取一些特定的项目 StringBuilder builder = new StringBuilder(); //fill builder int i = someNumber(); char ch = builder[i]; 最后一条指令(char ch=builder[i])的时间复杂度是多少?它是常数吗 O(1) 还是线性的 O(i) 它是一个常数,因为你给出了元素的精确位置,所以在这个例子中是O(1)。这里有更多细节 这是一个常数,

我在
StringBuilder
中保存了一些长文本,我想获取一些特定的项目

StringBuilder builder = new StringBuilder();
//fill builder
int i = someNumber();
char ch = builder[i];
最后一条指令(
char ch=builder[i]
)的时间复杂度是多少?它是常数吗

O(1)

还是线性的

O(i)


它是一个常数,因为你给出了元素的精确位置,所以在这个例子中是O(1)。这里有更多细节

这是一个常数,因为您给出了获取元素的确切位置。因此在本例中为O(1)。这里有更多细节
charch=builder[i]
是O(1)


因为StringBuilder使用了数组索引。

char ch=builder[i]
是O(1)

因为StringBuilder使用数组索引。

根据
StringBuilder
类将字符串存储在字符数组中

通过属性getter
this[int index]
访问此数组会进行一些检查,然后返回数组项:

    internal char[] m_ChunkChars;                // The characters in this block
    //...more stuff

    [System.Runtime.CompilerServices.IndexerName("Chars")]
    public char this[int index] {
        // 

        get {
            StringBuilder chunk = this;
            for (; ; )
            {
                int indexInBlock = index - chunk.m_ChunkOffset;
                if (indexInBlock >= 0)
                {
                    if (indexInBlock >= chunk.m_ChunkLength)
                        throw new IndexOutOfRangeException();
                    return chunk.m_ChunkChars[indexInBlock];
                }
                chunk = chunk.m_ChunkPrevious;
                if (chunk == null)
                    throw new IndexOutOfRangeException();
            }
        }
        //... more stuff
    }
因此,复杂性是O(1)或恒定的访问时间。

根据
StringBuilder
类将字符串存储在字符数组中

通过属性getter
this[int index]
访问此数组会进行一些检查,然后返回数组项:

    internal char[] m_ChunkChars;                // The characters in this block
    //...more stuff

    [System.Runtime.CompilerServices.IndexerName("Chars")]
    public char this[int index] {
        // 

        get {
            StringBuilder chunk = this;
            for (; ; )
            {
                int indexInBlock = index - chunk.m_ChunkOffset;
                if (indexInBlock >= 0)
                {
                    if (indexInBlock >= chunk.m_ChunkLength)
                        throw new IndexOutOfRangeException();
                    return chunk.m_ChunkChars[indexInBlock];
                }
                chunk = chunk.m_ChunkPrevious;
                if (chunk == null)
                    throw new IndexOutOfRangeException();
            }
        }
        //... more stuff
    }

因此,复杂性是O(1)或恒定访问时间。

查看
StringBuilder
的实现,它是O(1),因为它是使用
char[]

    //
    //
    //  CLASS VARIABLES
    //
    //
    internal char[] m_ChunkChars;                // The characters in this block
    internal StringBuilder m_ChunkPrevious;      // Link to the block logically before this block
    internal int m_ChunkLength;                  // The index in m_ChunkChars that represent the end of the block
    internal int m_ChunkOffset;                  // The logial offset (sum of all characters in previous blocks)
    internal int m_MaxCapacity = 0;

查看
StringBuilder
的实现,它是O(1),因为它使用
char[]

    //
    //
    //  CLASS VARIABLES
    //
    //
    internal char[] m_ChunkChars;                // The characters in this block
    internal StringBuilder m_ChunkPrevious;      // Link to the block logically before this block
    internal int m_ChunkLength;                  // The index in m_ChunkChars that represent the end of the block
    internal int m_ChunkOffset;                  // The logial offset (sum of all characters in previous blocks)
    internal int m_MaxCapacity = 0;