当前位置: 首页 > wzjs >正文

58同城网站模板有哪些网站可以做设计挣钱

58同城网站模板,有哪些网站可以做设计挣钱,可以做logo设计单子的网站,中国建设网站首页【LetMeFly】3110.字符串的分数:模拟(注意一个小细节) 力扣题目链接:https://leetcode.cn/problems/score-of-a-string/ 给你一个字符串 s 。一个字符串的 分数 定义为相邻字符 ASCII 码差值绝对值的和。 请你返回 s 的 分数 。 示例 1: 输…

【LetMeFly】3110.字符串的分数:模拟(注意一个小细节)

力扣题目链接:https://leetcode.cn/problems/score-of-a-string/

给你一个字符串 s 。一个字符串的 分数 定义为相邻字符 ASCII 码差值绝对值的和。

请你返回 s 的 分数 。

 

示例 1:

输入:s = "hello"

输出:13

解释:

s 中字符的 ASCII 码分别为:'h' = 104 ,'e' = 101 ,'l' = 108 ,'o' = 111 。所以 s 的分数为 |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13 。

示例 2:

输入:s = "zaz"

输出:50

解释:

s 中字符的 ASCII 码分别为:'z' = 122 ,'a' = 97 。所以 s 的分数为 |122 - 97| + |97 - 122| = 25 + 25 = 50 。

 

提示:

  • 2 <= s.length <= 100
  • s 只包含小写英文字母。

解题方法:模拟

直接从第二个字符开始遍历字符串,将这个字符减去前一个字符的绝对值累加到答案中,最终返回即可。

注意:请谨慎使用8比特数减8比特数(如char - char或byte - byte),因为有的编程语言中8比特数相减不会类型提升转为int相减。

如下代码的运行结果为255和255:

package mainimport "fmt"func main() {a := byte(1)b := byte(2)c := a - bfmt.Println(c)  // 255d := int(c)fmt.Println(d)  // 255
}
  • 时间复杂度 O ( l e n ( s ) ) O(len(s)) O(len(s))
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-03-15 10:26:54* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-03-15 10:27:59*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endifclass Solution {
public:int scoreOfString(string s) {int ans = 0;for (int i = 1; i < s.size(); i++) {ans += abs(s[i] - s[i - 1]);}return ans;}
};
Python
'''
Author: LetMeFly
Date: 2025-03-15 10:28:26
LastEditors: LetMeFly.xyz
LastEditTime: 2025-03-15 10:28:50
'''
class Solution:def scoreOfString(self, s: str) -> int:return sum(abs(ord(s[i]) - ord(s[i - 1])) for i in range(1, len(s)))
Java
/** @Author: LetMeFly* @Date: 2025-03-15 10:36:15* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-03-15 10:36:23*/
class Solution {public int scoreOfString(String s) {int ans = 0;for (int i = 1; i < s.length(); i++) {ans += Math.abs(s.charAt(i) - s.charAt(i - 1));}return ans;}
}
Go
/** @Author: LetMeFly* @Date: 2025-03-15 10:29:29* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-03-15 10:32:20*/
package mainfunc abs3110(a int) int {if a < 0 {return -a}return a
}func scoreOfString(s string) (ans int) {for i := 1; i < len(s); i++ {ans += abs3110(int(s[i]) - int(s[i - 1]))}return
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源


文章转载自:

http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://00000000.Lhrwy.cn
http://www.dtcms.com/wzjs/604338.html

相关文章:

  • 网站建设需要哪些岗位北京金山办公软件有限公司
  • 长春网站建设策划方案做网站答辩
  • 杭州装饰网站建设方案注册个公司大概多少钱
  • 成都网站建设蜀美网络项目运营方案计划书
  • 汉阳网站推广优化wordpress手机显示
  • 金华市建设银行网站平台网站建设调研报告
  • 怎样用电脑做网站服务器昆明网站的建设
  • 昆明做网站要多少钱施工企业信用评价
  • 南昌p2p网站建设公司营销软文推广平台
  • 杭州建设工程信息网站关系营销案例100例
  • 网时代it培训官网seo免费工具
  • 网站群建设 中标西安营销型网站建设公司
  • 微信公众号绑定网站西宁网站建设公司
  • 东莞最好的网站建设响应式网站开发教程pdf
  • 商务网站建设实训心得制作网站难还是编程难
  • 怎么用esc服务器做网站网站ui设计规范
  • wordpress主题换图片不显示湛江seo网站管理
  • 自己做网站需要什么技术互联网建设
  • 简述电子商务网站建设的主要步骤桂林学校网站制作
  • 利用手机搭建网站做电影平台网站怎么赚钱吗
  • 安徽省建设干部培训学校网站手机移动端网站做多大
  • 如何建立网站和网页有哪些网站做的比较好的
  • 年度考核表个人总结网站建设产品推广方式
  • 自己做衣服的网站平远县建设工程交易中心网站
  • 移动端网站怎么做ui设计是做什么工作
  • 学院网站建设的意义网站做icp备案需要多久
  • 做海报的话网站广告公司好做吗
  • 成都网络公司网站建设wordpress 相关帖子
  • 沈阳建站网页模板网页视频怎么下载到本地视频电脑
  • 长春火车站附近有什么好玩的地方网站的管理有是