数据结构编程题,出10道题目.给出答案。
时间: 2023-07-25 19:24:32 浏览: 61
好的,以下是10道数据结构编程题和答案:
1. 给定一个数组,找到数组中的最大值和最小值。
答案:
```python
def find_max_min(arr):
max_val = arr[0]
min_val = arr[0]
for i in range(1, len(arr)):
if arr[i] > max_val:
max_val = arr[i]
elif arr[i] < min_val:
min_val = arr[i]
return max_val, min_val
```
2. 给定一个字符串,判断它是否是回文字符串。
答案:
```python
def is_palindrome(s):
s = s.lower()
s = ''.join(c for c in s if c.isalnum())
return s == s[::-1]
```
3. 实现一个栈,包括 push、pop 和 get_min 方法。
答案:
```python
class Stack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val):
self.stack.append(val)
if not self.min_stack or val <= self.min_stack[-1]:
self.min_stack.append(val)
def pop(self):
if self.stack[-1] == self.min_stack[-1]:
self.min_stack.pop()
return self.stack.pop()
def get_min(self):
return self.min_stack[-1]
```
4. 给定一个升序排列的数组和一个目标值,查找目标值在数组中的插入位置。
答案:
```python
def search_insert(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return left
```
5. 给定一个链表,反转链表。
答案:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_list(head):
prev = None
curr = head
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
return prev
```
6. 给定一个字符串,找出其中出现次数最多的字符。
答案:
```python
def most_frequent_char(s):
count = {}
for c in s:
if c in count:
count[c] += 1
else:
count[c] = 1
max_count = max(count.values())
return [k for k, v in count.items() if v == max_count][0]
```
7. 实现一个队列,包括 enqueue、dequeue 和 get_front 方法。
答案:
```python
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, val):
self.queue.append(val)
def dequeue(self):
return self.queue.pop(0)
def get_front(self):
return self.queue[0]
```
8. 给定一个字符串,找出其中第一个不重复的字符。
答案:
```python
def first_unique_char(s):
count = {}
for c in s:
if c in count:
count[c] += 1
else:
count[c] = 1
for c in s:
if count[c] == 1:
return c
return None
```
9. 给定一个二叉树,计算树的深度。
答案:
```python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def tree_depth(root):
if not root:
return 0
left_depth = tree_depth(root.left)
right_depth = tree_depth(root.right)
return max(left_depth, right_depth) + 1
```
10. 给定一个数组,找到数组中第 k 大的元素。
答案:
```python
def find_kth_largest(arr, k):
arr.sort()
return arr[-k]
```
希望这些题目和答案能对您有所帮助。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)