LeetCode知识点总结 - 1974
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相关推荐
- LeetCode知识点总结 - 884
LeetCode 884. Uncommon Words from Two Sentences 考点 难度 Hash Table Easy 题目 A sentence is a string of s ...
- LeetCode知识点总结 - 868
LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...
- LeetCode知识点总结 - 1413
LeetCode 1413. Minimum Value to Get Positive Step by Step Sum 考点 难度 Prefix Sum Easy 题目 Given an arra ...
- LeetCode知识点总结 - 2073
LeetCode 2073. Time Needed to Buy Tickets 考点 难度 Simulation Easy 题目 There are n people in a line queu ...
- LeetCode知识点总结 - 347
LeetCode 347. Top K Frequent Elements 考点 难度 Array Medium 题目 Given an integer array nums and an integ ...
- LeetCode知识点总结 - 997
LeetCode 997. Find the Town Judge 考点 难度 Hash Table Easy 题目 In a town, there are n people labeled fro ...
- LeetCode知识点总结 - 606
LeetCode 606. Construct String from Binary Tree 考点 难度 String Easy 题目 Given the root of a binary tree ...
- LeetCode知识点总结 - 844
LeetCode 844. Backspace String Compare 考点 难度 Simulation Easy 题目 Given two strings s and t, return tr ...
- LeetCode知识点总结 - 977
LeetCode 977. Squares of a Sorted Array 考点 难度 Sorting Easy 题目 Given an integer array nums sorted in ...
最新文章
- 图解DIY高手组装电脑全过程
- 这些基础协议,你懂吗?
- Python map, reduce, filter和sorted
- python字典默认输出键还是值_说说在 Python 字典中如何在读取不存在的键时得到一个默认值...
- 8-1 回溯法实验报告 (15 分)(思路+详解)
- 不同类的方法 事务问题_【高中地理】描述类问题的答题方法
- 微服务升级_SpringCloud Alibaba工作笔记0013---SpringCloud Alibaba简介
- 两数据库Dblink数据抽取blob
- 程序员 45 岁怎么了?!
- 基于依存句法分析的资金账户交易画像
- ssh-copy-id命令的介绍,使免密更方便
- python爱好者社区公众号历史文章合集_微信公众号历史文章爬取api
- win7计算机名称格式,win7笔记本电脑如何显示文件扩展名
- Python轻松实现地图可视化(附详细源码)
- Android 切割图片
- 重估2020:“黑天鹅”催生的AI新业态
- pytorch 提高gpu利用率
- GameDesign历程回顾
- sudo命令及权限管理命令
- html5 image 内存溢出,解决内存溢出问题