1. 程式人生 > >劍指offer題目

劍指offer題目

1.二叉樹映象


python:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回映象樹的根節點
    def Mirror(self, root):
        if root == None:
            return 
        self.Mirror(root.left)
        self.Mirror(root.right)
        root.left,root.right = root.right,root.left

C++:

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) {
    if (pRoot) {
        Mirror(pRoot->left);
        Mirror(pRoot->right
); TreeNode *temp=pRoot->left; pRoot->left=pRoot->right; pRoot->right=temp; } } };

2.判斷連結串列中是否包含環,輸出環入口節點,否則輸出NULL

思路:
該題使用一種巧妙的方法,使用兩個步長分別是1和2的指標


python

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
# self.next = None class Solution: def EntryNodeOfLoop(self, pHead): if pHead== None or pHead.next == None: return None fast = slow = pHead while(fast and fast.next): slow = slow.next fast = fast.next.next if slow == fast: fast = pHead while(fast!=slow): fast = fast.next slow = slow.next return fast return None

C++

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead)
    {
        if ((pHead->next==NULL)||(pHead==NULL)) return NULL;
        ListNode* fast = pHead;
        ListNode* slow = pHead;
        bool isCycle = false;

        while((fast!=NULL)&&(slow!=NULL)){
            fast=fast->next;
            slow=slow->next;
            if (fast->next==NULL)return NULL;
            fast=fast->next;
            if(fast==slow){isCycle=true;break;}
        }
        if(!isCycle)return NULL;
        fast=pHead;
        while(fast!=slow){
            fast=fast->next;
            slow=slow->next;
        }
        return fast;
    }
};

3.刪除連結串列重複節點

python

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def deleteDuplication(self, pHead):
        # write code here
        if(pHead==None):
            return None
        if(pHead.next==None):
            return pHead
        val = pHead.val
        p = pHead.next
        if(val!=p.val):
            pHead.next=self.deleteDuplication(p)
            return pHead
        else:
            while (p and (val==p.val)):
                p=p.next
            return self.deleteDuplication(p)

c++

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        if (!pHead) return 0;
        if (!pHead->next) return pHead;

        int val = pHead->val;
        ListNode* p = pHead->next;

        if (p->val != val) {
            pHead->next = deleteDuplication(p);
            return pHead;
        } else {
            while (p && p->val == val) p = p->next;
            return deleteDuplication(p);
        }
    }
};

4.反轉連結串列

使用python array與c++ STL自帶的reverse函式即可


python

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回從尾部到頭部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        ret = []
        head = listNode
        while(head):
            ret.append(head.val)
            head = head.next
        ret.reverse()
        return ret

c++

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> myvector;
        while(head!=NULL){myvector.push_back(head->val);head=head->next;}
        reverse(myvector.begin(),myvector.end()); 
        return myvector;
    }
};

5.斐波那契數列


python

# -*- coding:utf-8 -*-
class Solution:
    def Fibonacci(self, n):
        # write code here
        if n==0:
            return 0;
        elif n==1:
            return 1;
        else:
            a0=0;
            a1=1;
            for i in range(n-1):
                a2=a0+a1;
                a0=a1;
                a1=a2;
            return a2

C++

class Solution {
public:
    int Fibonacci(int n) {
        if (n ==0) return 0;
        else if(n == 1) return 1;
        else{
            int a0 = 0;
            int a1 = 1;
            int a2;
            for (int i=0;i<n-1;i++){
                a2 = a0 + a1;
                a0 = a1;
                a1 = a2;
            }
            return a2;
        }
    }
};

6.青蛙跳臺階,一次跳1或2階,共n階,幾種跳法

思路:
f(1)=1
f(2)=2

f(n)=f(n-1)+f(n-2)
注意:遞迴效率低,重複呼叫函式,儘量使用迭代。


python

# -*- coding:utf-8 -*-
class Solution:
    def jumpFloor(self, number):
        # write code here
        if number==1:
            return 1
        elif number==2:
            return 2
        else:
            a0=1
            a1=2
            for i in range(number-2):
                a2=a0+a1
                a0=a1
                a1=a2
            return a2

c++

class Solution {
public:
    int jumpFloor(int number) {
        if (number==0)return 0;
        else if (number==1)return 1;
        else if (number==2)return 2;
        else
            return jumpFloor(number-1)+jumpFloor(number-2);
    }
};

7.變態跳臺階,一次可以跳1~n階

思路:
f(1)=1
f(2)=f(1)+1
f(3)=f(2)+f(1)+1=2*f(2)

f(n)=2*f(n-1)


python

# -*- coding:utf-8 -*-
class Solution:
    def jumpFloorII(self, number):
        # write code here
        a0=1
        for i in range(number-1):
            a0=a0*2
        return a0

C++

class Solution {
public:
    int jumpFloorII(int number) {
        int a0 = 1;
        for(int i=0;i<number-1;i++){
            a0=a0*2;
        }
        return a0;
    }
};

8.矩陣覆蓋

用n個2*1的小矩形無重疊地覆蓋一個2*n的大矩形,總共有多少種方法
思路:依舊斐波那契


python

# -*- coding:utf-8 -*-
class Solution:
    def rectCover(self, number):
        # write code here
        if number==1:
            return 1
        elif number==2:
            return 2
        elif number==0:
            return 0
        else:
            a0=1
            a1=2
            for i in range(number-2):
                a2=a0+a1
                a0=a1
                a1=a2
            return a2

c++

class Solution {
public:
    int rectCover(int number) {
        if(number==1)return 1;
        else if(number==2)return 2;
        else if(number==0)return 0;
        else{
            int a0=1;
            int a1=2;
            int a2;
            for(int i=0;i<number-2;i++){
                a2=a0+a1;
                a0=a1;
                a1=a2;
            }
            return a2;
        }
    }
};

9.字串轉整數

將一個字串轉換成一個整數(實現Integer.valueOf(string)的功能,但是string不符合數字要求時返回0),要求不能使用字串轉換整數的庫函式。 數值為0或者字串不是一個合法的數值則返回0


python

# -*- coding:utf-8 -*-
class Solution:
    def StrToInt(self, s):
        # write code here
        symbol=1
        num=0
        if s=='':
            return 0
        for i in s:
            if i=='+':
                symbol=1
            elif i=='-':
                symbol=-1
            elif i>'0' and i<'9':
                num=num*10+int(i)
            else:
                return 0
        return symbol*num

c++

class Solution {
public:
    int StrToInt(string str) {
        int symbol = 1;
        int num=0;
        for (int i=0;i<str.size();i++){
            if (str[i]=='+')symbol=1;
            else if(str[i]=='-')symbol=-1;
            else if((str[i]>'0')&&(str[i]<'9'))num=num*10+(str[i]-'0');
            else return 0;
        }
        return symbol*num;
    }
};

10.平衡二叉樹的判斷

思路:BST的定義為|height(lefttree)−height(righttree)|<=1且兩個子樹均為BST,原問題拆分為計算樹高度和判斷高度差


python

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def IsBalanced_Solution(self, pRoot):
        # write code here
        if pRoot==None:
            return True
        h1=self.Get_Tree_Height(pRoot.left)
        h2=self.Get_Tree_Height(pRoot.right)
        if(h1-h2)<=1 and(h2-h1)<=1:
            return True
        else:
            return False
    def Get_Tree_Height(self, pRoot):
        if pRoot==None:
            return 0
        else:
            return max(self.Get_Tree_Height(pRoot.left),self.Get_Tree_Height(pRoot.right))+1

c++

class Solution {
public:
    int TreeDepth(TreeNode *pRoot)
{
    if (pRoot == NULL)
        return 0;
    int left = TreeDepth(pRoot->left);
    int right = TreeDepth(pRoot->right);

    return (left > right) ? (left + 1) : (right + 1);
}

    bool IsBalanced_Solution(TreeNode* pRoot) {
    if (pRoot == NULL)
        return true;

    int left = TreeDepth(pRoot->left);
    int right = TreeDepth(pRoot->right);
    int diff = left - right;
    if (diff > 1 || diff < -1)
        return false;

    return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
    }
};

11.和為S的連續正數序列

輸出所有和為S的連續正數序列。序列內按照從小至大的順序,序列間按照開始數字從小到大的順序
思考:S%奇數==0 或者S%偶數==偶數/2 就說明有這個連續序列,但是注意是正數序列,可能會出現越界情況


python

# -*- coding:utf-8 -*-
class Solution:
    def FindContinuousSequence(self, tsum):
        # write code here
        k = 2
        ret = []
        for k in range(2,tsum):
            if k%2==1 and tsum%k==0:
                tmp = []
                mid = tsum/k
                if mid-k/2>0:
                    for i in range(mid-k/2,mid+k/2+1):
                        tmp.append(i)
                    ret.append(tmp[:])
            elif k%2==0 and (tsum%k)*2==k:
                mid = tsum/k
                tmp = []
                if mid-k/2+1>0:
                    for i in range(mid-k/2+1,mid+k/2+1):
                        tmp.append(i)
                    ret.append(tmp[:])
        ret.sort()
        return ret

c++

class Solution {
public:
    vector<vector<int> > FindContinuousSequence(int sum) {
        int l = 1,r = 1,sumx = 1;
        vector<vector<int> > ans;
        while(l <= r){
            r ++;
            sumx += r;
            while(sumx > sum){
                sumx -= l;
                l ++;
            }
            if(sumx == sum && l != r){
                vector<int> tmp;
                for(int i = l;i <= r;i ++)  tmp.push_back(i);
                ans.push_back(tmp);
            }
        }
        return ans;
    }
};

12.左旋轉字串

對於一個給定的字元序列S,請你把其迴圈左移K位後的序列輸出。


python

# -*- coding:utf-8 -*-
class Solution:
    def LeftRotateString(self, s, n):
        # write code here
        if s=='':
            return s
        mov=n%len(s)
        news=s[mov:]+s[:mov]
        return news

c++

class Solution {
public:
    string LeftRotateString(string str, int n) {
        int lstr=str.size();
        if (lstr==0)return str;
        int mov=n%lstr;
        string newstr;
        for(int i = mov;i<lstr;i++)newstr.push_back(str[i]);
        for(int i = 0;i<mov;i++)newstr.push_back(str[i]);
        return newstr;
    }
};

13.數字在排序陣列中出現的次數

思路:二分查詢


python

# -*- coding:utf-8 -*-
class Solution:
    def GetNumberOfK(self, data, k):
        # write code here
        start = 0
        end = len(data)-1
        while(start<=end):
            mid = (start+end)/2
            if data[mid]==k:
                cnt = 0
                tmp = mid
                while(tmp>=0 and data[tmp]==k):
                    cnt+=1
                    tmp-=1
                tmp = mid+1
                while(tmp<len(data) and data[tmp]==k):
                    cnt+=1
                    tmp+=1
                return cnt
            elif data[mid]>k:
                end = mid-1
            else:
                start = mid+1
        return 0

C++

class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        if(data.size()==0)return 0;
        int len=data.size();
        int KeyIndex=0;
        KeyIndex=BinarySearch(data,0,len-1,k);
        if(KeyIndex==-1) return 0;
        int sumber=1;
        int m=KeyIndex-1;
        int n=KeyIndex+1;

       while(m>=0&&data[m]==k)
        {
                m--;sumber++;
            }
        while(n<len&&data[n]==k)
        {
               n++; sumber++;
            }
        return sumber;
    }
    int BinarySearch(vector<int> data, int low, int high, int k){
        while (low<=high){
        int m = (high + low) / 2;
        if (data[m] == k)return m;
        else if (data[m] < k) low = m+ 1;
        else high = m - 1;}
        return -1;
    }
};

14.陣列中只出現一次的數字

思路:在其他題目中,要求時間複雜度為O(n),排除先排序;空間複雜度要求O(1),排除hash思路。考慮到對於一個int型別變數a,a與0的異或為本身,與自身的異或為0,因此可以將所有數字依次異或,這樣偶數次數的數字被消除,僅剩一次的數字。但本題中有兩個這樣的數字,因此需要將兩個數字分離成兩堆。在將所有數字異或後,所得結果即所求兩個數字的異或值,因此可從兩個數字的異或值中第一位不是0的位出手,將所有數字根據此位是否為1分為兩堆,兩堆分別求異或值結果即所求兩數。


python

class Solution:
    # 返回[a,b] 其中ab是出現一次的兩個數字
    def FindNumsAppearOnce(self, array):
        # write code here
        ans,a1,a2,flag= 0,0,0,1
        for num in array:
            ans = ans ^ num
        while(ans):
            if ans%2 == 0:
                ans = ans >>1 
                flag = flag <<1
            else:
                break
        for num in array:
            if num & flag:
                a1 = a1 ^ num
            else:
                a2 = a2 ^ num
        return a1,a2

c++

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        if (data.size() < 2)
        return;
        int resultExclusiveOR = 0;
        for (int i = 0; i < data.size(); ++ i)
            resultExclusiveOR ^= data[i];
        unsigned int indexOf1 = FindFirstBitIs1(resultExclusiveOR); 
        *num1 = *num2 = 0;
        for (int j = 0; j < data.size(); ++ j){
            if(IsBit1(data[j], indexOf1))
            *num1 ^= data[j];
            else
            *num2 ^= data[j];
    }
    }
    unsigned int FindFirstBitIs1(int num){
        int indexBit = 0;
        while (((num & 1) == 0) && (indexBit < 32)){
            num = num >> 1;
            indexBit++;}
        return indexBit;
    }
    bool IsBit1(int num, unsigned int indexBit){
        num = num >> indexBit;
        return (num & 1);}
};

15.翻轉單詞順序列

將句子中由空格間隔的各詞彙順序翻轉。


python

# -*- coding:utf-8 -*-
class Solution:
    def ReverseSentence(self, s):
        # write code here
        ret = s.split(" ")
        ret.reverse()
        return ' '.join(ret)

c++

class Solution {
public:
     string ReverseSentence(string str) {
        int len = str.size();
        int start = 0;
        for(int i = 0; i < len; i ++){
             if(str[i] == ' '){
                 reverse(str.begin()+start, str.begin()+i);
                 start = i+1;
             }
        if(i == len-1){
            reverse(str.begin()+start, str.end());
        }
        }
        reverse(str.begin(), str.end());
        return str;
    }
};

16.二叉樹深度

python

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        if(pRoot==None):
            return 0
        h1 = self.TreeDepth(pRoot.left);
        h2 = self.TreeDepth(pRoot.right);
        if h1>h2:
            return h1+1
        else:
            return h2+1

c++

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
class Solution {
public:
    int TreeDepth(TreeNode* pRoot)
    {
        if (pRoot==NULL)return 0;
        int depth1 = TreeDepth(pRoot->left);
        int depth2 = TreeDepth(pRoot->right);
        int depth = (depth1 > depth2 ? depth1 : depth2)+1;
        return depth;
    }
};

17.和為S的兩個數字

輸入一個遞增排序的陣列和一個數字S,在陣列中查詢兩個數,是的他們的和正好是S,如果有多對數字的和等於S,輸出兩個數的乘積最小的。
思路:採用兩個指標分別從陣列兩端向中間推進,最先滿足條件的兩個數乘積最小。


python

# -*- coding:utf-8 -*-
class Solution:
    def FindNumbersWithSum(self, array, tsum):
        # write code here
        a1=0
        a2=-1
        while(a1-a2<len(array)):
            if(array[a1]+array[a2]<tsum):
                a1+=1
            elif(array[a1]+array[a2]>tsum):
                a2-=1
            else:
                return [array[a1],array[a2]]
        return[]

c++

class Solution {
public:
    vector<int> FindNumbersWithSum(vector<int> array,int sum) {
        int len = array.size();
        vector<int>twonum;
        if (len<=1)return twonum;
        int v1=0,v2=len-1;

        while(v1!=v2){
            if((array[v1]+array[v2])<sum)v1++;
            else if((array[v1]+array[v2])>sum)v2--;
            else {twonum.push_back(array[v1]);twonum.push_back(array[v2]);break;}
        }
        return twonum;
    }
};

18.順時針列印矩陣

python

# -*- coding:utf-8 -*-
class Solution:
    # matrix型別為二維列表,需要返回列表
    def printMatrix(self, matrix):
        # write code here
        m=len(matrix)
        ans=[]
        if m==0:
            return ans
        n=len(matrix[0])
        w1=0;w2=n-1;h1=0;h2=m-1;
        while(True):
            if((w1>w2)or(h1>h2)):
                break;
            for i in range(w1,w2+1):
                ans.append(matrix[h1][i])
            h1=h1+1
            if((w1>w2)or(h1>h2)):
                break;
            for i in range(h1,h2+1):
                ans.append(matrix[i][w2])
            w2=w2-1
            if((w1>w2)or(h1>h2)):
                break;
            for i in range(w1,w2+1):
                ans.append(matrix[h2][n-2-i])
            h2=h2-1
            if((w1>w2)or(h1>h2)):
                break;
            for i in range(h1,h2+1):
                ans.append(matrix[m-1-i][w1])
            w1=w1+1
        return ans

c++

class Solution {
public:
    vector<int> printMatrix(vector<vector<int>> matrix) {
        int row=matrix.size();
        int col=matrix[0].size();
        vector<int> result;
        if(row==0||col==0)
            return result;
        int left=0,right=col-1,top=0,btm=row-1;
        while(left<=right&&top<=btm)
            {
            for(int i=left;i<=right;i++)
                result.push_back(matrix[top][i]);
            if(top<btm)
                for(int i=top+1;i<=btm;i++)
                    result.push_back(matrix[i][right]);
            if(top<btm&&left<right)
                for(int i=right-1;i>=left;i--)
                    result.push_back(matrix[btm][i]);
            if(top+1<btm&&left<right)
                for(int i=btm-1;i>=top+1;i--)
                    result.push_back(matrix[i][left]);
            left++;right--;top++;btm--;
        }
        return result;
    }
};

19.二叉樹的下一個結點

給定一個二叉樹和其中的一個結點,請找出中序遍歷順序的下一個結點並且返回。注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指標。
思路:中序遍歷,根據其是否含有右子樹、若無則返回父節點等系列條件判斷
python

class Solution:
    def GetNext(self, pNode):
        # write code here
        # left root right
        if pNode == None:
            return None
        if pNode.right:
            tmp = pNode.right
            while(tmp.left):
                tmp = tmp.left
            return tmp
        p = pNode.next
        while(p and p.right==pNode):
            pNode = p
            p = p.next
        return p

c++

/*
struct TreeLinkNode {
    int val;
    struct TreeLinkNode *left;
    struct TreeLinkNode *right;
    struct TreeLinkNode *next;
    TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {

    }
};
*/
class Solution {
public:
    TreeLinkNode* GetNext(TreeLinkNode* pNode)
    {
        if(pNode->right!=NULL){
            TreeLinkNode *temp=pNode->right;
            while(temp->left!=NULL)temp=temp->left;
            return temp;
        }
        else{
            TreeLinkNode *temp=pNode;
            while(temp->next!=NULL){
                if(temp==temp->next->left)return temp->next;
                else{temp=temp->next;}
            }
            return NULL;
        }
    }
};

20.

相關推薦

Offer題目:字符串的排列

wap toc java res add font 16px turn return 題目描述: 輸入一個字符串,按字典序打印出該字符串中字符的所有排列。例如輸入字符串abc,則打印出由字符a,b,c所能排列出來的所有字符串abc,acb,bac,bca,cab和cba。

Offer題目索引

兩個 打印 平衡二叉樹 代碼 知識 循環 加減乘 復雜 字符串 數組 數組中重復的數字 二維數組中的查找 構建乘積數組 字符串 替換空格 字符流中第一個不重復的字符 表示數值的字符串 遞歸和循環 斐波那契數列 跳臺階 變態跳臺階 矩形覆蓋 鏈表 從尾到頭打印鏈表 刪除鏈表中

offer題目系列一

過程 一個數 1-1 二維數組 等於 style 所有 RR htm 本篇介紹《劍指offer》第二版中的四個題目:找出數組中重復的數字、二維數組中的查找、替換字符串中的空格、計算斐波那契數列第n項。 這些題目並非嚴格按照書中的順序展示的,而

offer題目系列三(鏈表相關題目

eight 信息 賦值 n) 內容 指針 alt 延續 合並兩個排序的鏈表 本篇延續上一篇劍指offer題目系列二,介紹《劍指offer》第二版中的四個題目:O(1)時間內刪除鏈表結點、鏈表中倒數第k個結點、反轉鏈表、合並兩個排序的鏈表。同樣,這些題目並非嚴

[持久更新] offer題目Python做題記錄

array tno 節點 gif tlist 思路 實現 span elf 第一題   題目:在一個二維數組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。   思路:先

offer題目答案集合

package org.nix.learn.offer; import org.junit.jupiter.api.Test; import sun.rmi.transport.tcp.TCPChannel; import java.util.*; import java

offer題目

1.二叉樹映象 python: # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #

【面試】offer題目解析及tips速查(不斷更新中)

4、二維陣列中的查詢x:陣列從左到右遞增,從上到下遞增。P38 tips:右上角開始查詢,x小左移,x大下移。 tips+:陣列為空(空指標),行列<=0情況;左移和下移越界情況;引數為指標,根據記憶體儲存規則訪問陣列元素。 5、替換空格:實現函式將一個字串中所有空格替

牛客網劍指offer題目一在一個二維陣列中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函式,輸入這樣的一個二維陣列和一個整數,判斷陣列中是否含有該整數

在一個二維陣列中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函式,輸入這樣的一個二維陣列和一個整數,判斷陣列中是否含有該整數。 思路:因為是隻需判斷有無該整數,所以用bool函式,返回false或true      假設是這樣一個數組

offer題目分類

一、線性表 1、陣列 面試題3:二維陣列中的查詢 面試題14:調整陣列順序使得奇數位於偶數前面 面試題29:陣列中出現超過一半的數字 面試題30:最小的k個數 面試題33:把陣列排成最小的數 面試題36:陣列中的逆序對 面試題40:數字在排序陣列中出現的次數 面試題51:陣

offer題目java實現

Problem2:實現Singleton模式 題目描述:設計一個類,我們只能生成該類的一個例項 1 package Problem2; 2 3 public class SingletonClass { 4 5 /* 6 * 題目描述:設計一個類,我們只能生成

劍指offer題目描述 輸入一個整數陣列,實現一個函式來調整該陣列中數字的順序,使得所有的奇數位於陣列的前半部分,所有的偶數位於位於陣列的後半部分,並保證奇數和奇數,偶數和偶數之間的相對位置不變。

牛客網劍指offer線上程式設計: 方法一:利用兩個指標,實現了是陣列奇數在前偶數在後,但通過不了測試用例,因為改變了奇數和奇數,偶數和偶數之間的相對位置 class Solution { public:     void reOrderArray(vector<in

offer算法編程題目部分匯總(解法略)

面試題 搜索 中序遍歷 順時針 路徑 奇數 第一個 重復 不同 總結一下本書中遇到的大部分面試題。面試題3:二維數組中的查找 題目:在一個二維數組中,每一行都按照從左到右的遞增順序排列,每一列都按照從上到下遞增的順序排列,請完成一個函數,輸入這樣的一個整數,判斷數組中是否含

Offer題目——位運算

ati 劍指offer 多少 amp nes 位運算 規則 -s n) 題目描述:輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼表示。 題目分析:無論使用什麽方式,最關鍵的就是要考慮負數的處理方式 public class NumberOfOne {

Offer題目:樹的子結構

bool root 子結構 boolean res pub 二叉樹 amp turn 題目描述:樹的子結構 輸入兩棵二叉樹A,B,判斷B是不是A的子結構。(ps:我們約定空樹不是任意一個樹的子結構) 題目分析:此題可以拆成兩題,1.在二叉樹中查找是否存在某個結點;2.判斷兩

Offer題目:合並兩個排序的鏈表

合成 sorted 合並 邊界情況 logs pub st2 next null 題目描述:輸入兩個單調遞增的鏈表list1,list2,輸出兩個鏈表合成後的鏈表,當然我們需要合成後的鏈表滿足單調不減規則。 題目分析: 1.對於鏈表題目,首先考慮邊界情況,即鏈表為空的情況,

Offer題目:鏈表中倒數第k個結點

倒數 -- 輸出 col ota pan code 輸入 tno 題目描述:輸入一個鏈表,輸出該鏈表中倒數第k個結點 題目分析:因為不能直接從鏈表的尾部遍歷,所以要分兩步走: 第一步:從鏈表的頭部開始遍歷,直至鏈表的尾部,統計出鏈表結點的個數 第二步:根據鏈表結點的個數,計

Offer題目:調整數組順序使奇數位於偶數前面

code ont else 指向 偶數 span cnblogs for emp 題目描述:調整數組順序使奇數位於偶數前 輸入一個整數數組,實現一個函數來調整該數組中數字的順序,使得所有的奇數位於數組的前半部分,所有的偶數位於位於數組的後半部分,並保證奇數和奇數,偶數和偶數

Offer - 後面再做的題目

blog odin interview https 劍指offer coder class int odi https://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId

leetcode 70. 爬樓梯【遞迴】【Easy】&& Offer面試題10 題目2:青蛙跳臺階問題

題目: 假設你正在爬樓梯。需要 n 階你才能到達樓頂。 每次你可以爬 1 或 2 個臺階。你有多少種不同的方法可以爬到樓頂呢? 注意:給定 n 是一個正整數。 示例 1: 輸入: 2 輸出: 2 解釋: 有兩種方法可以爬到樓頂。 1. 1