💫你好,我是辰chen,本文旨在准备考研复试或就业
💫文章题目大多来自于 leetcode,当然也可能来自洛谷或其他刷题平台
💫欢迎大家的关注,我的博客主要关注于考研408以及AIoT的内容
🌟 仅给出C++版代码
以下的几个专栏是本人比较满意的专栏(大部分专栏仍在持续更新),欢迎大家的关注:
💥ACM-ICPC算法汇总【基础篇】
💥ACM-ICPC算法汇总【提高篇】
💥AIoT(人工智能+物联网)
💥考研
💥CSP认证考试历年题解
题目链接:罗马数字转整数
C++版AC代码:
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> m;
m['I'] = 1, m['V'] = 5, m['X'] = 10, m['L'] = 50, m['C'] = 100, m['D'] = 500, m['M'] = 1000;
int ans = 0;
for (int i = 0; i < s.size(); i ++ ){
if (s[i] != 'I' && s[i] != 'X' && s[i] != 'C') ans += m[s[i]];
else if (i == s.size() - 1) ans += m[s[i]];
else{
if (s[i] == 'I' && (s[i + 1] == 'V' || s[i + 1] == 'X'))
ans += m[s[i + 1]] - m[s[i]], i ++;
else if (s[i] == 'X' && (s[i + 1] == 'L' || s[i + 1] == 'C'))
ans += m[s[i + 1]] - m[s[i]], i ++;
else if (s[i] == 'C' && (s[i + 1] == 'D' || s[i + 1] == 'M'))
ans += m[s[i + 1]] - m[s[i]], i ++;
else ans += m[s[i]];
}
}
return ans;
}
};
题目链接:环形链表
C++版AC代码:
快慢指针
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast){
slow = slow -> next;
fast = fast -> next;
if (fast && fast -> next) fast = fast -> next; // 这里光判fast->next会有空指针错
else return false;
if (slow == fast) return true;
}
return false;
}
};
C++版AC代码:
哈希表,存储遍历过的结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
unordered_map<ListNode*, int> m;
ListNode * p = head;
while(p){
if (m.find(p) != m.end()) return true;
m[p] ++;
p = p -> next;
}
return false;
}
};
题目链接:相交链表
C++版AC代码:
双指针
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *l1 = headA, *l2 = headB;
int len1 = 0, len2 = 0;
while (l1) l1 = l1 -> next, len1 ++;
while (l2) l2 = l2 -> next, len2 ++;
l1 = headA, l2 = headB;
if (len1 < len2){ // 保证l1和len1存储的较长的链
ListNode *p = l1;
l1 = l2;
l2 = p;
int tmp = len1;
len1 = len2;
len2 = tmp;
}
int k = len1 - len2;
while (k -- && l1) l1 = l1 -> next;
while (l1){
if (l1 == l2) return l1;
l1 = l1 -> next;
l2 = l2 -> next;
}
return NULL;
}
};
C++版AC代码:
哈希表,先遍历一轮链表A,把A中的所有元素加入哈希表,然后遍历链表B,对于每一个结点去查是否在哈希表中,查到的第一个结点就是第一个公共结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map<ListNode*, int> m;
ListNode *p = headA;
while (p) m[p] ++, p = p -> next;
p = headB;
while (p){
if (m.find(p) != m.end()) return p;
p = p -> next;
}
return NULL;
}
};
题目链接:多数元素
C++版AC代码:
经典的投票算法
class Solution {
public:
int majorityElement(vector<int>& nums) {
int data = nums[0];
int cnt = 1;
for (int i = 1; i < nums.size(); i ++ ){
if (nums[i] == data) cnt ++;
else{
cnt --;
if (cnt == 0){
cnt = 1;
data = nums[i];
}
}
}
return data;
}
};
题目链接:快乐数
C++版AC代码:
三种情况:
1.不断地循环最终发现是快乐数
2.不断地循环(该数不是快乐数)那么必定会有死循环(如果把每次出现的数拉成一个链表,则必定有环)可用哈希
3.不断地循环(该数不是快乐数)超出最大范围,易知本题不存在这种情况(不可能循环到正无穷)
class Solution {
public:
bool isHappy(int n) {
unordered_map<int, int> m;
while (n != 1){
int tmp = n, num = 0;
while (tmp){
int d = tmp % 10;
num += d * d;
tmp /= 10;
}
n = num;
if (m.find(num) != m.end()) return false;
m[num] = 1;
}
return true;
}
};