leetcode 393. UTF-8 Validation(UTF-8 编码验证)

IDEA中Maven工具栏与命令执行全攻略:高效开发必备技巧 IntelliJ IDEA 作为强大的 Java 开发工具,其 Maven 功能独具优势。Maven 工具栏位于工作区右侧,图标功能丰富,涵盖项目导入、源码处理、依赖操作等。执行 Maven 命令有多种途径,Run Anything 窗口可输入或选目标执行,上下文菜单能右键运行目标,还可创建运行配置,指定命令参数后运行,为开发者高效管理 Maven 项目、便捷执行命令提供有力支持。 阅读详情

Given an integer array data representing the data, return whether it is a valid UTF-8 encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters).

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

For a 1-byte character, the first bit is a 0, followed by its Unicode code.
For an n-bytes character, the first n bits are all one’s, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10.
This is how the UTF-8 encoding would work:

     Number of Bytes   |        UTF-8 Octet Sequence
                       |              (binary)
   --------------------+-----------------------------------------
            1          |   0xxxxxxx
            2          |   110xxxxx 10xxxxxx
            3          |   1110xxxx 10xxxxxx 10xxxxxx
            4          |   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

x denotes a bit in the binary form of a byte that may be either 0 or 1.

Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.

Example 1:

Input: data = [197,130,1]
Output: true
Explanation: data represents the octet sequence: 11000101 10000010 00000001.
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
Example 2:

Input: data = [235,140,4]
Output: false
Explanation: data represented the octet sequence: 11101011 10001100 00000100.
The first 3 bits are all one’s and the 4th bit is 0 means it is a 3-bytes character.
The next byte is a continuation byte which starts with 10 and that’s correct.
But the second continuation byte does not start with 10, so it is invalid.

看着很绕,简单解释下,
UFT-8是1~4byte长的,不能超过4byte.
4个byte可能出现的4种情况都在表里,不符合的就是false。
那你会说1 <= data.length <= 20000,data的长度可能到20000啊,怎么可能只有4 byte。
1~4 byte代表一个字符,后面的就是另一个字符,1~4 byte为一组。

思路:

经过上面的解释,可以做出如下分析。

第一个byte开头为0的不用考虑,肯定是UTF-8,跳过。
然后就是表里的case 2,3,4.
利用bit移位来判断,cnt记录后面应该出现多少个开头为10的byte.

public boolean validUtf8(int[] data) {
    int cnt = 0;
    
    for(int num : data) {
        if(cnt == 0) {
            if(num >> 5 == 0b110) cnt = 1;
            else if(num >> 4 == 0b1110) cnt = 2;
            else if(num >> 3 == 0b11110) cnt = 3;
            else if(num >> 7 == 1) return false;
        } else {
            if(num >> 6 != 0b10) return false;
            cnt --;
        }
    }
    
    return cnt == 0;
}

代码很简洁,但不是最快的,因为还要移位运算。
倒不如直接给出mask, 做与运算。
然后记录需要多少个“10”,一组一组判断。
注意numOfBytes中只有 numOfBytes-1个“10",比如4 byte中只有3个”10“

public boolean validUtf8(int[] data) {
    boolean isValid = true;
	for(int i=0;i<data.length;i++) {
		if(data[i]>255) return false; // 1 after 8th digit, 100000000
		int numberOfBytes = 0;
		if((data[i] & 128) == 0) numberOfBytes = 1; // 0xxxxxxx, 1 byte, 128(10000000)
		else if((data[i] & 224) == 192) numberOfBytes = 2; // 110xxxxx, 2 bytes, 224(11100000), 192(11000000)
		else if((data[i] & 240) == 224) numberOfBytes = 3; // 1110xxxx, 3 bytes, 240(11110000), 224(11100000)
		else if((data[i] & 248) == 240) numberOfBytes = 4; // 11110xxx, 4 bytes, 248(11111000), 240(11110000)
		else return false;

		for(int j=1;j<numberOfBytes;j++) { // check that the next n bytes start with 10xxxxxx
			if(i+j>=data.length) return false;
			if((data[i+j] & 192) != 128) return false; // 192(11000000), 128(10000000)
		}
		i=i+numberOfBytes-1; //循环结束后还会i++
	}
	return isValid;
}
从ISCXTor2016到CICIDS2017:手把手教你下载和使用UNB的网络安全数据集 本文详细介绍了UNB大学发布的ISCXTor2016和CICIDS2017等网络安全数据集的下载与使用方法,特别适合加密流量识别和网络行为分析研究。文章提供了多种高效获取路径、文件结构解析及实战分析示例,帮助研究者快速上手这些真实场景捕获的精细标注数据集。 阅读详情

相关推荐

AI提示词设计中的批判性思维训练方法

在人工智能应用中,提示词(prompt)设计是影响生成内容质量的关键因素。通过引入批判性思维框架,可以有效提升AI输出的逻辑性和多样性。该方法基于目的澄清、证据评估、假设检验和多视角分析四大支柱,构建结构化的问题拆解和证据链。在学术论文辅助写作、商业决策模拟等场景中,这种训练方式能显著提高内容质量,特别适合学生、职场人士和内容生产者。结合思维可视化工具和本地知识库,可以实现从基础应用到专家级分析的进阶。

weixin_33624741的博客 238

svn: Valid UTF-8 data 错误的解决

原文链接:svn: Valid UTF-8 data 错误的解决--FrancisSoung的博客 有时候,我们在项目svn提交时会出现这样的错误, svn: Valid UTF-8 data (hex: 47 64 20 53 63) followed by invalid UTF-8 sequence (hex: e9 6e 69 ...

weixin_34238642的博客 401

Ubuntu桌面消失?可能是lightdm配置在捣鬼:详细解析/etc/lightdm.conf的优先级机制与实战修复

本文详细解析了Ubuntu系统因lightdm显示管理器配置错误导致桌面消失的问题。通过分析/etc/lightdm.conf的优先级加载机制,提供了从systemctl诊断、调试模式运行到实战修复配置语法错误(如缺失[Seat:*]组)的完整方案。文章还探讨了在系统无法启动时使用chroot或恢复模式进行高级救援,并对比了lightdm与gdm3在NVIDIA驱动环境下的不同表现,帮助用户高效恢复图形界面。

weixin_29313547的博客 427

linux svn utf 8,转载:Linux下执行SVN命令时提示错误:Valid UTF-8 data

在Linux下执行svn add *时出现如下错误:svn: Valid UTF-8 data(hex: 4b)followed by invalid UTF-8sequence(hex: fc 63 68 65)出现这个错误是因为svn库里有文件的名字不是utf-8编码的,这种情况对于中文来说很常见。比如在自己的windows上建了一个中文名字的文件,就会使这种情况。几经周折,才找到解决...

weixin_35788914的博客 643

PIVlab软件入门:从GUI操作到2D2C粒子测速实战

本文详细介绍了PIVlab软件在2D2C粒子测速中的入门与实战应用。从GUI操作到环境搭建,再到图像预处理和核心参数设置,帮助用户快速掌握这一基于MATLAB的开源工具。通过实际案例和技巧分享,展示了PIVlab在流体力学实验中的高效性和实用性。

weixin_42547431的博客 233

SAP S/4HANA 可扩展性 – 面向初学者的简化指南

可扩展性(Extensibility)是 SAP ECC 和 SAP S/4HANA 的一项关键功能。它使客户能够通过定制其业务流程来创造竞争优势,并使合作伙伴能够通过量身定制的解决方案来丰富核心 ECC 或 S/4HANA。在过去的几十年中,SAP 的 on-premise 版本客户和合作伙伴主要使用经典的 ABAP 扩展性来扩展他们的 ERP 解决方案。经典扩展性允许 ABAP 开发人员使用甚至修改所有 SAP 对象。尽管经典扩展性非常强大和灵活,但在当今的云世界中,它已不再是一个好的选择。

SAP学习成长之路的博客 1821

393. UTF-8 Validation

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits

学渣记事本 448

Sourcetree与Gitee的搭配使用

初次将gitee与sourcetree联合起来使用,还是遇到了不少问题,其中最让人烦恼的就是在我修改了一下用户名之后,便无法在Sourcetree直接推送到gitee,很是让人郁闷。本文处理Sourcetree与gitee搭配使用过程中的问题:Sourcetree身份验证失败,无法推送;Sourcetree推送时,无法显示要推送的分支;Sourcetree远端上面有感叹号。

年少时的棉花糖 4446

utf-8-validate:验证缓冲区中的UTF-8编码文本

utf-8-validate:验证缓冲区中的UTF-8编码文本 在数字世界的交流与传输中,字符编码的正确性至关重要。utf-8-validate 是一个用于检查缓冲区是否包含有效UTF-8编码文本的开源项目,以下是对该项目的详细介绍。 项目介绍 utf-8-validate 是一个Node.js模块,它提供了一个简单的API来验证缓冲区中的数据是否为有效的UTF-8编码。这个模块特别适用于处理网络...

gitblog_00859的博客 732

电子纸价签实战:基于SSD1680的墨水屏驱动开发与优化

本文详细介绍了基于SSD1680驱动芯片的电子纸价签开发实战,涵盖硬件连接、驱动框架搭建、核心流程解析及性能优化。重点剖析了初始化配置、波形文件加载、图像数据写入与刷新触发等关键步骤,并提供了功耗优化、显示效果提升及代码健壮性等实用避坑指南,帮助开发者高效实现稳定低功耗的墨水屏驱动。

weixin_28278377的博客 240

华为OD机考双机位C卷 - 评委评分 (Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 评委评分 (OD上机考试双机位C卷)。提供在线OJ刷题。Java、C++、Python、GO语言、C语言、JavaScript题解,并提供完整思路讲解。

算法大师! 1773

leetcode 393. UTF-8 Validation | 393. UTF-8 编码验证(Java)

题目 https://leetcode.com/problems/utf-8-validation/ 题解 分析完情况之后,这题就是 if-else 堆起来的而已。 class Solution { public boolean validUtf8(int[] data) { for (int i = 0; i < data.length; i++) { int[] b = h2b(data[i]); int L = getLe

寒泉 4万+

倍福PLC--PERSISTENT断电保持数据和BOOT中各文件对应的解析

将D:\TC3程序\TwinCAT Project146\TwinCAT RT(x86)\Plc 文件夹的中三个替换文件复制到:G:\TwinCAT\3.1\Boot\Plc完成替换,如下图。(2) 找到程序所存的路径,编程电脑的D:\TC3程序\TwinCAT Project146路径下,并选择对应系统平台TwinCAT RT(x86)(3) 控制器断电,拔出内存卡,使用读卡器,在编程电脑中打开:(本次测试读卡器为G盘)(5) 开机完成后,TwinCAT自动运行,当前闪灯频率为50ms。

weixin_41883890的博客 2358

[leetcode] 393. UTF-8 Validation

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits

TstsUgeg的博客 815

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Leetcode-393. UTF-8 Validation

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。 博客链接:mcf171的博客 —————————————————————————————— A character in UTF8 can be from 1

mcf171的专栏 1040

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[leetcode]393. UTF-8 Validation

题目链接:https://leetcode.com/problems/utf-8-validation/#/description A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0,

xiaocong1990的博客 525

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

[leetcode] 393. UTF-8 Validation 解题报告

题目链接:https://leetcode.com/problems/utf-8-validation/ A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed b

小榕流光的专栏 2622

LeetCode //C - 393. UTF-8 Validation

【代码】LeetCode //C - 393. UTF-8 Validation。

Made in Code 776

【leetcode】393. UTF-8 Validation【M】

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits

sscssz的博客 1673

【leetcode】393. UTF-8 Validation

一、题目描述 A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the

小拳头的博客 1246

[Leetcode 393] UTF-8 Validation

393. UTF-8 Validation 393. UTF-8 编码验证 Intuition It’s obviously a bit manipulation problem. But also there are lots of states need to be considered. An encoded UTF-8 character is possiblely represented...

zdxiq000的专栏 325

Leetcode 393. UTF-8 Validation UTF-8 编码识别 解题报告

1 解题思想这道题主要是要求判别给定的二进制序列是否是合法的UTF-8编码 精简的说,UTF-8编码的特性有: 1、长度可以从1byte到4bytes 2、对于1byte的数据,其比特位的最高位是0 3、对于长度2-4的数据,其首个byte的高n-1位全部为1,随后的n-1个byte,其高2位全部为10在这道题中,以int的方式标示一个byte,取他的后8位就可以而解题的核心在于: 1、按

MebiuW的专栏 5099

Leetcode 393. UTF-8 Validation

【代码】Leetcode 393. UTF-8 Validation。

小白菜又菜 439

LeetCode393. UTF-8 Validation

A character in UTF8 can be from1 to 4 byteslong, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code. For n-bytes character, the first n-bits...

李歇特冯-兹拜因巴哈的博客 176

LeetCode--393. UTF-8 Validation

问题链接:https://leetcode.com/problems/utf-8-validation/ UTF-8的编码规则比较复杂,要仔细阅读确定编码规则,自己想的算法也比较暴力朴素,易于理解: 1.将十进制数转为长度为8的定长字符串  2.使用双指针法检查是否合法,代码如下 class Solution { public static boolean validUt...

To_be_thinking的博客 366
上一篇: Building wheel for opencv-python (PEP 517) 卡住
下一篇: from cv_bridge.boost.cv_bridge_boost import getCvType error解决
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址