zoukankan      html  css  js  c++  java
  • 剑指offer 37-42

    37.

    输入一棵二叉树,判断该二叉树是否是平衡二叉树。

    思路:
    1.平衡二叉树的定义为,当为空树或左右子树高度小于等于1,同时左右子树都要为平衡二叉树。
    2.先分别求出左右子树的高度,比较高度差,如果高度差大于1,直接返回false,如果高度不大于1,那么则递归判断左右子树是否为平衡二叉树。

    代码贴下:

    class Solution {
    public:
        int getDepth(TreeNode* pRoot)
        {
            if(!pRoot)
            {
                return 0;
            }
            if(!pRoot->left&&!pRoot->right)
            {
                return 1;
            }
            int len1=getDepth(pRoot->left)+1;
            int len2=getDepth(pRoot->right)+1;
            return len1>len2?len1:len2;
        }
        bool IsBalanced_Solution(TreeNode* pRoot) {
            if(!pRoot)
            {
                return true;
            }
            int llength=getDepth(pRoot->left);
            int rlength=getDepth(pRoot->right);
            if(llength>rlength+1||rlength>llength+1)
            {
                return false;
            }else
            {
                return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
            }
                
        }
    };
    

    题目描述
    一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。


    思路:
    1.先按升序排序(c++内置函数sort),那么如果只要是两个相同的数,都会排列在一块,假设原数组为2,4,5,5,6,4,经过排序后就变为2,4,4,5,5,6,出现次数为2的数,就会靠在一起,这时我们需要遍历一下数组,比较相邻的数字是否相等,如果相等就将遍历的指针,加2,不相等就赋值第一个数,指针加1,再碰到个不相等的,就再赋值给第二个数,break,完成任务了。

    代码如下:

    class Solution {
    public:
        void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
            sort(data.begin(),data.end());
            int p1=0;
            int i=0;
            while(i<data.size())
            {
                if(data[i]!=data[i+1])
                {
                    if(p1==0)
                    {
                          *num1=data[i];
                        p1=1;
                    }else
                    {
                        *num2=data[i];
                    }
                  
                    i++;
                }else
                {
                    i+=2;
                }
                
            } 
        }
    };
    

    题目描述
    小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列?
    Good Luck!

    思路:
    1.连续的序列的和,那么等差数列和的n(a1+an)/2公式肯定是少不了的。
    2.双指针,相等于一个滑动窗口,不断的扩大和缩小。直接看代码吧,比较简单。

    class Solution {
    public:
        vector<vector<int> > FindContinuousSequence(int sum) {
            vector<vector<int>> buffer;
        
            int i=1;
            int j=2;
            int res=0;
            while(i<j)
            {
              
                res=((i+j)*(j-i+1))/2;
                if(res==sum)
                {
                          vector<int> inside;
                    for(int z=i;z<=j;z++)
                    {
                        inside.push_back(z);
                    }
                    buffer.push_back(inside);
                    i++;
                }else if(res>sum)
                {
                    i++;
                }else if(res<sum)
                {
                    
                   j++;
                }
                  
            }
            return buffer;
            
        }
    };
    

    题目描述
    输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。

    思路:
    双重循环,遍历,就完事了。

    代码如下;

    class Solution {
    public:
        vector<int> FindNumbersWithSum(vector<int> array,int sum) {
            vector<int> res;
            for(int i=0;i<array.size();i++)
            {
                for(int j=i+1;j<array.size();j++)
                {
                    if(sum-array[i]==array[j])
                    {
                          res.push_back(array[i]);
                        res.push_back(array[j]);
                            return res;
                    }
                }
            }
            return res;
                
            
        }
    };
    

    题目描述
    汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!

    思路:
    左移多少位,相等于将前几位,拼接到后面去了,抱着这种思路去写,当然不止我这种写法,我这种删除的,也可以用截取的,看个人吧。

    代码如下:

    class Solution {
    public:
        string LeftRotateString(string str, int n) {
            if(n<0) return NULL;
            if(n==0)return str;
            string str1=str.substr(0,n);
            str.erase(0,n);
          str+=str1;
            return str;
            
        }
    };
    

    题目描述
    牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?

    思路:
    以空格分割开来,成为一个数组,之后再调换,最后再遍历一下,拼接起来,这里用的是java写的,c++对字符串感觉没java友好。

    public class Solution {
        public String ReverseSentence(String str) {
            if(str==null)
            {
                return null;
            }
            if(str.trim().equals(""))
            {
                return str;
            }
            String[] a=str.split(" ");
            String res="";
            int j=a.length-1;
            int i=0;
            while(i<j)
            {
                String tmp=a[i];
                a[i]=a[j];
                a[j]=tmp;
                i++;
                j--;
            }
            for(int x=0;x<a.length;x++)
            {   
                res+=a[x];
                if(x!=a.length-1)
                {
                    res+=" ";
                }
            }
            return res;
        }
    }
    
    
  • 相关阅读:
    Ubuntu配置sublime text 3的c编译环境
    ORA-01078错误举例:SID的大写和小写错误
    linux下多进程的文件拷贝与进程相关的一些基础知识
    ASM(四) 利用Method 组件动态注入方法逻辑
    基于Redis的三种分布式爬虫策略
    Go语言并发编程总结
    POJ2406 Power Strings 【KMP】
    nyoj 会场安排问题
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    Java的String、StringBuffer和StringBuilder的区别
  • 原文地址:https://www.cnblogs.com/YenKoc/p/12779959.html
Copyright © 2011-2022 走看看