LeetCode 1974. Minimum Time to Type Word Using Special Typewriter

考点 难度
Greedy Easy
题目

There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'.
Each second, you may perform one of the following operations:
Move the pointer one character counterclockwise or clockwise.
Type the character the pointer is currently on.
Given a string word, return the minimum number of seconds to type out the characters in word.

思路

先算word的长度(因为print每个字母需要一秒),再依次加上相邻两个字母顺时针和逆时针距离中更小的那个。

答案
public int minTimeToType(String word) {int cnt = word.length();char prev = 'a';for (int i = 0; i < word.length(); ++i) {char cur = word.charAt(i);int diff = Math.abs(cur - prev);cnt += Math.min(diff, 26 - diff);prev = cur;}return cnt;
}

LeetCode知识点总结 - 1974相关推荐

  1. LeetCode知识点总结 - 884

    LeetCode 884. Uncommon Words from Two Sentences 考点 难度 Hash Table Easy 题目 A sentence is a string of s ...

  2. LeetCode知识点总结 - 868

    LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...

  3. LeetCode知识点总结 - 1413

    LeetCode 1413. Minimum Value to Get Positive Step by Step Sum 考点 难度 Prefix Sum Easy 题目 Given an arra ...

  4. LeetCode知识点总结 - 2073

    LeetCode 2073. Time Needed to Buy Tickets 考点 难度 Simulation Easy 题目 There are n people in a line queu ...

  5. LeetCode知识点总结 - 347

    LeetCode 347. Top K Frequent Elements 考点 难度 Array Medium 题目 Given an integer array nums and an integ ...

  6. LeetCode知识点总结 - 997

    LeetCode 997. Find the Town Judge 考点 难度 Hash Table Easy 题目 In a town, there are n people labeled fro ...

  7. LeetCode知识点总结 - 606

    LeetCode 606. Construct String from Binary Tree 考点 难度 String Easy 题目 Given the root of a binary tree ...

  8. LeetCode知识点总结 - 844

    LeetCode 844. Backspace String Compare 考点 难度 Simulation Easy 题目 Given two strings s and t, return tr ...

  9. LeetCode知识点总结 - 977

    LeetCode 977. Squares of a Sorted Array 考点 难度 Sorting Easy 题目 Given an integer array nums sorted in ...

最新文章

  1. 图解DIY高手组装电脑全过程
  2. 这些基础协议,你懂吗?
  3. Python map, reduce, filter和sorted
  4. python字典默认输出键还是值_说说在 Python 字典中如何在读取不存在的键时得到一个默认值...
  5. 8-1 回溯法实验报告 (15 分)(思路+详解)
  6. 不同类的方法 事务问题_【高中地理】描述类问题的答题方法
  7. 微服务升级_SpringCloud Alibaba工作笔记0013---SpringCloud Alibaba简介
  8. 两数据库Dblink数据抽取blob
  9. 程序员 45 岁怎么了?!
  10. 基于依存句法分析的资金账户交易画像
  11. ssh-copy-id命令的介绍,使免密更方便
  12. python爱好者社区公众号历史文章合集_微信公众号历史文章爬取api
  13. win7计算机名称格式,win7笔记本电脑如何显示文件扩展名
  14. Python轻松实现地图可视化(附详细源码)
  15. Android 切割图片
  16. 重估2020:“黑天鹅”催生的AI新业态
  17. pytorch 提高gpu利用率
  18. GameDesign历程回顾
  19. sudo命令及权限管理命令
  20. html5 image 内存溢出,解决内存溢出问题

热门文章

  1. 953.验证外星语词典
  2. 石油大--2020年秋季组队训练赛第十三场----B、Bouldering(最短路)
  3. 有哪些能支持epub、txt格式的电子书阅读器?能在苹果手机上用的?
  4. 神奇的电子书阅读器Kindle
  5. STM32使用ESP8266接入机智云点亮LED教程
  6. 三柱汉诺塔四柱汉诺塔
  7. 几种“词云”制作工具效果评测
  8. Java:做一个自动生成24点游戏,并能自主解决的程序
  9. 强大新UI神器微信小程序源码多模板支持多种流量主模式
  10. php html转pdf wkhtmltopdf