Data Structure Linked List: Function to check if a singly linked list is palindrome

本文介绍了一种高效检查单链表是否为回文的方法,通过遍历链表找到中间节点并反转后半部分,再比较前后两部分来判断是否为回文。提供两种实现方式:一种使用额外空间,另一种采用递归方式。

http://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/

这里的reverse可以reverse整个list,这样空间需求就是O(n),不如这个网页写的O(1)的方法

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <string>
 7 #include <fstream>
 8 #include <map>
 9 #include <set>
10 using namespace std;
11 
12 struct node {
13     int data;
14     node *next;
15     node() : data(0), next(NULL) { }
16     node(int d) : data(d), next(NULL) { }
17 };
18 
19 void push(node* &head, int k) {
20     node *new_node = new node(k);
21     new_node->next = head;
22     head = new_node;
23 }
24 
25 void print(node* head) {
26     while (head) {
27         cout << head->data << " ";
28         head = head->next;
29     }
30     cout << endl;
31 }
32 
33 void reverselist(node *&head) {
34     if (!head) return;
35     node *cur = head;
36     node *next = head->next;
37     if (!next) return;
38     reverselist(next);
39     cur->next->next = cur;
40     cur->next = NULL;
41     head = next;
42 }
43 
44 bool compare(node *first, node* second) {
45     while (first && second) {
46         if (first->data != second->data) return false;
47         first = first->next;
48         second = second->next;
49     }
50     return first == NULL && second == NULL;
51 }
52 
53 bool palin(node *head) {
54     if (!head || !head->next) return true;
55     node *p, *q, *pre;
56     p = q = pre = head;
57     node *midnode = NULL;
58     while (q && q->next) {
59         q = q->next->next;
60         pre = p;
61         p = p->next;
62     }
63     if (q) { //odd number
64         midnode = p;
65         p = p->next;
66     }
67     node *second = p;
68     pre->next = NULL;
69     reverselist(second);
70     bool ans = compare(head, second);
71     reverselist(second);
72     if (midnode) {
73         pre->next = midnode;
74         midnode->next = second;
75     }
76     else pre->next = second;
77     return ans;
78 }
79 
80 int main() {
81     node *head = NULL;
82     push(head, 1);
83     push(head, 2);
84     push(head, 3);
85     push(head, 3);
86     push(head, 2);
87     push(head, 1);
88     if (palin(head)) cout << "yes" << endl;
89     else cout << "no" << endl;
90     print(head);
91     return 0;
92 }

 recursive的法子很巧

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <string>
 7 #include <fstream>
 8 #include <map>
 9 #include <set>
10 using namespace std;
11 
12 struct node {
13     int data;
14     node *next;
15     node() : data(0), next(NULL) { }
16     node(int d) : data(d), next(NULL) { }
17 };
18 
19 void push(node* &head, int k) {
20     node *new_node = new node(k);
21     new_node->next = head;
22     head = new_node;
23 }
24 
25 void print(node* head) {
26     while (head) {
27         cout << head->data << " ";
28         head = head->next;
29     }
30     cout << endl;
31 }
32 
33 bool palinhelp(node *&left, node *right) {
34     if (right == NULL) return true;
35     bool palin1 = palinhelp(left, right->next);
36     if (!palin1) return false;
37     bool palin2 = right->data == left->data;
38     left = left->next;
39     return palin2;
40 }
41 
42 bool palin(node *head) {
43     return palinhelp(head, head);
44 }
45 
46 int main() {
47     node *head = NULL;
48     push(head, 1);
49     push(head, 2);
50     push(head, 3);
51     push(head, 3);
52     push(head, 2);
53     push(head, 1);
54     if (palin(head)) cout << "yes" << endl;
55     else cout << "no" << endl;
56     print(head);
57     return 0;
58 }

 

转载于:https://www.cnblogs.com/yingzhongwen/p/3655620.html

一、数据采集层:多源人脸数据获取 该层负责从不同设备 / 渠道采集人脸原始数据,为后续模型训练与识别提供基础样本,核心功能包括: 1. 多设备适配采集 实时摄像头采集: 调用计算机内置摄像头(或外接 USB 摄像头),通过OpenCV的VideoCapture接口实时捕获视频流,支持手动触发 “拍照”(按指定快捷键如Space)或自动定时采集(如每 2 秒采集 1 张),采集时自动框选人脸区域(通过Haar级联分类器初步定位),确保样本聚焦人脸。 支持采集参数配置:可设置采集分辨率(如 640×480、1280×720)、图像格式(JPG/PNG)、单用户采集数量(如默认采集 20 张,确保样本多样性),采集过程中实时显示 “已采集数量 / 目标数量”,避免样本不足。 本地图像 / 视频导入: 支持批量导入本地人脸图像文件(支持 JPG、PNG、BMP 格式),自动过滤非图像文件;导入视频文件(MP4、AVI 格式)时,可按 “固定帧间隔”(如每 10 帧提取 1 张图像)或 “手动选择帧” 提取人脸样本,适用于无实时摄像头场景。 数据集对接: 支持接入公开人脸数据集(如 LFW、ORL),通过预设脚本自动读取数据集目录结构(按 “用户 ID - 样本图像” 分类),快速构建训练样本库,无需手动采集,降低系统开发与测试成本。 2. 采集过程辅助功能 人脸有效性校验:采集时通过OpenCV的Haar级联分类器(或MTCNN轻量级模型)实时检测图像中是否包含人脸,若未检测到人脸(如遮挡、侧脸角度过大),则弹窗提示 “未识别到人脸,请调整姿态”,避免无效样本存入。 样本标签管理:采集时需为每个样本绑定 “用户标签”(如姓名、ID 号),支持手动输入标签或从 Excel 名单批量导入标签(按 “标签 - 采集数量” 对应),采集完成后自动按 “标签 - 序号” 命名文件(如 “张三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值