编程之旅-Day23

52 篇文章 0 订阅
48 篇文章 0 订阅

目录

Day23-学习内容:

1.剑指Offer

面试题64:求1+2+...+n

面试题28:对称的二叉树

 2.Leetcode

例1:字符串模式匹配

例2:数组的排列

 


1.剑指Offer

面试题64:求1+2+...+n

题目描述:求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

考察点:发散思维,突破常规敢于尝试和挑战.

思路:可通过函数指针、模版类型、虚函数、构造函数多种方法实现,将数值变量n转换成布尔值(!!n),可将非零n转换为true,零转换为false.

代码:

class Solution {
public:
    int Sum_Solution(int n) {
        int ans=n;
        ans&&(ans+=Sum_Solution(n-1));
        return ans;
    }
};

 

面试题28:对称的二叉树

题目描述:请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

思路:比较一棵树的前序遍历序列是否等于对称的前序遍历序列,来判断该树是否对称。

代码:

class Solution {
public:
    bool isSymmetrical(TreeNode* pRoot)
    {
        return isSymmetrical(pRoot,pRoot);
    }
    bool isSymmetrical(TreeNode* pRoot1,TreeNode* pRoot2)
    {
        if(pRoot1==nullptr&&pRoot2==nullptr) return true;
        if(pRoot1==nullptr||pRoot2==nullptr) return false;
        if(pRoot1->val!=pRoot2->val) return false;
        return isSymmetrical(pRoot1->left,pRoot2->right)&&isSymmetrical(pRoot1->right,pRoot2->left);
    }
};

 

 2.Leetcode

例1:字符串模式匹配

题目描述:

Implement wildcard pattern matching with support for'?'and'*'. 

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

代码:

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        int i=0;
        int j=0;
        int indexstar=-1,indexmatch=0;
        while(s[i]!='\0'){
            if(p[j]!='\0'&&(s[i]==p[j]||p[j]=='?')){
                i++;
                j++;
            }
            else if(p[j]!='\0'&&p[j]=='*'){//有*的情况下,先忽略*,看不用*能走多远
                indexmatch=i;
                indexstar=j;  //把*的位置记下来
                j++;
            }
            else if(indexstar!=-1){   //上面两种都不符合,那就值靠*来解决了
                j=indexstar++;
                indexmatch++;
                i=indexmatch;   //从s后面一个开始,相当于第i个用*表示了
            }
            else{
                return false;   //没得救了就返回false吧
            }
        }
        while(p[j]!='\0'&&p[j]=='*') j++;  //去除多余的*号
        return p[j]=='\0';
    }
};

 

例2:数组的排列

题目描述:

Given a collection of numbers, return all possible permutations. 

For example,
[1,2,3]have the following permutations:
[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1]. 

 

思路:递归和分治法实现

代码:

class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {
        vector<vector<int> >  res;
        permutation(res,num,0);
        return res;
    }
    void permutation(vector<vector<int>> &res, vector<int> &num, int n){
        if(n==num.size()-1){
            res.push_back(num);
        }
        for(int i=n;i<num.size();i++){
            swap(num[i],num[n]);
            permutation(res,num,n+1);
            swap(num[i],num[n]);
        }
    }
};

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值