编程之旅-Day45

目录

Day45-学习内容:

1.剑指Offer

面试题6:从尾到头打印链表

面试题18:删除链表的节点(牛客网无,leetcode)

 2.Leetcode

例1:删除排序链表中的重复元素(保留一个重复元素)

例2:删除链表中重复的结点(重复的结点不保留)

 

 3.华为机试题

例1:字符串排序

例2:查找兄弟单词

例3:数据分类处理


1.剑指Offer

面试题6:从尾到头打印链表

题目描述:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

代码:

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> res;
        if(head==nullptr){
            return res;
        }
        ListNode* pNode=head;
        stack<int> stk;
        while(pNode!=nullptr){
            stk.push(pNode->val);
            pNode=pNode->next;
        }
        while(stk.size()){
            int a=stk.top();
            res.push_back(a);
            stk.pop();
        }
        return res;
    }
};

面试题18:删除链表的节点(牛客网无,leetcode)

题目描述:

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

现有一个链表 -- head = [4,5,1,9],它可以表示为:说明:

  • 链表至少包含两个节点。
  • 链表中所有节点的值都是唯一的。
  • 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
  • 不要从你的函数中返回任何结果。

思路:修改删除节点的指针和值

代码:

class Solution {
public:
    void deleteNode(ListNode* node) {
         node->val = node->next->val;
         node->next = node->next->next;
        
    }
};

 2.Leetcode

例1:删除排序链表中的重复元素(保留一个重复元素)

题目描述:

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

代码:

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==nullptr){
            return nullptr;
        }
        ListNode* pCurrent=head;
        ListNode* pNext=head->next;
        while(pNext!=nullptr){
            if(pNext->val==pCurrent->val){
                pNext=pNext->next;
                pCurrent->next=pNext;
            }
            else{
                pCurrent=pNext;
                pNext=pNext->next;
            }
        }
        return head;
    }
};

例2:删除链表中重复的结点(重复的结点不保留

题目描述:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

代码:

//递归

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        if(pHead==nullptr){
            return nullptr;
        }
        if(pHead!=nullptr&&pHead->next==nullptr){
            return pHead;
        }
        ListNode* pCurrent=pHead;
        if(pHead->val==pHead->next->val){
            pCurrent=pHead->next->next;
            while(pCurrent!=nullptr&&pCurrent->val==pHead->val){
                pCurrent=pCurrent->next;
            }
            return deleteDuplication(pCurrent);
        }
        else{
            pCurrent=pHead->next;
            pHead->next=deleteDuplication(pCurrent);
        }    
        return pHead;
    }
};
//非递归
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* p=new ListNode(0);
        p->next=head;
        head=p;
        ListNode *left,*right;
        while(p->next){
            left=p->next;
            right=left;
            while(right->next&&right->next->val==left->val){
                right=right->next;
            }
            if(left==right){
                p=p->next;
            }
            else{
                p->next=right->next;
            }
        }
        return head->next;
    }
};

 

 3.华为机试题

例1:字符串排序

题目描述:

编写一个程序,将输入字符串中的字符按如下规则排序。

规则 1 :英文字母从 A 到 Z 排列,不区分大小写。

       如,输入: Type   输出: epTy

规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。

     如,输入: BabA   输出: aABb

规则 3 :非英文字母的其它字符保持原来的位置。

     如,输入: By?e   输出: Be?y

样例:

    输入:

   A Famous Saying: Much Ado About Nothing(2012/8).

    输出:

   A  aaAAbc   dFgghh :  iimM   nNn   oooos   Sttuuuy  (2012/8).

思路:没有用排序,直接存储

代码:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){
    string str;
    vector<char> temp;
    while(getline(cin,str)){
        temp.clear();   //置空
        int len=str.size();
        for(int i=0;i<26;i++){   //只对字符串中字母字符忽略大小写排序
            for(int j=0;j<len;j++){
                if(str[j]-'a'==i||str[j]-'A'==i){
                    temp.push_back(str[j]);
                }
            }
        }
        
        for(int i=0,k=0;(i<len)&&(k<temp.size());i++){  //用排序的字符串替换原有字符串中字母字符
            if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
                str[i]=temp[k++];
            }
        }
        cout << str << endl;
    }
    return 0;
}

例2:查找兄弟单词

题目描述:

  

输入描述:

先输入字典中单词的个数,再输入n个单词作为字典单词。
输入一个单词,查找其在字典中兄弟单词的个数
再输入数字n

输出描述:

根据输入,输出查找到的兄弟单词的个数

示例1

输入

3	abc	bca	cab	abc	1

输出

2	bca

注意点:1.字典元素一定要排序

               2.输出两行,第一行表示兄弟单词个数,第二行表示指定的第几个兄弟,没有就不输出

代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool isBrother(string str1,string str2){
    if(str1.size()==str2.size()){
        if(str1==str2){
            return false;
        }
        sort(str1.begin(),str1.end());
        sort(str2.begin(),str2.end());
        if(str1==str2){
            return true;
        }
    }
    return false;
}

int main(){
    int n;
    while(cin>>n){
         vector<string> dict;
         string word,output;
         int index,count=0;
        
         for(int i=0;i<n;i++){
             string s;
             cin>>s;
             dict.push_back(s);
         }
        
        sort(dict.begin(),dict.end());
        
        cin>>word;
        cin>>index;
          
        for(int i=0;i<n;i++){
            if(isBrother(dict[i],word)){
                count++;
                if(count==index){
                    output=dict[i];
                }
            }
        }
        
        if(!dict.empty()){  //可以不用判断
            cout<<count<<endl;
        }
        if(count>=index){ //需要有,不存在就不输出
            cout<<output<<endl;
        }
    }
    return 0;
}

例3:数据分类处理

题目描述:

信息社会,有海量的数据需要分析处理,比如公安局分析身份证号码、 QQ 用户、手机号码、银行帐号等信息及活动记录。  

采集输入大数据和分类规则,通过大数据分类处理程序,将大数据分类输出。

输入描述:

一组输入整数序列I和一组规则整数序列R,I和R序列的第一个整数为序列的个数(个数不包含第一个整数);整数范围为0~0xFFFFFFFF,序列个数不限

输出描述:

从R依次中取出R<i>,对I进行处理,找到满足条件的I<j>: 

I<j>整数对应的数字需要连续包含R<i>对应的数字。比如R<i>为23,I<j>为231,那么I<j>包含了R<i>,条件满足 。 

按R<i>从小到大的顺序:

(1)先输出R<i>; 

(2)再输出满足条件的I<j>的个数; 

(3)然后输出满足条件的I<j>在I序列中的位置索引(从0开始); 

(4)最后再输出I<j>。 

附加条件: 

(1)R<i>需要从小到大排序。相同的R<i>只需要输出索引小的以及满足条件的I<j>,索引大的需要过滤掉 

(2)如果没有满足条件的I<j>,对应的R<i>不用输出 

(3)最后需要在输出序列的第一个整数位置记录后续整数序列的个数(不包含“个数”本身)

序列I:15,123,456,786,453,46,7,5,3,665,453456,745,456,786,453,123(第一个15表明后续有15个整数) 

序列R:5,6,3,6,3,0(第一个5表明后续有5个整数) 

输出:30, 3,6,0,123,3,453,7,3,9,453456,13,453,14,123,6,7,1,456,2,786,4,46,8,665,9,453456,11,456,12,786

说明:

30----后续有30个整数

3----从小到大排序,第一个R<i>为0,但没有满足条件的I<j>,不输出0,而下一个R<i>是3

6--- 存在6个包含3的I<j> 

0--- 123所在的原序号为0 

123--- 123包含3,满足条件 

示例1

输入

15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123
5 6 3 6 3 0

输出

30 3 6 0 123 3 453 7 3 9 453456 13 453 14 123 6 7 1 456 2 786 4 46 8 665 9 453456 11 456 12 786

代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool isMatch(int i, int j){
    string str1=to_string(i);
    string str2=to_string(j);
    int pos=str1.find(str2);
    if(pos==-1){
        return false;
    }
    return true;
}

int main(){
    int n,m;
    while(cin>>n){
        vector<int> I;
        vector<int> R;
        for(int i=0;i<n;i++){   //输入I序列
            int a;
            cin>>a;
            I.push_back(a);
        }
        cin>>m;
        for(int i=0;i<m;i++){   //输入R序列
            int b;
            cin>>b;
            R.push_back(b);
        }
        sort(R.begin(),R.end());
        R.erase(unique(R.begin(),R.end()),R.end());
        
        vector<int> value;
        vector<int> index;
        vector<int> cnt;
        vector<int> Rvalue;
        
        for(int i=0;i<R.size();i++){
            int cnt1=0;
            for(int j=0;j<I.size();j++){
                if(isMatch(I[j],R[i])){
                    cnt1++;
                    value.push_back(I[j]);
                    index.push_back(j);
                }
            }
            if(cnt1!=0){
                cnt.push_back(cnt1);
                Rvalue.push_back(R[i]);
            }
        }
        int j=0;
        cout<<2*value.size()+cnt.size()+Rvalue.size()<<' ';
        for(int i=0;i<cnt.size();i++){
            cout<<Rvalue[i]<<' '<<cnt[i]<<' ';
            while(cnt[i]-->0){
                cout<<index[j]<<' '<<value[j];
                if(i==cnt.size()-1&&cnt[i]==0){
                    cout<<endl;
                }
                else{
                    cout<<' ';
                }
                j++;
            }      
        }
    }
    return 0;
}

输出的第二种写法:
      for(int i=0;i<cnt.size();i++){
            cout<<Rvalue[i]<<' '<<cnt[i]<<' ';
            while(cnt[i]-->0){
                cout<<index[j]<<' '<<value[j]<<' ';
                //if(i==cnt.size()-1&&cnt[i]==0){
                    //cout<<endl;
                //}
                //else{
                    //cout<<' ';
                //}
                j++;
            }      
        }
        cout<<endl;

猜你喜欢

转载自blog.csdn.net/linyuhan3232/article/details/89742546
今日推荐