算法
xuri8888
这个作者很懒,什么都没留下…
展开
-
假设有一个排好序的数组,数字都是两个两个出现的,只有一个是单独出现的,求这个数
class Solution{public:int FindNumAppearOnce(const vector<int>& nums){ if(nums.empty()) return 0; int num=0; for(auto i:nums) num ^= i; return num;}};...原创 2018-07-31 17:04:30 · 367 阅读 · 0 评论 -
有两个链表,怎么求交点
struct ListNode{int val;ListNode* next;ListNode(int x):val(x),next(nullptr);};class Solution{public:ListNode* findNode(ListNode* node1,ListNode* node2){ if(node1==nullptr || node2==null...原创 2018-07-31 16:46:40 · 326 阅读 · 0 评论 -
求一个数组的最长连续子序列
分析:如果允许O(nlogn)的复杂度,那么可以先排序,可是本题要求O(n)。由于序列里的元素是无序的,又要求O(n),首先要想到用哈希表。用一个哈希表unordered_map<int,bool> used 记录每个元素是否使用,对每个元素,以该元素为中心,往左右扩张,直到不连续为止,记录下最长的长度。class Solution {public:int ...原创 2018-07-31 16:33:23 · 1756 阅读 · 0 评论 -
判断一个数是不是回文数
class Solution{public: bool isPalindrome(string s) { auto left=s.begin(),right=prev(s.end()); while(left<right) { if(*left != *right) ...原创 2018-07-31 16:17:39 · 629 阅读 · 0 评论 -
求二叉树的深度
//树的深度int TreeDepth(BTree* root) { int nLeft, nRight; if(root == NULL)//必不可少的条件,递归的出口 return 0; nLeft = TreeDepth(root->lchild); nRight = TreeDepth(root->r...原创 2018-07-31 16:08:31 · 365 阅读 · 0 评论 -
Remove Duplicates from Sorted List 删除单项链表中重复的节点 (1 2 2 3 3 9) -> (1 2 3 9)
class Solution {public: ListNode *deleteDuplicates(ListNode* head) { if(head==nullptr) return head; for(ListNode* prev=head,*cur=head->next; cur; cur=prev->...原创 2018-07-31 15:37:53 · 185 阅读 · 0 评论 -
char*s1, const char *s2,删除s1中s2出现过的字符
class Solution {public: char* delChar(char* s1,char* s2) { if(s1==nullptr) return nullptr; if(s2==nullptr) retrun s1; const int table...原创 2018-07-27 09:49:49 · 1119 阅读 · 0 评论 -
C++后台开发--算法方面
1、逆转单链表;https://blog.csdn.net/m0_38099899/article/details/809572352、链表带环检测以及环入口;https://blog.csdn.net/m0_38099899/article/details/809572553、链表排序(nlogn不退化); https://blog.csdn.net/one_piece...原创 2018-07-08 11:23:36 · 473 阅读 · 0 评论 -
链表带环检测以及环入口
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public:ListNode* detectCycle(ListNode* head){ ListN...原创 2018-07-08 10:25:45 · 317 阅读 · 0 评论 -
逆转单链表
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* ReverseList(ListNode* pHead) { if(pHead == NULL || pH...原创 2018-07-08 10:24:24 · 324 阅读 · 0 评论 -
C++后台开发最常问到的面试题
一、C/C++方面1、说说C++中的多态及其实现;https://blog.csdn.net/u010864559/article/details/70242984 https://blog.csdn.net/menyangyang/article/details/247792573、带虚函数与否的空类大小; 不带虚函数:sizeof为1;带虚函数:sizeof为4(3...转载 2018-09-24 13:06:45 · 2695 阅读 · 0 评论