diff --git a/Week_01/id_36/LeeCode_1047_36.py b/Week_01/id_36/LeeCode_1047_36.py new file mode 100644 index 00000000..d315735f --- /dev/null +++ b/Week_01/id_36/LeeCode_1047_36.py @@ -0,0 +1,18 @@ +# +# @lc app=leetcode id=1047 lang=python3 +# +# [1047] 删除字符串中的所有相邻重复项 +# +# 思路 : 判断栈顶的元素和即将加入的元素是否相等,相等则弹出栈顶,否则压入栈顶 +# +class Solution: + def removeDuplicates(self, S: str) -> str: + stack = ['-'] + for s in S: + if s == stack[-1]: + stack.pop() + else: + stack.append(s) + + return ''.join(stack[1:]) + diff --git a/Week_01/id_36/LeeCode_242_36.py b/Week_01/id_36/LeeCode_242_36.py new file mode 100644 index 00000000..c626c487 --- /dev/null +++ b/Week_01/id_36/LeeCode_242_36.py @@ -0,0 +1,23 @@ +# +# @lc app=leetcode.cn id=242 lang=python3 +# +# [242] 有效的字母异位词 +# +# 思路 : 空间换时间 +# +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + l_list = [0]*(26+97) + + for i in s: + l_list[ord(i)] += 1 + + for i in t: + l_list[ord(i)] -= 1 + + for i in l_list: + if i != 0: + return False + + return True + diff --git a/Week_01/id_36/LeetCode_26_36.py b/Week_01/id_36/LeetCode_26_36.py new file mode 100644 index 00000000..c3b50f58 --- /dev/null +++ b/Week_01/id_36/LeetCode_26_36.py @@ -0,0 +1,21 @@ +# +# @lc app=leetcode.cn id=26 lang=python3 +# +# [26] 删除排序数组中的重复项 +# +# 笔记 : 读题目的时候,受到题目中的删除这个词的影响,陷入到如何删除数组中的某一个元素,实际上题目是要求返回不同元素的个数 +# +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + n, count = len(nums), 0 + + if n < 2: + return n + + for i in range(1, n): + if nums[i] != nums[count]: + count += 1 + nums[count] = nums[i] + + return count + 1 + diff --git a/Week_01/id_36/NOTE.md b/Week_01/id_36/NOTE.md index 107ea7d6..bd79c8e6 100644 --- a/Week_01/id_36/NOTE.md +++ b/Week_01/id_36/NOTE.md @@ -1 +1,7 @@ # 学习笔记 + +这是我第一周的学习实战,我专业是学软件工程的,但是由于大学的时候没重视数据结构与算法,那时候只当应付考试了。现在工作快五年了,目前的岗位是技术管理,敲代码的时间越来越少,但是我不想放弃编程,所以我就从算法打基础开始,以备将来跳槽之需。 + +目前来到极客大学的算法集训营,就是希望给自己压力去突破这个算法,从老师那里学到了实用的五毒神掌的步骤,确实对算法的学习带来了很大的帮助,也希望接下来的算法作业自己能够多多的突破和解锁更多的思维方式。 + +在做《26.删除排序数组中的重复项》题目的时候,读题目的时候,受到题目中的删除这个词的影响,陷入到如何删除数组中的某一个元素,实际上题目是要求返回不同元素的个数。所以第一步需要先理解题目。所以读懂题目才是接下来思考的第一步。 diff --git "a/Week_01/id_36/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.xmind" "b/Week_01/id_36/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.xmind" new file mode 100644 index 00000000..b477d935 Binary files /dev/null and "b/Week_01/id_36/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.xmind" differ