You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution {
public:intduplicateInArray(vector<int>& nums) {
int l = 1;
int r = nums.size() - 1;
while(l < r){
int mid = l + r >> 1;
int s = 0;
for(auto x : nums) s += (x >= l && x <= mid ? 1 : 0);
if(s > mid -l + 1){
r = mid;
} else {
l = mid + 1;
}
}
return r;
}
};
Java
classSolution {
publicintduplicateInArray(int[] nums) {
intl = 1;
intr = nums.length - 1;
while(l < r){
intmid = l + r >> 1;
ints = 0;
for(intx : nums) s += x >= l && x <= mid ? 1 : 0;
if(s > mid - l + 1){
r = mid;
} else {
l = mid + 1;
}
}
returnl;
}
}
Python
```python
class Solution(object):
def inorderSuccessor(self, q):
"""
:type q: TreeNode
:rtype: TreeNode
"""
if not q:
return None
# 右子树的最左节点
if q.right:
q = q.right
p = q
while q:
p = q
q = q.left
return p
else:
# 整个子树的父节点
f = q.father
while f and f.right == q:
q = f
f = q.father
return f
```
面试题9:用两个栈实现队列Java
classMyQueue {
Deque<Integer> in;
Deque<Integer> out;
/** Initialize your data structure here. */publicMyQueue() {
in = newArrayDeque<>();
out = newArrayDeque<>();
}
/** Push element x to the back of queue. */publicvoidpush(intx) {
in.push(x);
}
// 假如out是空 则调用此函数填充inpublicvoidclearIn(){
while(!in.isEmpty()){
out.push(in.pop());
}
}
/** Removes the element from in front of queue and returns that element. */publicintpop() {
if(out.isEmpty()){
clearIn();
}
returnout.pop();
}
/** Get the front element. */publicintpeek() {
if(out.isEmpty()){
clearIn();
}
returnout.peek();
}
/** Returns whether the queue is empty. */publicbooleanempty() {
returnout.isEmpty() && in.isEmpty();
}
}
Python
classMyQueue(object):
def__init__(self):
""" Initialize your data structure here. """self.com= []
self.out= []
defpush(self, x):
""" Push element x to the back of queue. :type x: int :rtype: void """self.com.append(x)
defpop(self):
""" Removes the element from in front of queue and returns that element. :rtype: int """ifnotself.out:
whilelen(self.com):
self.out.append(self.com.pop())
returnself.out.pop()
defpeek(self):
""" Get the front element. :rtype: int """ifnotself.out:
whilelen(self.com):
self.out.append(self.com.pop())
returnself.out[len(self.out) -1]
defempty(self):
""" Returns whether the queue is empty. :rtype: bool """returnlen(self.com) ==0andlen(self.out) ==0;
面试题10:斐波那契数列
// a b (c)publicintFibonacci(intn) {
inta = 0, b = 1, c = 0;
while(n -- > 0){
c = a + b;
a = b;
b = c;
}
returna;
}
剑指offer题解
多种编程语言打卡剑指offer 👏
面试题3: 找出数组中重复数字
Java
C++
Python
面试题3.2:不修改数组找出重复数字
C++
Java
面试题4: 二维数组查找
Java
C++
面试题5: 替换空格
面试题7:重建二叉树
Java
二叉树
Python
class Solution(object):
def buildTree(self, preorder, inorder):
map = {}
def build(preL, preR, inL):
if preL > preR:
return
# preorder root
# 根节点
val = preorder[preL]
root = TreeNode(val)
index = map.get(val)
length = index - inL
root.left = build(preL + 1, preL + length, inL)
root.right = build(preL + length + 1, preR, index + 1)
return root
for i in range(len(inorder)):
map[inorder[i]] = i
return build(0, len(preorder) - 1, 0)
面试题8:二叉树的下一个节点
Java
Python
```python class Solution(object): def inorderSuccessor(self, q): """ :type q: TreeNode :rtype: TreeNode """ if not q: return None # 右子树的最左节点 if q.right: q = q.right p = q while q: p = q q = q.left return p else: # 整个子树的父节点 f = q.father while f and f.right == q: q = f f = q.father return f ```面试题9:用两个栈实现队列
Java
Python
面试题10:斐波那契数列
面试题11: 旋转数组的最小值
二分查找
面试题12:矩阵中的路径
dfs
面试题13:机器人的运动范围
dfs
面试题21:调整数据顺序使奇数位于偶数前面
面试题32:从上到小打印二叉树
面试题32.2 分层从上到小打印二叉树
队列的最大值
分析和代码
分析
给定一个数组和窗口大小,求滑动窗口的最大值
可以用单调队列求解
所有元素只会进队一次,出队一次,所以时间负责度是O(N)
AC代码
Java
Python
C++
JavaScript
The text was updated successfully, but these errors were encountered: