日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

剑指offer(刷题61-65)--c++,Python版本

發(fā)布時(shí)間:2023/12/13 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 剑指offer(刷题61-65)--c++,Python版本 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 目錄
    • 第61題:
      • 解題思路:
      • 代碼實(shí)現(xiàn):
        • c++
        • python
    • 第62題:
      • 解題思路:
      • 代碼實(shí)現(xiàn):
        • c++
        • python
    • 第63題:
      • 解題思路:
      • 代碼實(shí)現(xiàn):
        • c++
        • python
    • 第64題:
      • 解題思路:
      • 代碼實(shí)現(xiàn):
        • c++
        • python
    • 第65題:
      • 解題思路:
      • 代碼實(shí)現(xiàn):
        • c++
          • 回溯法
          • 動(dòng)態(tài)規(guī)劃法
        • python

目錄

第61題:

給定一棵二叉搜索樹,請(qǐng)找出其中的第k小的結(jié)點(diǎn)。例如, (5,3,7,2,4,6,8) 中,按結(jié)點(diǎn)數(shù)值大小順序第三小結(jié)點(diǎn)的值為4。

解題思路:

  • 由于是二叉搜索樹,其節(jié)點(diǎn)存放的值是有一定的規(guī)律的,所以我們可以使用一個(gè)數(shù)組存放二叉搜索樹的中序遍歷結(jié)果,然后直接將數(shù)字的第K個(gè)結(jié)果給出即可。時(shí)間復(fù)雜度為O(N),空間復(fù)雜度也為O(N)。

代碼實(shí)現(xiàn):

c++

class Solution { public:int index = 0;TreeNode* KthNode(TreeNode* pRoot, int k){if(pRoot != NULL){TreeNode* node = KthNode(pRoot->left , k);if(node != NULL) return node;index ++;if(index == k) return pRoot;node = KthNode(pRoot->right , k);if(node != NULL) return node;}return NULL;} };

運(yùn)行時(shí)間:4ms

占用內(nèi)存:608k

class Solution { public:TreeNode* KthNode(TreeNode* pRoot, unsigned int k){if(pRoot==NULL||k<=0) return NULL;vector<TreeNode*> vec;Inorder(pRoot,vec);if(k>vec.size())return NULL;return vec[k-1];}//中序遍歷,將節(jié)點(diǎn)依次壓入vector中void Inorder(TreeNode* pRoot,vector<TreeNode*>& vec){if(pRoot==NULL) return;Inorder(pRoot->left,vec);vec.push_back(pRoot);Inorder(pRoot->right,vec);} };

python

# -*- coding:utf-8 -*-

第62題:

如何得到一個(gè)數(shù)據(jù)流中的中位數(shù)?如果從數(shù)據(jù)流中讀出奇數(shù)個(gè)數(shù)值,那么中位數(shù)就是所有數(shù)值排序之后位于中間的數(shù)值。如果從數(shù)據(jù)流中讀出偶數(shù)個(gè)數(shù)值,那么中位數(shù)就是所有數(shù)值排序之后中間兩個(gè)數(shù)的平均值。我們使用Insert()方法讀取數(shù)據(jù)流,使用GetMedian()方法獲取當(dāng)前讀取數(shù)據(jù)的中位數(shù)。

解題思路:

/**
* 插入有兩種思路:
* 1:直接插入大堆中,之后若兩堆尺寸之差大于1(也就是2),則從大堆中彈出堆頂元素并插入到小堆中
* 若兩隊(duì)之差不大于1,則直接插入大堆中即可。
* 2:奇數(shù)個(gè)數(shù)插入到大堆中,偶數(shù)個(gè)數(shù)插入到小堆中,
* 但是 可能會(huì)出現(xiàn)當(dāng)前待插入的數(shù)比小堆堆頂元素大,此時(shí)需要將元素先插入到小堆,然后將小堆堆頂元素彈出并插入到大堆中
* 對(duì)于偶數(shù)時(shí)插入小堆的情況,一樣的道理。why?
* 因?yàn)橐WC最大堆的元素要比最小堆的元素都要小。
* @param num
*/

代碼實(shí)現(xiàn):

c++

class Solution { public:priority_queue<int, vector<int>, less<int> > p; //小堆priority_queue<int, vector<int>, greater<int> > q; //大堆void Insert(int num){if(p.empty() || num <= p.top()) p.push(num);else q.push(num);if(p.size() == q.size() + 2) q.push(p.top()), p.pop();if(p.size() + 1 == q.size()) p.push(q.top()), q.pop();}double GetMedian(){ return p.size() == q.size() ? (p.top() + q.top()) / 2.0 : p.top();}};

運(yùn)行時(shí)間:6ms

占用內(nèi)存:484k

python

# -*- coding:utf-8 -*-

第63題:

給定一個(gè)數(shù)組和滑動(dòng)窗口的大小,找出所有滑動(dòng)窗口里數(shù)值的最大值。例如,如果輸入數(shù)組{2,3,4,2,6,2,5,1}及滑動(dòng)窗口的大小3,那么一共存在6個(gè)滑動(dòng)窗口,他們的最大值分別為{4,4,6,6,6,5}; 針對(duì)數(shù)組{2,3,4,2,6,2,5,1}的滑動(dòng)窗口有以下6個(gè): {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

解題思路:

  • 暴力解法:直接使用滑動(dòng)窗的原理,得出每個(gè)窗口的數(shù)據(jù),然后給出最大值即可。時(shí)間復(fù)雜度為O(N^2).

代碼實(shí)現(xiàn):

c++

python

# -*- coding:utf-8 -*- class Solution:def maxInWindows(self, num, size):# write code hereresList = []sampleNum = (len(num)-size + 1)if sampleNum <= 0 or size <=0:return resListfor i in range(sampleNum):resList.append(max(num[i:i+size]))return resList

運(yùn)行時(shí)間:31ms

占用內(nèi)存:5728k

第64題:

請(qǐng)?jiān)O(shè)計(jì)一個(gè)函數(shù),用來(lái)判斷在一個(gè)矩陣中是否存在一條包含某字符串所有字符的路徑。路徑可以從矩陣中的任意一個(gè)格子開始,每一步可以在矩陣中向左,向右,向上,向下移動(dòng)一個(gè)格子。如果一條路徑經(jīng)過(guò)了矩陣中的某一個(gè)格子,則之后不能再次進(jìn)入這個(gè)格子。 例如 a b c e s f c s a d e e 這樣的3 X 4 矩陣中包含一條字符串"bcced"的路徑,但是矩陣中不包含"abcb"路徑,因?yàn)樽址牡谝粋€(gè)字符b占據(jù)了矩陣中的第一行第二個(gè)格子之后,路徑不能再次進(jìn)入該格子。

解題思路:

代碼實(shí)現(xiàn):

c++

class Solution {bool hasPathRecu(char* matrix, int rows, int cols, int row, int col, char *str, int length, vector<bool> used){if(length==strlen(str))return true;if(row<0||row>=rows||col<0||col>=cols)return false;int index = col + row*cols;bool result = false;if( !used[index] && matrix[index]==str[length]){used[index] = true;result = hasPathRecu(matrix, rows, cols, row-1, col, str, length+1, used)|| hasPathRecu(matrix, rows, cols, row+1, col, str, length+1, used)||hasPathRecu(matrix, rows, cols, row, col+1, str, length+1, used)||hasPathRecu(matrix, rows, cols, row, col-1, str, length+1, used);used[index] = false;}if(result)return true;return false;} public:bool hasPath(char* matrix, int rows, int cols, char* str){vector<bool> used(strlen(matrix),false);if(str==NULL) return true;for(int i=0;i<rows;i++){for(int j=0;j<cols;j++){if(hasPathRecu(matrix, rows, cols, i, j, str, 0, used))return true;}}return false;} };

運(yùn)行時(shí)間:5ms

占用內(nèi)存:364k

python

# -*- coding:utf-8 -*-

第65題:

地上有一個(gè)m行和n列的方格。一個(gè)機(jī)器人從坐標(biāo)0,0的格子開始移動(dòng),每一次只能向左,右,上,下四個(gè)方向移動(dòng)一格,但是不能進(jìn)入行坐標(biāo)和列坐標(biāo)的數(shù)位之和大于k的格子。 例如,當(dāng)k為18時(shí),機(jī)器人能夠進(jìn)入方格(35,37),因?yàn)?+5+3+7 = 18。但是,它不能進(jìn)入方格(35,38),因?yàn)?+5+3+8 = 19。請(qǐng)問(wèn)該機(jī)器人能夠達(dá)到多少個(gè)格子?

解題思路:

代碼實(shí)現(xiàn):

c++

回溯法
class Solution {bool hasPathRecu(char* matrix, int rows, int cols, int row, int col, char *str, int length, vector<bool> used){if(length==strlen(str))return true;if(row<0||row>=rows||col<0||col>=cols)return false;int index = col + row*cols;bool result = false;if( !used[index] && matrix[index]==str[length]){used[index] = true;result = hasPathRecu(matrix, rows, cols, row-1, col, str, length+1, used)|| hasPathRecu(matrix, rows, cols, row+1, col, str, length+1, used)||hasPathRecu(matrix, rows, cols, row, col+1, str, length+1, used)||hasPathRecu(matrix, rows, cols, row, col-1, str, length+1, used);used[index] = false;}if(result)return true;return false;} public:bool hasPath(char* matrix, int rows, int cols, char* str){vector<bool> used(strlen(matrix),false);if(str==NULL) return true;for(int i=0;i<rows;i++){for(int j=0;j<cols;j++){if(hasPathRecu(matrix, rows, cols, i, j, str, 0, used))return true;}}return false;} };

運(yùn)行時(shí)間:3ms

占用內(nèi)存:476k

動(dòng)態(tài)規(guī)劃法
public class Solution {public int movingCount(int threshold, int rows, int cols){if(threshold<0)return 0;boolean [][] dp=new boolean[rows+1][cols+1];dp[0][0]=true;for(int i=1;i<=rows;i++){//初始化if(dp[i-1][0]&&canreach(threshold,i,0)){dp[i][0]=true;}else {dp[i][0]=false;}}for(int i=1;i<=cols;i++){//初始化if(dp[0][i-1]&&canreach(threshold,0,i)){dp[0][i]=true;}else {dp[0][i]=false;}}for(int i=1;i<=rows;i++){for(int j=1;j<=cols;j++){if((dp[i-1][j]&&canreach(threshold, i, j))||(dp[i][j-1]&&canreach(threshold, i, j))){dp[i][j]=true;}else {dp[i][j]=false;}}}int count=0;for(int i=0;i<rows;i++){for(int j=0;j<cols;j++){if(dp[i][j])count++;}} return count; }public boolean canreach(int threshold, int x, int y) {int sum = 0;while (x > 0) {sum += x % 10;x /= 10;}while (y > 0) {sum += y % 10;y /= 10;}return sum <= threshold;} }

python

# -*- coding:utf-8 -*-

總結(jié)

以上是生活随笔為你收集整理的剑指offer(刷题61-65)--c++,Python版本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。