diff --git a/notes/面试总结.md b/notes/面试总结.md index 82974d9a..68dfab57 100644 --- a/notes/面试总结.md +++ b/notes/面试总结.md @@ -22,6 +22,8 @@ * [18.1 在 O(1) 时间内删除链表节点](#181-在-o1-时间内删除链表节点) * [18.2 删除链表中重复的结点](#182-删除链表中重复的结点) * [19. 正则表达式匹配](#19-正则表达式匹配) +* [20. 表示数值的字符串](#20-表示数值的字符串) +* [21. 调整数组顺序使奇数位于偶数前面](#21-调整数组顺序使奇数位于偶数前面) * [参考文献](#参考文献) @@ -1571,6 +1573,115 @@ class Solution: else: return True ``` +# 20. 表示数值的字符串 + +[NowCoder](https://www.nowcoder.com/practice/6f8c901d091949a5837e24bb82a731f2?tpId=13&tqId=11206&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) + +## 题目描述 + +```html +true + +"+100" +"5e2" +"-123" +"3.1416" +"-1E-16" + +false + +"12e" +"1a3.14" +"1.2.3" +"+-5" +"12e+4.3" +``` + + +## 解题思路 + +使用正则表达式进行匹配。 + +```html +[] : 字符集合 +() : 分组 +? : 重复 0 ~ 1 ++ : 重复 1 ~ n +* : 重复 0 ~ n +. : 任意字符 +\\. : 转义后的 . +\\d : 数字 +``` + +```java +public boolean isNumeric(char[] str) { + if (str == null || str.length == 0) + return false; + return new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?"); +} +``` + +```python +# -*- coding:utf-8 -*- +import re +class Solution: + # s字符串 + def isNumeric(self, s): + # write code here + return re.match('[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?$', s) +``` + +# 21. 调整数组顺序使奇数位于偶数前面 + +[NowCoder](https://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) + +## 题目描述 + +需要保证奇数和奇数,偶数和偶数之间的相对位置不变,这和书本不太一样。 + +## 解题思路 + +```java +public void reOrderArray(int[] nums) { + // 奇数个数 + int oddCnt = 0; + for (int val : nums) + if (val % 2 == 1) + oddCnt++; + int[] copy = nums.clone(); + int i = 0, j = oddCnt; + for (int num : copy) { + if (num % 2 == 1) + nums[i++] = num; + else + nums[j++] = num; + } +} +``` + +```python +# -*- coding:utf-8 -*- +import copy + +class Solution: + def reOrderArray(self, array): + # write code here + oddCnt = 0 + for val in array: + if val % 2 == 1: + oddCnt += 1 + copyArray = copy.deepcopy(array) + i, j = 0, oddCnt + for num in array: + if num % 2 ==1: + copyArray[i] = num + i += 1 + else: + copyArray[j] = num + j += 1 + return copyArray +``` + # 参考文献