算法LeetCode
jj_千寻
qq:425776024
展开
-
python算法-2整形数组-1Leetcode 027 Remove
python算法-2整形数组-1Leetcode 027 Remove Element在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_027_Remove_Element.htmlclass Solution: def call(self, arr, elem): i = 0 j = 0 while i < len(arr): # 如果相当则..原创 2020-07-19 21:56:05 · 292 阅读 · 0 评论 -
python算法-1字符串-10数数Leetcode 038 Count and Say
python算法-1字符串-10数数Leetcode 038 Count and Say在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_038_Count_and_Say.htmldef countAndSay(n): if n == 0: return '' res = '3' while n != 0: n -= 1 i = 0 cou..原创 2020-07-19 21:55:04 · 253 阅读 · 0 评论 -
python算法-1字符串-9 Leetcode 044Wildcard_Matching
python算法-1字符串-9 Leetcode 044Wildcard在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_044_4Wildcard_Matching.htmldef isMatch2( s, p): s = '0' + s p = '0' + p # dp[i][j]表示:s的前i个字符与p的前j个字符是否匹配 dp = [[False for _ in range(len(p)..原创 2020-07-19 21:52:43 · 199 阅读 · 0 评论 -
python算法-1字符串-8空格替换
python算法-1字符串-8空格替换def replaceBlank(str, length): s = '' for i in range(length): if str[i] == ' ': str[i] = '%20' s += str[i] return sif __name__ == '__main__': str = "Mr John Smith" str = list(str).原创 2020-07-19 21:51:45 · 275 阅读 · 0 评论 -
python算法-1字符串-7最长回文子串Leetcode_005_Longest_Palindromic_Substring
python算法-1字符串-7最长回文子串Leetcode在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_005_Longest_Palindromic_Substring.html# 1穷举class Solution: def str(self, s): if not s: return '' n = len(s) logest, left,..原创 2020-07-19 21:50:41 · 201 阅读 · 0 评论 -
python算法-1字符串-6回文串判断Leetcode_125_Valid_Palindrom
python算法-1字符串-6回文串判断Leetcode在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_125_Valid_Palindrome.htmlclass Solution(object): def isPalindrome(self, s): """ :type s: str :rtype: bool """ alnum_s = [t.原创 2020-07-19 21:49:14 · 187 阅读 · 0 评论 -
python算法-1字符串-5翻转单词
python算法-1字符串-5翻转单词在线运行:https://pyleetcode.gitee.io/codes_html/SYL_1%E5%AD%97%E7%AC%A6%E4%B8%B2_4%E7%BF%BB%E8%BD%AC%E5%8D%95%E8%AF%8D.htmlclass Solution: def str(self, s): if len(s) == 0: return '' temp = [] ..原创 2020-07-19 21:47:51 · 229 阅读 · 0 评论 -
python算法-1字符串-4 Leetcode 1143 Longest Common
python算法-1字符串-4 Leetcode 1143 Longest Common Subsequence最长公共子序列在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_1143_Longest_Common_Subsequence.htmldef printflsc(x, y): global flag, string1, string2, s # 回溯输出最长公共子序列 if x == 0 ..原创 2020-07-19 21:46:49 · 219 阅读 · 0 评论 -
python算法-1字符串-4Longest Common Substring最长公共子串
python算法-1字符串-4Longest Common Substring最长公共子串在线运行:https://pyleetcode.gitee.io/codes_html/SYL_1%E5%AD%97%E7%AC%A6%E4%B8%B2_10Longest%20Common%20Substring.htmldef LCstring(string1, string2): len1 = len(string1) len2 = len(string2) r..原创 2020-07-19 21:45:18 · 310 阅读 · 0 评论 -
python算法-1字符串-3字符串是否是子集
python算法-1字符串-3字符串是否是子集在线运行:https://pyleetcode.gitee.io/codes_html/SYL_1%E5%AD%97%E7%AC%A6%E4%B8%B2_3%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%98%AF%E5%AD%90%E9%9B%86.htmlimport collectionsclass Solution: def compare_strings(self,..原创 2020-07-19 21:43:02 · 281 阅读 · 0 评论 -
python算法-1字符串-2判断是否同一个字符串
python算法-1字符串-2判断是否同一个字符串python算法-1字符串-2判断是否同一个字符串在线运行:https://pyleetcode.gitee.io/codes_html/SYL_1%E5%AD%97%E7%AC%A6%E4%B8%B2_2%E6%98%AF%E5%90%A6%E5%90%8C%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2.htmlimport collectionsclass Solution: .原创 2020-07-09 09:20:18 · 791 阅读 · 0 评论 -
python算法-1字符串-1.查找(Leetcode 28 Implement strStr)
python算法-1字符串-1.查找(Leetcode 28 Implement strStr)python算法-1字符串-1.查找(Leetcode 28 Implement strStr)在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_028_Implement_strStr.htmlclass Solution: def strStr(self, source, target): if sou..原创 2020-07-09 09:18:08 · 166 阅读 · 0 评论 -
python算法-0排序-7堆排序
python算法-0排序-7堆排序python算法-0排序-7堆排序在线运行:https://pyleetcode.gitee.io/codes_html/SYL_0%E6%8E%92%E5%BA%8F_7%E5%A0%86%E6%8E%92%E5%BA%8F.htmldef sort(array): # 遍历非叶子节点,建立堆结构数组 for i in range(int(len(array) / 2) - 1, -1, -1): adjustHeap.原创 2020-07-09 09:15:47 · 232 阅读 · 1 评论 -
python算法-0排序-6归并排序
python算法-0排序-6归并排序python算法-0排序-6归并排序在线运行:https://pyleetcode.gitee.io/codes_html/SYL_0%E6%8E%92%E5%BA%8F_6%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F.htmldef merge(a, b): c = [] h = j = 0 while j < len(a) and h < len(b): # 谁小插入.原创 2020-07-09 09:14:45 · 205 阅读 · 1 评论 -
python算法-0排序-5.快速排序
python算法-0排序-5.快速排序python算法-0排序-5.快速排序在线运行:https://pyleetcode.gitee.io/codes_html/SYL_0%E6%8E%92%E5%BA%8F_5%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F.htmldef Partion(arr, left, right): # 一次对比操作,返回中间索引(索引前面比他小,后面比他大) tag = right # 作为判断点,一...原创 2020-07-09 09:13:21 · 205 阅读 · 0 评论 -
python算法-0排序-4.希尔排序
python算法-0排序-4.希尔排序python算法-0排序-4.希尔排序在线运行:https://pyleetcode.gitee.io/codes_html/SYL_0%E6%8E%92%E5%BA%8F_4%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F.htmldef shell(data): size = len(data) # 最初,一半长度的跳跃对比 jmp = size // 2 while jmp != ..原创 2020-07-09 09:12:16 · 156 阅读 · 0 评论 -
python算法-0排序-3.插入排序
python算法-0排序-3.插入排序python算法-0排序-3.插入排序在线运行:https://pyleetcode.gitee.io/codes_html/SYL_0%E6%8E%92%E5%BA%8F_3%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F.html# 插入排序,o(n^2)def Sort(arr): n = len(arr) for i in range(1, n): # 第一个数直接进入数组 ..原创 2020-07-09 09:10:56 · 155 阅读 · 0 评论 -
python算法-0排序-2冒泡排序
python算法-0排序-2冒泡排序python算法-0排序-2冒泡排序https://www.bilibili.com/video/BV11v411B7Eyhttps://pyleetcode.gitee.io/codes_html/SYL_0%E6%8E%92%E5%BA%8F_2%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F.htmldef Sort(arr): for i in range(len(arr)): for..原创 2020-07-09 09:07:52 · 172 阅读 · 0 评论 -
python算法-0排序-1选择排序
<iframe src="//player.bilibili.com/player.html?aid=371135586&bvid=BV1wZ4y1p7Fe&cid=207954024&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>原创 2020-07-09 09:03:55 · 220 阅读 · 0 评论 -
数组-(sum close)找到数组中和与k最接近的3个数字
class Solution: ''' 找到数组中结果与k接近的3个元素 ''' def threeSumClosest(self, numbers, target): # 排序 numbers.sort() # 最接近的和 ans = None # 结果列表 res...原创 2019-03-15 22:28:53 · 973 阅读 · 0 评论 -
数组—找出数组的第k大的数
'''找出数组的第k大的数:[6,7,8,9, 3, 2, 4, 8]第3大的数是4'''class Solution: def call(self, nums, k): if nums == None or len(nums) == 0: return -1 result = self.qsort(nums, 0, le...原创 2019-03-15 22:47:52 · 1277 阅读 · 2 评论 -
数组-(Remove Element)移除指定数字返回新的数组
'''Given an array and a value, remove all occurrences of that value in place and return the new length.The order of elements can be changed, and the elements after the new length don't matter.Ex...原创 2019-03-01 17:42:15 · 1098 阅读 · 0 评论 -
1.(字符串)-空格替换
如: a b s,替换:%20%20%20a%20b%20spython:def replaceBlank(str, length): s = '' for i in range(length): if str[i] == ' ': str[i] = '%20' s += str[i] return s...转载 2019-02-12 21:14:37 · 204 阅读 · 0 评论 -
数组-(3 Sum of k)找到数组中和为k的3个数字
'''在数组中找到3个元素的索引,满足和为k,且找到多个解'''class Solution: def call(self, nums, target): ''' 在nums中找到两个数,满足和为target :param nums: :param target: :return: ...原创 2019-03-15 22:12:39 · 718 阅读 · 0 评论 -
数组-(Sum)找到和为k的两个数字
'''找出数组中和为某个数的两个数的序号思路:和为k等价与k-a=b,建立字典,遍历每个元素ai如果k-ai不存在字典中就记下ai的位置i来,到下一个数如果k-aj=ai在字典中,那么结果就是(i,j)'''class Solution: def call(self, nums, target): hashset = {} for i, m ...原创 2019-03-15 22:00:27 · 2343 阅读 · 0 评论 -
数组-(First Missing Positive)首个缺失的正数
'''Given an unsorted integer array, find the smallest missing positive integer.Example 1:Input: [1,2,0]Output: 3Example 2:Input: [3,4,-1,1]Output: 2Example 3:Input: [7,8,9,11,12]Output:...原创 2019-03-14 22:52:25 · 242 阅读 · 0 评论 -
2.(图)-广度优先遍历BFS
从图的某个节点开始遍历,访问了则标记下来,然后遍历此点邻域中且未访问的点为新起点,且做上访问标记,通常通过递归和队列的技巧实现。1:以1为起点,[2,5]入队列2:取出2,相邻且未标记访问的3,4入[5,3,4]3:取出5,入3,4;[3,4,3,4]4:取出3,入4;[4,3,4,4]5:取出4,剩余[3,4,4]6:[3,4,4]均被标记访问,依次弹...原创 2019-02-16 19:51:04 · 146 阅读 · 0 评论 -
2.(图)-深度优先遍历DFS
从图的某个节点开始遍历,访问了则标记下来,然后访问此点所有邻域中任意点为新起点,且做上访问标记,通常通过递归和栈的技巧实现。1:以1为起点,[5,2]入栈2:弹出上面的2,2相邻且未标记访问的3,4入栈[5,4,3]3:弹3,入4,5;[5,4,5,4]4:弹4,入5;[5,4,5,5]5:弹5,剩余[5,4,5]6:[5,4,5]均被标记访问,依次弹出...原创 2019-02-16 19:50:55 · 196 阅读 · 0 评论 -
2.(图)-邻接表链表表示
class list_node: def __init__(self): self.val = 0 self.next = Nonehead = [list_node] * 6newnode = list_node()data = [[0, 5], [1, 2], [2, 1], [1, 5], [5, 1], [2, 3], [3, 2]...转载 2019-03-03 19:32:30 · 279 阅读 · 0 评论 -
1.(字符串)-计算n个数count-and-say
如:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211.python:def countAndSay(n): if n == 0: ...转载 2019-02-12 22:03:29 · 310 阅读 · 0 评论 -
1.(字符串)-获取最后一个字符串及长度
如:' hello word is good ',返回:good,4python:class Solution: def strc(self, s): end = len(s) #取到最后一个字母所在位置的后一位 while end > 0 and s[end - 1] == ' ': end -= ...原创 2019-02-12 21:35:23 · 1160 阅读 · 0 评论 -
1.(字符串)-获取字符串的最长回文子串
如:'abcdzdcab',返回:cdzdc'abcdzdcab111',返回:cdzdcpython:# 1穷举class Solution: def str(self, s): if not s: return '' n = len(s) logest, left, right = 0, 0, 0 ...原创 2019-02-12 21:09:25 · 310 阅读 · 0 评论 -
1.(字符串)-回文判断
忽略标点空格大小写,判断字母是否回文:如: A man, a plan, a can al: Panama,return true;python:class Solution: def isPalindrome(self, s): if not s: return True l, r = 0, len(s) - 1...原创 2019-02-12 20:08:10 · 172 阅读 · 0 评论 -
1.(字符串)-反转单词
如给定:"the sky is blue",返回: "blue is sky the".(多个空格变成一个空格)python:class Solution: def str(self, s): if len(s) == 0: return '' temp = [] i = 0 while...转载 2019-02-12 20:02:54 · 354 阅读 · 0 评论 -
1.(字符串)-判断字符串是否是子集字符串
python:这里的关键首先是通过collections.defaultdict(int) ,创建默认value是int的字典对象,然后对每一个主串字符统计字母次数;然后对子字符串的每一个字母出现一次就对字典中对应的key字母减1,如果减到小于0或者字母不在字典的key中则返回false,否则全部遍历后返回trueimport collectionsclass Solution:...转载 2019-02-12 19:55:17 · 1634 阅读 · 0 评论 -
1.(字符串)-判断两字符串是否相等
Python:通过collections的Counter类统计字典,转为判断字典是否相等 import collectionsclass Solution: """ @param s: The first string @param b: The second string @return true or false """ def...转载 2019-02-12 19:48:46 · 662 阅读 · 0 评论 -
(树)-二叉树链表表示
1.二叉树新节点的插入:create(root,val) class node: def __init__(self): self.data = 0 self.left = None self.right = Nonedef create(root, val): newnode = node() newnode.d...转载 2019-02-16 19:51:27 · 546 阅读 · 0 评论 -
(树)-二叉树前中后遍历
def inorder(root): ''' 中序遍历 :param root: :return: ''' if root != None: inorder(root.left) print('[%d]' % root.data, end=' ') inorder(root.right)def ...转载 2019-02-16 19:51:18 · 152 阅读 · 0 评论 -
(树)-线索二叉树
class Node: def __init__(self): self.data = 0 self.right = None self.left = None self.left_thread = 0 # 0没有子节点,1有子节点 self.right_thread = 0def add(roo...转载 2019-02-20 22:10:30 · 153 阅读 · 0 评论 -
数组-(partition array)数组划分为左右两个大小区间
'''1:用一个变量right保存<k的数组长度:遇到<k的就赋值到right位置,然后right++2:快速排序的思想'''class Solution: def partitionArray(self, nums, k): right = 0 size = len(nums) for i in range(si...原创 2019-03-14 22:38:37 · 1157 阅读 · 0 评论