LeetCode 393 - 编码验证_leetcode393_决定要好好毒树的Lucifer的博客-程序员宅基地

技术标签: 普通模拟  算法  leetcode  职场和发展  

题目链接icon-default.png?t=M276https://leetcode-cn.com/problems/utf-8-validation/题目:

Given an integer array data representing the data, return whether it is a valid UTF-8 encoding.

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:   

Char. number range  |        UTF-8 octet sequence
      (hexadecimal)    |              (binary)
   --------------------+---------------------------------------------
   0000 0000-0000 007F | 0xxxxxxx
   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

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.

思路:

最开始的想法是把给定的整数转化为二进制数再进行判断。不过这一步很明显是多余的,其实直接整数利用位运算判断前几位的几个情况即可。注意边界条件的处理,在判断array[i+1]的时候需要先判断有没有数组越界!另外,循环变量的处理也需要注意:不能多也不能少加,continue之后会执行一次i++!

一开始想要转化为二进制却不知道从何下手,有空需要复习进制转化的有关知识。

题解:

class Solution {
public:
    bool validUtf8(vector<int>& data) {
        int index=0, len=data.size();
        for(int i=0;i<len;i++){
            if(data[i]>>3==(2+4+8+16)){
                cout<<"find four bytes\n";
                if(i==len-1 || data[i+1]>>6!=2) return false;
                if(i==len-2 || data[i+2]>>6!=2) return false;
                if(i==len-3 || data[i+3]>>6!=2) return false;
                i+=3;
            }
            else if(data[i]>>4==(2+4+8)){
                cout<<"find three bytes\n";
                if(i==len-1 || data[i+1]>>6!=2) return false;
                if(i==len-2 || data[i+2]>>6!=2) return false;
                i+=2;
            }
            else if(data[i]>>5==(2+4)){
                cout<<"find two bytes\n";
                if(i==len-1 || data[i+1]>>6!=2) return false;
                i+=1;
            }
            else if(data[i]>>7==0) {
                cout<<"find one byte\n";
                continue;
            }
            else return false;
        }
        return true;
    }
};

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/djisbduaos/article/details/123465446

智能推荐

输入一个整数,判断这个整数是奇数还是偶数_咸汤圆的博客-程序员宅基地

有两种思路:A、利用取余运算。if(data%2) coutB、利用位与运算。if(data&0x1==1) coutend 剧终!!!!

php怎么设置当时系统时间,php如何设置服务器时间_王克丹的博客-程序员宅基地

php设置服务器时间的方法:首先找到并打开“php.ini”配置文件;然后修改“date.timezone”的值即可成功设置php服务器的时区。推荐:《PHP视频教程》Apache PHP服务器时间时区修改及时间比较做一个project需要用PHP比较当前时间,发现php显示的当前时间与实际时间有较大的差异,于是查了资料进行修改——在php.ini配置文件里修改date.timezone的值就能成..._php设置变量时间等于系统时间

中标麒麟(龙芯MIPS64版本)安装QT-Creator_mracal的博客-程序员宅基地

  刚拿到这台龙芯3A3000计算机的时候,着实兴奋了一把,于是各种折腾,记不得一开始的QT版本是哪个版本了。但是目前计算机上存在两个版本的QT,一个是第四版,一个是第五版,第五版是5.9.8,应该是系统自带的版本。 以下是安装QT-Creator的过程。  在安装之前,我们检查一下qmake版本    [user@localhost 桌面]$ qmake -v    QMake version 3.1    Using Qt version 5.9.8 in /usr/lib...

Intel Threading Building Blocks (Intel TBB) Developer Guide 中文 Parallelizing Data Flow and Depende..._weixin_30381317的博客-程序员宅基地

https://www.threadingbuildingblocks.org/docs/help/index.htmParallelizing Data Flow and Dependency GraphsIn addition to loop parallelism, the Intel Threading Building Blocks (Intel TBB) library..._onetbb developer guide

DSP相关笔记_4:LaunchXL-F28379D的简单教程系列(三)X-Bar与GPIO中断-下_c2000 x-bar_Msixram的博客-程序员宅基地

DSP相关笔记_4:LaunchXL-F28379D的简单教程系列(三)X-Bar与GPIO中断-下_c2000 x-bar

(admin.E403) | 使用jinjia2时报错_Bert Sun的博客-程序员宅基地

(admin.E403) A ‘django.template.backends.django.DjangoTemplates’ instance must be configured in TEMPLATES in order to use the admin application.| 使用jinjia2时报错

随便推点

java生成随机数的代码_java生成随机数的代码_kk七的博客-程序员宅基地

package test;/** * 产生10个随机数的各种方法 * @author Administrator * */ public class Test3 { //利用Math.random产生 public static void generateRandom1(){ for(int i =0;i<10;i++){ double tmp = Math.rando..._java产生随机数的代码

bzoj3224(treap)_cq_phqg的博客-程序员宅基地

3224: Tyvj 1728 普通平衡树Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1844 Solved: 727[Submit][Status]Description您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)

使用IDEA开发RabbitMQ教程系列(四)RabbitMQ的Queue队列和Message详细使用_rabbitmq_queue_messages_Micro麦可乐的博客-程序员宅基地

1、导读本章节主要介绍Queue队列和消息Message的详细介绍。包含构建过程中各参数的详细解释,话不多说我们开始吧2、Queue队列在第一章 初识RabbitMQ 中我们简单介绍了Queue,Queue的主要是作用于存储消息;在之前的样例中大家应该能看到如何构建一个Queue,即queueDeclare方法queueDeclarequeueDeclare 有两个重载方法Queue...._rabbitmq_queue_messages

c语言五子棋mouseclick,MouseClick - AutoHotkey 中文手册_敏大的博客-程序员宅基地

点击或按住鼠标按钮,或滚动鼠标滚轮。注意:Click 命令通常更灵活和易于使用。MouseClick [, WhichButton , X, Y, ClickCount, Speed, D|U, R]参数WhichButton要点击的按钮:Left(默认)、Right、Middle(或仅使用这些名称的首个字母),或鼠标的第四或第五个按钮(X1 或 X2)。例如:MouseClick, X1。此参数..._autohotkey中文手册

QProgressBar进度条_qpecessbar-程序员宅基地

QProgressBar进度条_qpecessbar

TrailRenderer LineRenderer残影_weixin_34354945的博客-程序员宅基地

2019独角兽企业重金招聘Python工程师标准>>> ...

推荐文章

热门文章

相关标签