c#将字母转换为数字以进行二分查找

时间:2016-12-18 05:00:36

标签: c# algorithm encoding character-encoding

学习搜索算法。我对这个问题的引导感兴趣,以及你可以推荐我加入这个想法的内容。你知道任何快速方法,参考资料等吗?你可以添加任何深度都很酷。

我将字母表编码为二进制搜索,依赖于字符串

(伪代码)

ReactDOM.render(
   <Hello />,
   document.getElementById('entry')
);

所以字符串“bab dab”将返回2120412.

然后数字进入某种方法包含类似于此的算法:

(伪示例)

        "indent = 0, A = 1, B = 2, C = 3, D = 4" 

1 个答案:

答案 0 :(得分:0)

是的,您可以将char投射到int,它会为您提供角色的ASCII value。例如,A = 65,a = 97等。这是一个代码示例来说明这一点(你可以调整它以适合你自己的目的):

private static int[] StringToIntArray(string str)
    {
        List<int> ints = new List<int>();

        foreach (char chr in str)
        {
            // Technically you don't have to be explicit about the cast but I include it here for clarity
            int chrAsInt = (int)chr;

            // Do the following if you don't care about capital vs. lowercase
            // Otherwise, tweak the numbering system a little
            if (chrAsInt >= 97)
            {
                ints.Add(chrAsInt - 96);
            }
            else
            {
                ints.Add(chrAsInt - 64);
            }
        }

        return ints.ToArray();
    }