add #4 python implement
This commit is contained in:
parent
f52cbfd47b
commit
1ac3cb20a0
129
notes/面试总结.md
129
notes/面试总结.md
@ -2,6 +2,8 @@
|
||||
* [1. 字符串组合](#1-字符串组合)
|
||||
* [2. 整数组合求和](#2-整数组合求和)
|
||||
* [3. 数组中重复的数字](#3-数组中重复的数字)
|
||||
* [4. 二维数组中的查找](#4-二维数组中的查找)
|
||||
* [5. 替换空格](#5-替换空格)
|
||||
|
||||
* [参考文献](#参考文献)
|
||||
<!-- GFM-TOC -->
|
||||
@ -177,6 +179,7 @@ private void swap(int[] nums, int i, int j) {
|
||||
int t = nums[i]; nums[i] = nums[j]; nums[j] = t;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
nums = [int(k) for k in raw_input().split(" ")]
|
||||
print nums
|
||||
@ -197,6 +200,132 @@ def duplicate(nums):
|
||||
print duplicate(nums)
|
||||
```
|
||||
|
||||
# 4. 二维数组中的查找
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqId=11154&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 题目描述
|
||||
|
||||
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
|
||||
|
||||
```html
|
||||
Consider the following matrix:
|
||||
[
|
||||
[1, 4, 7, 11, 15],
|
||||
[2, 5, 8, 12, 19],
|
||||
[3, 6, 9, 16, 22],
|
||||
[10, 13, 14, 17, 24],
|
||||
[18, 21, 23, 26, 30]
|
||||
]
|
||||
|
||||
Given target = 5, return true.
|
||||
Given target = 20, return false.
|
||||
```
|
||||
|
||||
## 解题思路
|
||||
|
||||
从右上角开始查找。矩阵中的一个数,它左边的数都比它小,下边的数都比它大。因此,从右上角开始查找,就可以根据 target 和当前元素的大小关系来缩小查找区间。
|
||||
|
||||
复杂度:O(M + N) + O(1)
|
||||
|
||||
当前元素的查找区间为左下角的所有元素,例如元素 12 的查找区间如下:
|
||||
|
||||
<div align="center"> <img src="../pics//f94389e9-55b1-4f49-9d37-00ed05900ae0.png" width="250"/> </div><br>
|
||||
|
||||
```java
|
||||
public boolean Find(int target, int[][] matrix) {
|
||||
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
|
||||
return false;
|
||||
int rows = matrix.length, cols = matrix[0].length;
|
||||
int r = 0, c = cols - 1; // 从右上角开始
|
||||
while (r <= rows - 1 && c >= 0) {
|
||||
if (target == matrix[r][c])
|
||||
return true;
|
||||
else if (target > matrix[r][c])
|
||||
r++;
|
||||
else
|
||||
c--;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
target = int(input())
|
||||
|
||||
# nums = [[1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]]
|
||||
|
||||
nums = eval(input())
|
||||
|
||||
def find(target, matrix):
|
||||
if len(matrix) == 0 or len(matrix[0]) == 0:
|
||||
return False
|
||||
|
||||
rows, cols = len(matrix), len(matrix[0])
|
||||
r, c = 0, cols - 1
|
||||
|
||||
while r <= rows - 1 and c >= 0:
|
||||
if target == matrix[r][c]:
|
||||
return True
|
||||
elif target > matrix[r][c]:
|
||||
r += 1
|
||||
else:
|
||||
c -= 1
|
||||
return False
|
||||
|
||||
print (find(target, nums))
|
||||
```
|
||||
|
||||
# 5. 替换空格
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 题目描述
|
||||
|
||||
|
||||
将一个字符串中的空格替换成 "%20"。
|
||||
|
||||
```text
|
||||
Input:
|
||||
"We Are Happy"
|
||||
|
||||
Output:
|
||||
"We%20Are%20Happy"
|
||||
```
|
||||
|
||||
## 解题思路
|
||||
|
||||
在字符串尾部填充任意字符,使得字符串的长度等于替换之后的长度。因为一个空格要替换成三个字符(%20),因此当遍历到一个空格时,需要在尾部填充两个任意字符。
|
||||
|
||||
令 P1 指向字符串原来的末尾位置,P2 指向字符串现在的末尾位置。P1 和 P2从后向前遍历,当 P1 遍历到一个空格时,就需要令 P2 指向的位置依次填充 02%(注意是逆序的),否则就填充上 P1 指向字符的值。
|
||||
|
||||
从后向前遍是为了在改变 P2 所指向的内容时,不会影响到 P1 遍历原来字符串的内容。
|
||||
|
||||
```java
|
||||
public String replaceSpace(StringBuffer str) {
|
||||
int P1 = str.length() - 1;
|
||||
for (int i = 0; i < P1 + 1; i++)
|
||||
if (str.charAt(i) == ' ')
|
||||
str.append(" ");
|
||||
|
||||
int P2 = str.length() - 1;
|
||||
while (P1 >= 0 && P2 > P1) {
|
||||
char c = str.charAt(P1--);
|
||||
if (c == ' ') {
|
||||
str.setCharAt(P2--, '0');
|
||||
str.setCharAt(P2--, '2');
|
||||
str.setCharAt(P2--, '%');
|
||||
} else {
|
||||
str.setCharAt(P2--, c);
|
||||
}
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
# 参考文献
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user