算法刷題5-27 找到一個數組中出現一次的數字, 其他數字出現均爲偶數次

 

找到一個數組中出現一次的數字, 其他數字出現均爲偶數次

input【1,1,2,3,3,4,4,6,7,6,7】

out: 2

算法思路:

1^1=0

0^1 =1

0^1^2^1=2

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector <int> v1 = {1,1,3,3,4,4,2};
    int res = 0;
    for(int i= 0 ;i < v1.size();i++)
    {
        res = res^v1[i];
    }
	cout<<res;
    return res;
}

數組中值出現了一次的數字

給定一個數字arr,其中只有有兩個數字出現了奇數次,其它數字都出現了偶數次,按照從小到大順序輸出這兩個數。

輸入描述


 

輸入包含兩行,第一行一個整數n,代表數組arr的長度,第二行n個整數,代表數組arr,arr[i]爲32位整數。

輸出描述

輸出出現奇數次的兩個數,按照從小到大的順序。

示例1

輸入

4
1 1 2 3

輸出

2 3

示例2

輸入

6
11 22 11 23 23 45

輸出

22 45

 

#include <iostream>
#include <vector>
#include <map>
using namespace std;


int main() {
    
    vector<int> v1 ={1,1,2,3};
    vector<int> v2;
    int len = v1.size();
    map<int,int> ma1;
    for(int i=0; i < len;i++)
    {
        ma1[v1[i]]++;
    }
    map<int,int> ::iterator it = ma1.begin();
    while(it !=  ma1.end())
    {
        if(1 == it->second)
        {
            v2.push_back(it->first);
            it++;
        }
        
    }

     if(v2[0]>v2[1])
     {
              cout<<v2[0]<<" "<<v2[1]<<endl;
     }else{
              cout<<v2[1]<<" "<<v2[0]<<endl;
     }  

   
        
   //cout << "Hello World!" << endl;
}

 

 

next_permutation函數

    組合數學中經常用到排列,這裏介紹一個計算序列全排列的函數:next_permutation(start,end),和prev_permutation(start,end)。這兩個函數作用是一樣的。

next_permutation(start,end)求的是當前排列的下一個排列

prev_permutation(start,end)求的是當前排列的上一個排列。

至於這裏的“前一個”和“後一個”,我們可以把它理解爲序列的字典序的前後,嚴格來講,就是對於當前序列pn,他的下一個序列pn+1滿足:不存在另外的序列pm,使pn<pm<pn+1.

 

對於next_permutation函數,其函數原型爲:

     #include <algorithm>

     bool next_permutation(iterator start,iterator end)

當前序列不存在下一個排列時,函數返回false,否則返回true

 

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;
    v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	do  
    {  
         cout<<v1[0]<<" "<<v1[1]<<" "<<v1[2]<<" "<<v1[3]<<endl;  
    }while(next_permutation(v1.begin(),v1.end()));  
  
   return 0;
}

面試題25. 合併兩個排序的鏈表

難度簡單29收藏分享切換爲英文關注反饋

輸入兩個遞增排序的鏈表,合併這兩個鏈表並使新鏈表中的節點仍然是遞增排序的。

示例1:

輸入:1->2->4, 1->3->4
輸出:1->1->2->3->4->4

限制:

0 <= 鏈表長度 <= 1000

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
    {
        ListNode* head = new ListNode(0);
        if(nullptr == l1 && nullptr !=l2) return l2;
        if(nullptr != l1 && nullptr ==l2) return l1;
        ListNode* p1 = head;

        while(l1 || l2)
        {
            if(!l1||(l1 && l2 &&(l1->val >= l2->val)))
            {
                p1->next = new ListNode(l2->val);
                p1=p1->next;
                l2=l2->next;
            }else if(!l2||(l1 && l2 &&(l1->val < l2->val)))
            {
                p1->next = new ListNode(l1->val);
                p1=p1->next;
                l1=l1->next;
            }
        
        }
        return head->next;

    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章