剑桥offer(21~30)

21.题目描述

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
 
还不会

22.题目描述

数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
 
class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
         map<int,int> m;
        map<int,int>::iterator it;
            for(int i=0;i<numbers.size();i++)
                m[numbers[i]]++;
            for(it=m.begin();it!=m.end();it++)
            {
                if(it->second>numbers.size()/2)
                return it->first;
            }
        return 0;
    }
};
View Code

23.题目描述

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        vector<int>r;
        if(input.empty()||k>input.size()) return r;
        sort(input.begin(),input.end());
        r.assign(input.begin(),input.begin()+k);
        return r;
    }
};
View Code

24.题目描述

HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?(子向量的长度至少是1)
class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
    
        if(array.size() == 0)return 0;
        
        int sum = array[0];
        int tmp = array[0];
        
        for (int i=1;i<array.size();i++){
            tmp = (tmp>0)?tmp+array[i]:array[i];
            sum = tmp>sum?tmp:sum;
        }
        return sum;
        
    }
};
View Code

25.题目描述

求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数。
class Solution {
public:
    int NumberOf1Between1AndN_Solution(int n)
    {
        
     int count = 0;
        for(int i=0; i<=n; i++){
            int temp = i;
            //如果temp的任意位为1则count++
            while(temp){
                if(temp%10 == 1){
                    count++;
                }
                temp /= 10;
            }
        }
        return count;   
    }
};
View Code

26.题目描述

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
class Solution {

public:
  
  static bool cmp(int a,int b){
         string A="";
         string B="";
         A+=to_string(a);
         A+=to_string(b);
         B+=to_string(b);
         B+=to_string(a);
          
         return A<B;
     }
     string PrintMinNumber(vector<int> numbers) {
         string  answer="";
         sort(numbers.begin(),numbers.end(),cmp);
         for(int i=0;i<numbers.size();i++){
             answer+=to_string(numbers[i]);
         }
         return answer;
     }
    
};
View Code

27.题目描述

把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
http://www.cnblogs.com/yuguangyuan/p/5937702.html
class Solution {
public:
    int GetUglyNumber_Solution(int index) {
        
    if (index <= 0)return 0;

    if (index == 1){
        return 1;
    }
    int m2=0,m3=0,m5=0;
     vector<int>a(index);
        a[0]=1;
    for(int i=1;i<index;i++){
        a[i]=min(a[m2]*2,min(a[m3]*3,a[m5]*5));
        if(a[i]==a[m2]*2)m2++;
        if(a[i]==a[m3]*3)m3++;
        if(a[i]==a[m5]*5)m5++;
    }
        return a[index-1];
    }
        
};
View Code

28.题目描述

在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置
class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        
        
        char s[256] = {0};
        int i=0;
        while(str[i]!=''){
           
            s[str[i]]++;
            i++;
        }
         i=0;
        while(str[i]!=''){
            if(1==s[str[i]]){
                return i;
            }
            i++;
        }
        return -1;
        
    }
};
View Code

29.题目描述

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007 
待做

30.题目描述

输入两个链表,找出它们的第一个公共结点。
思路:从后面开始,当不等的时候,就跳出循环,找到相同的最后一个节点。
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) {
        
  
            if (!pHead1 || !pHead2) return NULL;

        stack<ListNode *>p1;
        stack<ListNode *>p2;
        ListNode* node = NULL;

        while (pHead1){
            p1.push(pHead1);
            pHead1 = pHead1->next;
        }
        while (pHead2){
            p2.push(pHead2);
            pHead2 = pHead2->next;
        }
        while ((p1.size()>0 && p2.size()>0) && p1.top() == p2.top()){

            node = p1.top();
            p1.pop();
            p2.pop();
            
        }
        return node;
    }

};
View Code
 
原文地址:https://www.cnblogs.com/yuguangyuan/p/6065054.html