题解 | 小红的字符串 c#

小红的字符串

https://www.nowcoder.com/practice/f9738786c00a40a2b7ddf6c82c1a59b1

using System;
using System.Collections.Generic;
public class Program {
    public static void Main() {
        string str = Console.ReadLine();

        //双指针
        int left = 0;
        int right = str.Length - 1;

        int totalOpCount = 0;

        while (left < right) {
            char leftChar = str[left];
            char rightChar = str[right];
            int leftPos = leftChar - 'a';
            int rightPos = rightChar - 'a';
			//计算两个字符之间的直接距离
            int directDistance = Math.Abs(leftPos - rightPos);
		  	//计算环绕距离并对比和直接距离的大小,选择出最小的那个
            int wrapAroundDistance = 26 - directDistance;
            int shortestDistance = Math.Min(directDistance, wrapAroundDistance);

            totalOpCount += shortestDistance;

            left++;
            right--;
        }
        Console.WriteLine(totalOpCount);
    }
    
}

全部评论

相关推荐

在喝茶的杨桃很郁闷:我简单喵两句: 1.如果不是实在没东西写不要写熟悉async await这些语法层面的东西 2.也不要写熟悉HTTP,因为http内容很多,稍微深挖一点你不会的话会让人有一种“原来你简历上面的东西都没有完全掌握”的感觉,容易给自己挖坑 3.自我评价可以删了 4.我在复习明天的面试,先mark,后面再回来继续建议
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
02-23 19:06
已编辑
西北工业大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务