面試程式設計題目總結

2021-10-19 14:26:23 字數 3627 閱讀 8308

1.leetcode**現not all control paths return a value

參考:在leetcode上做題,一直提示這個,原來是因為各分支必須有返回值

快慢指標同步移動,最後判斷是否重合

/**

* definition for singly-linked list.

* struct listnode

* };

*/class solution

return false;

}};

class solution

int pop()

int x=stack2.top();

stack2.pop();

return x;

}private:

stackstack1;

stackstack2;

};

class solution 

}if(r>left&&k<=r)find_kth(a,left,r,k);

if(l=l)find_kth(a,l,right,k);

}int findkth(vectora, int n, int k)

};

題目大意:給定乙個由若干 0 和 1 組成的陣列 a,我們最多可以將 k 個值從 0 變成 1 。

返回僅包含 1 的最長(連續)子陣列的長度。

class solution 

ans=max(ans,r-l+1);

for(int l=1;lr)r++;

else if(a[l-1]==0)k++;

while(r0))

ans=max(ans,r-l+1);

}return ans;

}};

/**

* definition for singly-linked list.

* struct listnode

* listnode(int x) : val(x), next(nullptr) {}

* listnode(int x, listnode *next) : val(x), next(next) {}

* };

*/class solution

return head;

}};

給定乙個二叉樹, 找到該樹中兩個指定節點的最近公共祖先。

/**

* definition for a binary tree node.

* struct treenode

* };

*/class solution

if(now->right!=null)

}treenode* lowestcommonancestor(treenode* root, treenode* p, treenode* q)

while(flag[q->val]==null)q=fa[q->val];

return q;

}};

給你乙個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?請你找出所有和為 0 且不重複的三元組。

注意:答案中不可以包含重複的三元組。

class solution );}}

}return ans;

}};

運用你所掌握的資料結構,設計和實現乙個 lru (最近最少使用) 快取機制 。

實現 lrucache 類:

lrucache(int capacity) 以正整數作為容量 capacity 初始化 lru 快取

int get(int key) 如果關鍵字 key 存在於快取中,則返回關鍵字的值,否則返回 -1 。

void put(int key, int value) 如果關鍵字已經存在,則變更其資料值;如果關鍵字不存在,則插入該組「關鍵字-值」。當快取容量達到上限時,它應該在寫入新資料之前刪除最久未使用的資料值,從而為新的資料值留出空間。

class lrucache q[100000];

int r=0,cnt=0;

public:

lrucache(int capacity)

int get(int key)

}ans=max(ans,f[i]);

}return ans;

}};

用鍊錶實現快速排序

#includeusing namespace std;

struct node;

int n,x;

void kspx(node *head,node *tail,int left,int right)

}if(r>left)kspx(head,t,left,r);

if(l>n;

node *head=new node;

//注意不能node *head,*tail寫成這樣,這樣會出現runtimeerror在luogu上,估計是有可能

//head和tail沒有new node,所以開成了同乙個

node *tail=new node;

tail=head;

for(int i=1;i<=n;i++)

head=head->next;

kspx(head,tail,1,n);

while(head!=null)

cout《可以用個小根堆維護當前m個佇列的隊首,每次選出min,然後選擇下乙個加入堆,重複k次。複雜度klogm。

也可以用大根堆維護,當新加入的元素大於堆頂,直接放棄這個佇列(因為後面的肯定不可能),如果可以,則需要退出乙個(退出的那個佇列之後的元素肯定也不需要了,也可以直接pass那個佇列).

#include using namespace std;

struct node

};priority_queueheap;//注意一下優先佇列的定義

int n,m,k,a[1110][1100];

int main()

for(int i=1;i<=m;i++));

}cin>>k;

while(k--)

};bool taskcmp(vector&a,vector&b)else

}return res;

}int main()

vectorans;

ans=getorder(tasks);

for(int i=0;i或者這樣 vector裡面是結構體

#includeusing namespace std;

struct data

};bool taskcmp(data a,data b)

if(q.size()>0)else

}return res;

}int main());

}vectorans;

ans=getorder(tasks);

for(int i=0;icout

}

面試 程式設計 質數

第一種演算法 public static boolean prime int num return true 看後不得不佩服原創多太有才了,乙個整數傳過來,for迴圈i初始值為2,第一步首先砍掉了偶數的。先發制人,一箭雙鵰。第二種做法,借第一種做法。判斷num 1 或者2省略 public stat...

面試程式設計 atoi實現

main.cpp my atoi created by on 14 5 6.基礎需求 1 號 2 開頭空格 3 合法性判斷 4 溢位判斷 高階需求 未實現 1 逗號,如1,234,567 2 進製,如0x123 3 科學計數,1.367 10 3 include include long my at...

面試程式設計題整理

1.輸入c寫乙個輸入的整數,倒著輸出整數的函式,要求用遞迴方法 要求用遞迴的方法對乙個整數進行倒敘 include void func int n else func n int main 2.編寫乙個函式,作用是把乙個char組成的字串迴圈右移n個。比如原來的是 abcdefgh 如果n 2,移位...