【Leetcode_easy】821. Shortest Distance to a Character
problem
821. Shortest Distance to a Character
solution1:
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.size();
vector<int> res(n, n);
for(int i=; i<n; ++i) if(S[i]==C) res[i] = ;
for(int i=; i<n; ++i) res[i] = min(res[i], abs(res[i-]+));
for(int i=n-; i>=; --i) res[i] = min(res[i], abs(res[i+]+));
return res;
}
};
solution2:
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.size();
vector<int> res(n, n);
int pos = -n;//errr...
for(int i=; i<n; ++i)
{
if(S[i]==C) pos = i;
res[i] = min(res[i], abs(i-pos));
}
for(int i=n-; i>=; --i)
{
if(S[i]==C) pos = i;
res[i] = min(res[i], abs(i-pos));
}
return res;
}
};
参考
1. Leetcode_easy_821. Shortest Distance to a Character;
2. Discuss;
3. grandyang;
完
【Leetcode_easy】821. Shortest Distance to a Character的更多相关文章
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- 821. Shortest Distance to a Character - LeetCode
Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
- LeetCode 821 Shortest Distance to a Character 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- [LeetCode&Python] Problem 821. Shortest Distance to a Character
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- 【LeetCode】1182. Shortest Distance to Target Color 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://lee ...
- 【Leetcode_easy】849. Maximize Distance to Closest Person
problem 849. Maximize Distance to Closest Person solution1: class Solution { public: int maxDistToCl ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
- 【Leetcode_easy】748. Shortest Completing Word
problem 748. Shortest Completing Word 题意: solution1: class Solution { public: string shortestComplet ...
随机推荐
- 2.3 vue配置(上)
rm,在打包之前把上一次打包之后的东西删掉,然后webpack重新打包 通过DefinePlugin形成一个环境变量 HTML打包插件
- 如何的keil试试调试中,看逻辑分析仪Logic viwer
在调试过程中,可以使用keil自带的逻辑分析仪查看变量的试试信息,减少串口输出,提高部分cpu的效率,可以添加以下信息: 1.gpio引脚 2.全局变量 全局静态变量.局部变量是不行的. 然后,添加变 ...
- [CSS] The :empty Pseudo Selector Gotchas
The :empty pseudo selector selects empty elements. We can use this to display useful messages instea ...
- 使用jQuery快速高效制作网页交互特效---JavaScript对象及初始面向对象
一.JavaScript中的基本数据类型 number(数值类型) string(字符串类型) boolean(布尔类型) null(空类型) undefined(未定义类型) ...
- 引入其他服务器的JS文件,同时也引入本地JS文件 报错时:
控制台报错: Uncaught ReferenceError: define is not defined at core.js:5
- 坑爹的IE8
1.不能用trim(),要用$.trim() var aa = $("#id").val().trim() 这样素不行的,要变成这样Jquery的方式 var aa = $ ...
- DOM操作2
一.API和WebAPI API就是接口,就是通道,负责一个程序和其他软件的沟通,本质是预先定义的函数. Web API是网络应用程序接口.包含了广泛的功能,网络应用通过API接口,可以实现存储服务. ...
- 差分约束 4416 FFF 团卧底的后宫
/* 4416 FFF 团卧底的后宫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 你在某日收到了 FFF ...
- PHP全栈学习笔记25
<?php /* *@Author: 达叔小生 **/ header("content-type:image/png"); //设置页面编码 $num = $_GET['nu ...
- java Spring定时器 每个季度执行一次
@Scheduled(cron = " 0 00 00 1 4,7,10,1 ?")//每个季度的第一天零点进行统计此注解是每个季度结束后的下一天执行(因为Spring不识别字母( ...