1. 四舍六入五成双规则
输入:1234
输出:1234
12 12.0
4 4.00
0.2 0.200
0.32 0.320
1.3 1.30
1.235 1.24
1.245 1.24
1.2451 1.25
出处:
https://edu.csdn.net/practice/26046539
代码:
nums = [1234,12,4,0.2,0.32,1.3,1.235,1.245,1.2451] for n in nums: a = str(n) if '.' in a: a = float(a) if a*1000%10!=5: a = '%.2f'%(a) else: if len(str(a).split(".")[1])>3: a = '%.2f'%(a) else: if int(a*100%10%2)==1: a = float('%.2f'%float(int(a*100)/100))+0.01 else: a = '%.2f'%float(int(a*100)/100) print(a) else: a = int(a) if a>99: print(a) else: if 0 < a < 10: print('%.2f'%a) else: print(float(a))
输出:
1234
12.0
4.00
0.20
0.32
1.30
1.24
1.24
1.25
四舍六入五成双规则:
对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,也即“4舍6入5凑偶”
“四”是指≤4 时舍去,
“六”是指≥6时进上,
“五”指的是根据5后面的数字来定:
1. 当5后有数时,舍5入1;
2. 当5后无有效数字时,需要分两种情况来讲:
(1)5前为奇数,舍5入1;
(2)5前为偶数,舍5不进(0是偶数)。
2. 编辑距离
给你两个单词 word1
和 word2
,请你计算出将 word1
转换成 word2
所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
- 插入一个字符
- 删除一个字符
- 替换一个字符
示例 1:
输入:word1 = "horse", word2 = "ros"
输出:3
解释:horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')
示例 2:
输入:word1 = "intention", word2 = "execution"
输出:5
解释:intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')
提示:
0 <= word1.length, word2.length <= 500
word1
和word2
由小写英文字母组成
以下程序实现了这一功能,请你填补空白处内容:
``` class Solution(object): def minDistance(self, word1, word2): ls_1, ls_2 = len(word1), len(word2) dp = list(range(ls_1 + 1)) for j in range(1, ls_2 + 1): pre = dp[0] dp[0] = j for i in range(1, ls_1 + 1): temp = dp[i] if word1[i - 1] == word2[j - 1]: dp[i] = pre else: ____________________________; pre = temp return dp[ls_1] if __name__ == '__main__': s = Solution() print (s.minDistance("horse","ros")) print (s.minDistance("intention","execution")) ```
出处:
https://edu.csdn.net/practice/24915040
代码:
class Solution(object): def minDistance(self, word1, word2): ls_1, ls_2 = len(word1), len(word2) dp = list(range(ls_1 + 1)) for j in range(1, ls_2 + 1): pre = dp[0] dp[0] = j for i in range(1, ls_1 + 1): temp = dp[i] if word1[i - 1] == word2[j - 1]: dp[i] = pre else: dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1) pre = temp return dp[ls_1] if __name__ == '__main__': s = Solution() print (s.minDistance("horse","ros")) print (s.minDistance("intention","execution"))
输出:
3
5
3. 分割回文串
给你一个字符串 s
,请你将 s
分割成一些子串,使每个子串都是 回文串 。返回 s
所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a"
输出:[["a"]]
提示:
1 <= s.length <= 16
s
仅由小写英文字母组成
出处:
https://edu.csdn.net/practice/24915041
代码:
class Solution(object): def partition(self, s): """ :type s: str :rtype: List[List[str]] """ if len(s) == 0: return [] else: res = [] self.dividedAndsel(s, [], res) return res def dividedAndsel(self, s, tmp, res): if len(s) == 0: res.append(tmp) for i in range(1, len(s) + 1): if s[:i] == s[:i][::-1]: self.dividedAndsel(s[i:], tmp + [s[:i]], res) if __name__ == '__main__': s = Solution() print (s.partition("aab")) print (s.partition("a"))
输出:
[['a', 'a', 'b'], ['aa', 'b']]
[['a']]
方法2:
from typing import List class Solution: def partition(self, s: str) -> List[List[str]]: res = [] self.dfs(s, [], res) return res def dfs(self, s, path, res): if not s: res.append(path) return for i in range(1, len(s)+1): if self.is_palindrome(s[:i]): self.dfs(s[i:], path+[s[:i]], res) def is_palindrome(self, s): return s == s[::-1] if __name__ == '__main__': s = Solution() print (s.partition("aab")) print (s.partition("a"))
🌟 每日一练刷题专栏 🌟
✨持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/