
算法
Lsang_
这个作者很懒,什么都没留下…
展开
-
[leetcode]C函数参数
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). *///返回一维数组, returnSize是指向size的指针int* func(int rowIndex, int* returnSize) { int...原创 2019-03-31 14:38:13 · 906 阅读 · 0 评论 -
[leetcode]22. 括号生成
1. 题目:数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。示例:输入:n = 3输出:[ "((()))", "(()())", "(())()", "()(())", "()()()" ]2. 代码:DFS回溯class Solution: def generateParenthesis(self, n: int) -> List[str]:原创 2020-10-25 16:58:35 · 186 阅读 · 0 评论 -
[leetcode] 面试题 08.07. 无重复字符串的排列组合
1. 题目:无重复字符串的排列组合。编写一种方法,计算某字符串的所有排列组合,字符串每个字符均不相同。示例1: 输入:S = "qwe" 输出:["qwe", "qew", "wqe", "weq", "ewq", "eqw"]2. 代码:DFS 模版+ used[]class Solution: def permutation(self, S: str) -> List[str]: lens = len(S) if lens == 0:原创 2020-10-25 16:53:36 · 367 阅读 · 0 评论 -
[leetcode]17. 电话号码的字母组合
1. 题目:给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].2. 代码:递归回溯class Solution: def letterCombinations(self, digits: str) -> List[str]: lens = le原创 2020-10-25 16:23:42 · 343 阅读 · 0 评论 -
[leetcode]24. 两两交换链表中的节点
1.题目:给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3.2.代码:/** * Definition for singly-linked list. * struct ListNode {...原创 2019-04-10 13:06:43 · 109 阅读 · 0 评论 -
[leetcode]23. 合并K个排序链表
1.题目:合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。示例:输入:[ 1->4->5, 1->3->4, 2->6]输出: 1->1->2->3->4->4->5->62.代码:/** * Definition for singly-linked list. * s...原创 2019-04-09 13:49:23 · 143 阅读 · 0 评论 -
[leetcode]46. 全排列
1.题目:给定一个没有重复数字的序列,返回其所有可能的全排列。示例:输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]2.代码:/** * Return an array of arrays of size *returnSize. * Note: The ret...原创 2019-04-14 15:40:40 · 152 阅读 · 0 评论 -
[leetcode]51. N 皇后
1.题目:n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。输入:4输出:[ [".Q..", // 解法 1 "...Q", "Q...", "..Q."], ["..Q.", // 解法 2 "Q...", "...Q", ".Q.."]]解释: 4 皇后问题存在两个不同的解法。2.代码:class Solution: def solveNQueens(self, n: int) -> List原创 2020-10-18 21:02:30 · 245 阅读 · 0 评论 -
[leetcode]1. 两数之和
1.题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]2.代码:class Solution: def twoSum(self, nums: List[int], target原创 2020-10-18 20:04:41 · 172 阅读 · 0 评论 -
[leetcode]92. 反转链表 II
1.题目:反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。说明: 1 ≤ m ≤ n ≤ 链表长度。输入: 1->2->3->4->5->NULL, m = 2, n = 4输出: 1->4->3->2->5->NULL2.代码:# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.v原创 2020-10-18 19:47:14 · 214 阅读 · 0 评论 -
[leetcode]220. 存在重复元素 III
1.题目:在整数数组 nums 中,是否存在两个下标 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值小于等于 t ,且满足 i 和 j 的差的绝对值也小于等于 ķ 。如果存在则返回 true,不存在返回 false。输入: nums = [1,5,9,1,5,9], k = 2, t = 3输出: false2.代码:分析:与(219)存在重复元素 II不同在于在k窗口基础上,将相等改为绝对值。==> 哈希表 + 桶排序:"""用哈希表模拟桶,hash[nu原创 2020-10-05 20:45:15 · 263 阅读 · 0 评论 -
[leetcode]219. 存在重复元素 II
1.题目:给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的 绝对值 至多为 k。输入: nums = [1,2,3,1,2,3], k = 2输出: false2.代码:'''要维护一个长度为k的窗口,只用考虑窗口内是否有重复;保证i-HashDict[num]<=k,即当前num与dict内相同num下标差<=k'''class Solution: def conta原创 2020-10-05 20:11:04 · 267 阅读 · 1 评论 -
α-β剪枝算法解释
剪枝算法解释:(3)=min((4)=3,(5)=17)=3;(2)=max((3)=3、(6)=?) ==>求(6);(6)=min((7)=2,(8)=?) ==>(6)<=2;==>(2)=max((3)=3、(6)<=2)=3 ,即只要访问了(7)就确定了(2)的值,(8)就不用访问了,即剪枝;算法详细信息,参考:解释1、解释2;...原创 2020-05-03 02:53:00 · 1956 阅读 · 0 评论 -
[leetcode]36. 有效的数独
1.题目:判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。即:数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次示例 :输入:[ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5...原创 2019-04-29 14:22:44 · 127 阅读 · 0 评论 -
[leetcode]328. 奇偶链表
1.题目:给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。示例 :输入: 2->1->3->5->6->4->7->NULL 输出: 2-&...原创 2019-05-06 01:04:07 · 99 阅读 · 0 评论 -
[leetcode]203. 移除链表元素
1.题目:删除链表中等于给定值 val 的所有节点。示例:输入: 1->2->6->3->4->5->6, val = 6输出: 1->2->3->4->52.代码:/** * Definition for singly-linked list. * struct ListNode { * int val;...原创 2019-05-14 13:01:30 · 137 阅读 · 0 评论 -
[leetcode]876. 链表的中间结点(快慢指针)
数据结构:快慢指针1.题目:给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。2.代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; *//*快...原创 2019-05-14 14:06:21 · 180 阅读 · 0 评论 -
[leetcode]485. 最大连续1的个数
1.题目:给定一个二进制数组, 计算其中最大连续1的个数。Given a binary array, find the maximum number of consecutive 1s in this array.2.代码:int findMaxConsecutiveOnes(int* nums, int numsSize) { int n=0,max=0; for(int...原创 2019-03-05 23:02:45 · 149 阅读 · 0 评论 -
[leetcode]92. 反转链表 II
1.题目:反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。说明:1 ≤ m ≤ n ≤ 链表长度。示例:输入: 1->2->3->4->5->NULL, m = 2, n = 4输出: 1->4->3->2->5->NULL2.代码:/** * Definition for singly-linked lis...原创 2019-05-09 15:46:48 · 111 阅读 · 0 评论 -
数组循环左移
1.题目:将n个整数存放到一维数组R中。试设计一个在时间和空间都尽可能高效的算法。将R中保存的序列循环左移P个位置,即将R中数据由[X0,X1, …,Xn-1]变换为[Xp,Xp+1,…,Xn-1,X0,X1,…,Xp-1]。2.代码:/*思路:即[a,b]=>[b,a],a为前p个元素,左移之后到右边。[a,b] =>[a^-1,b] =>[a^-1,b^-1] =&...原创 2019-07-01 23:41:19 · 1065 阅读 · 0 评论 -
求两个等长升序序列中位数
1.题目:如题;2.代码:/*结论:分别求两个升序序列A,B的中位数,设为a和b,求序列A,B的中位数过程如下:1)若a=b,则a或b即为所求;2)若a<b,则舍弃A中较小的一半,B中较大的一半,要求两次舍弃长度相等;3)若a>b,则舍弃A中较大的一半,B中较小的一半,要求两次舍弃长度相等;重复1),2),3),直到两个序列中都只剩一个元素,较小的即为所求。-----...原创 2019-07-02 23:49:13 · 785 阅读 · 0 评论 -
找出两个单链表的公共节点
1.题目:如题。2.代码:/*假设链表A比B长k,让A先走k个节点,再同时遍历。*/LinkList search_common(LinkList L1,LinkList L2){ int len1=Length(L1); int len2=Lenght(L2); LinkLIst shortList,longList; if(Len1>Len2){ longList...原创 2019-07-03 22:08:38 · 1583 阅读 · 1 评论 -
[leetcode]48. 旋转图像
1.题目:给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。说明:你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。示例输入: [ [0,1,2,0], [3,4,5,2], [1,3,1,5]]输出: [ [0,0,0,0], [0,4,5,0], [0,3,1,0]]2.代码:...原创 2019-04-28 14:44:26 · 153 阅读 · 0 评论 -
[leetcode]237. 删除链表中的节点
1.题目:请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。现有一个链表 – head = [4,5,1,9],它可以表示为:示例 :输入: head = [4,5,1,9], node = 5输出: [4,1,9]解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.2代...原创 2019-04-01 10:18:53 · 136 阅读 · 0 评论 -
[leetcode]147. 对链表进行插入排序
1.题目:插入排序算法:插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。.每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。.重复直到所有输入数据插入完为止。2.代码:/** * Definition for singly-linked list. * struct ListNode { * i...原创 2019-03-15 15:29:49 · 532 阅读 · 0 评论 -
[leetcode]35. 搜索插入位置
1.题目:给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。Given a sorted array and a target value, return the index if the target is found. If not, return the index where it wo...原创 2019-03-03 18:47:46 · 150 阅读 · 0 评论 -
[leetcode]21. 合并两个有序链表
1.题目:将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.2.代码:...原创 2019-03-03 18:29:46 · 117 阅读 · 0 评论 -
[leetcode]82. 删除排序链表中的重复元素 II
1.题目:给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.2.代码:/** * Definitio...原创 2019-03-03 18:14:38 · 149 阅读 · 0 评论 -
[leetcode]2. 两数相加
1.题目:给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 8072....原创 2019-03-06 10:22:10 · 116 阅读 · 0 评论 -
[leetcode]905. 按奇偶排序数组
1.题目:给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.2.代码:...原创 2019-03-05 22:28:28 · 119 阅读 · 0 评论 -
[leetcode]83. 删除排序链表中的重复元素
1.题目:给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。Given a sorted linked list, delete all duplicates such that each element appear only once.2.代码:/** * Definition for singly-linked list. * struct ListNode { *...原创 2019-03-02 15:05:11 · 125 阅读 · 0 评论 -
[leetcode]9. 回文数
1.题目:判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward asforward.2.代码:bool isPalindrome(int x) {...原创 2019-03-02 14:05:07 · 144 阅读 · 0 评论 -
[leetcode]7. int型整数反转
1.题目:给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。Given a 32-bit signed integer, reverse digits of an integer.2.代码:#include"limits.h"int reverse(int x) { int rev=0; while(x!=0){ int pop=x...原创 2019-02-20 21:45:29 · 268 阅读 · 0 评论 -
[leetcode]14. 最长公共前缀
1.题目:编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 “”。Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.2.代码:...原创 2019-03-03 20:11:24 · 137 阅读 · 0 评论 -
[leetcode]445. 两数相加 II
1.题目:给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。示例:输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)输出: 7 -> 8 -> 0 -> 72.代码:思想:将两个链表反转实现对齐->之前的题型,最后再反转回来。/...原创 2019-03-06 16:07:39 · 106 阅读 · 0 评论 -
[leetcode]771. 宝石与石头
1.题目:给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。2.代码:int numJewelsInStones(char* J, char* S) { int n=0; i...原创 2019-03-06 16:48:45 · 118 阅读 · 0 评论 -
[leetcode]119. 杨辉三角 II
1.题目:给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行11 11 2 11 3 3 1示例:输入: 3输出: [1,3,3,1]2.代码:/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller call...原创 2019-03-31 14:25:51 · 257 阅读 · 0 评论 -
[leetcode]118. 杨辉三角
1.题目:给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。11 11 2 11 3 3 1输入: 5输出:[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]2.代码:/** * Return an array of arrays. * The sizes of the ar...原创 2019-03-31 14:23:39 · 143 阅读 · 0 评论 -
[leetcode]217. 存在重复元素
1.题目:给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。2.代码:int compare(const void* a, const void* b) { return (*(int*)a)-(*(int*)b);}bool containsDuplicate(int* nums, i...原创 2019-03-26 21:17:44 · 141 阅读 · 0 评论 -
[leetcode]561. 数组拆分 I
1.题目:给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), …, (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。示例 :输入: [1,4,3,2]输出: 4解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4).2.代码:int compare(const v...原创 2019-03-30 09:23:33 · 277 阅读 · 0 评论