学渣带你刷Leetcode271. 字符串的编码与解码

题目描述

请你设计一个算法,可以将一个 字符串列表 编码成为一个 字符串。这个编码后的字符串是可以通过网络进行高效传送的,并且可以在接收端被解码回原来的字符串列表。

1 号机(发送方)有如下函数:

string encode(vector<string> strs) {
  // ... your code
  return encoded_string;
}
2 号机(接收方)有如下函数:

vector<string> decode(string s) {
  //... your code
  return strs;
}
1 号机(发送方)执行:

string encoded_string = encode(strs);
2 号机(接收方)执行:

vector<string> strs2 = decode(encoded_string);
此时,2 号机(接收方)的 strs2 需要和 1 号机(发送方)的 strs 相同。

请你来实现这个 encode 和 decode 方法。

注意:

因为字符串可能会包含 256 个合法 ascii 字符中的任何字符,所以您的算法必须要能够处理任何可能会出现的字符。
请勿使用 “类成员”、“全局变量” 或 “静态变量” 来存储这些状态,您的编码和解码算法应该是非状态依赖的。
请不要依赖任何方法库,例如 eval 又或者是 serialize 之类的方法。本题的宗旨是需要您自己实现 “编码” 和 “解码” 算法。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/encode-and-decode-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

白话题目:
TLV通信协议

TLV协议的基本格式如下:

 

其中,Tag占2个字节,是报文的唯一标识;Length占4个字节,表示Value字段的长度;Value字段的数据是需要传输的数据,长度由Length字段表示。

似乎没啥用,还整得稀蒙。

不过领导的指数和精神还是要体会的,我们拿出一些要传输的信息指明后续要传的,之后再编好码,最后再解码的时候按照这些规则把之前编进去的单词解出来。

算法:

申请一个大大大大的字符数组,存字符串,

(0)二位数组的输入

(1)利用一个神奇的函数 int sprintf(char *str, const char *format, B),把B按照format的格式存入str的位置

memcpy(目标, 源, 这么些个);

(2)有个这个神器,我们就将

编码:单词个数%   第一个要编写的单词的个数%单词   第二个要编写的单词的个数%单词  。。。。。。。

(3)解码:识别出不是%的,那就是单词的个数,在掠过%,之后的一个字符转数字就是单词的个数,掠过%存单词

那种单词长度是大于10的,存入字符数组就如实存,比如1百1十7,存入里面占3个位置1 1 7 

(4)正常的输出

 

详细解释关注 B站  【C语言全代码】学渣带你刷Leetcode 不走丢 https://www.bilibili.com/video/BV1C7411y7gB

C语言完全代码

#include <stdio.h>
#include <stdlib.h>

#define MAXLEN 50000


char* encode(char** strs, int strsSize) {
	char tmp[MAXLEN];
	int count;
	sprintf(tmp, "%d", strsSize);  //strsSize到tmp中,  3个
	count = strlen(tmp);
	printf("---%d\n",count);
	puts(tmp);
	tmp[count++] = '%';       //tmp  ====3%
	puts(tmp);
	int i, len;
	for (i = 0; i < strsSize; i++) {
		len = strlen(strs[i]);      //单词apple的数量 5
		printf("第二个count---%d\n",count);
		sprintf(tmp + count, "%d", len);  //  放到哪个位置呢
		puts(tmp);
		count = strlen(tmp);
		tmp[count++] = '%';
		tmp[count] = '\0';
		strcat(tmp + count, strs[i]);
		count = strlen(tmp);
		printf("放入单词后---%d\n",count);
	}
	char * ret = (char*)malloc(count + 1);
	memcpy(ret, tmp, count + 1);
	puts(ret);
    return ret;

}


char** decode(char* s, int* returnSize) {
	int strsize, count, len;
	strsize = 0;
	while (*s != '%') {
		strsize = strsize * 10 + *s - '0';
		s = s + 1;  //往后挪一位
	}   //变成10进制的数

	s = s + 1;

	if (strsize == 0) {
		*returnSize = 0;
		return NULL;
	}

	count = 0;
	char **ret = (char**)malloc(sizeof(char*)*strsize);
	while (count < strsize) {
		len = 0;
		while (*s != '%') {
			len = len * 10 + *s - '0';
			s = s + 1;
		}
		s = s + 1;
		ret[count] = (char*)malloc(len + 1);
		memcpy(ret[count], s, len);
		ret[count++][len] = '\0';
		s = s + len;
	}
	*returnSize = count;
	return ret;

}



// Your functions will be called as such:
// char* s = encode(strs, strsSize);
// decode(s, &returnSize);



int main()
{
    char *test[3]= {{"apple"},{"orange"},{"banana"}};
    char **strs = test;
    char *s= encode(strs, 3);

    int returnSize=0;
    puts(s);
    char **result=  decode(s, &returnSize);
    int i=0;
    for(i=0;i<returnSize;i++)
    {
        char *temp=result[i];
        puts(temp);
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值