随笔分类 -  算法与数据结构

基本的算法代码
INT_MIN绝对值
摘要:求某个int32的绝对值: unsigned int abs_n = n < 0 ? UINT_MAX - ((unsigned int)(n)) + 1U : (unsigned int)(n); Update: As @aka.nice suggests, we can actually rep 阅读全文

posted @ 2021-12-20 21:17 wsw_seu 阅读(47) 评论(0) 推荐(0)

字典树(前缀树)
摘要:实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。 示例: Trie trie = new Trie(); trie.insert("apple");trie.search("apple"); // 返回 truetrie.search("a 阅读全文

posted @ 2020-12-09 20:44 wsw_seu 阅读(114) 评论(0) 推荐(0)

315. Count of Smaller Numbers After Self(二分或者算法导论中的归并求逆序数对)
摘要:You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smal 阅读全文

posted @ 2020-11-15 11:00 wsw_seu 阅读(99) 评论(0) 推荐(0)

329. Longest Increasing Path in a Matrix(核心在于缓存遍历过程中的中间结果)
摘要:Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or do 阅读全文

posted @ 2020-11-08 11:58 wsw_seu 阅读(108) 评论(0) 推荐(0)

451. Sort Characters By Frequency(桶排序)
摘要:Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twi 阅读全文

posted @ 2020-11-08 10:48 wsw_seu 阅读(86) 评论(0) 推荐(0)

leetcode bitmap系列问题整理
摘要:1、 题目: 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。 示例 : 输入: 11输出: 3解释: 整数 11 的二进制表示为 00000000000000000000000000001011 示例 2: 输入: 11输出: 3解释: 整数 阅读全文

posted @ 2020-11-07 10:10 wsw_seu 阅读(295) 评论(0) 推荐(0)

334. Increasing Triplet Subsequence(也可以使用dp动态规划)
摘要:Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if 阅读全文

posted @ 2020-11-05 22:30 wsw_seu 阅读(90) 评论(0) 推荐(0)

454. 4Sum II
摘要:Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make pro 阅读全文

posted @ 2020-10-29 21:58 wsw_seu 阅读(78) 评论(0) 推荐(0)

218. The Skyline Problem
摘要:城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线(图B)。 每个建筑物的几何信息用三元组 [Li,Ri,Hi] 表示,其中 Li 和 Ri 分别是第 i 座建筑物左右边缘 阅读全文

posted @ 2020-10-29 21:57 wsw_seu 阅读(106) 评论(0) 推荐(0)

327. 区间和的个数
摘要:给定一个整数数组 nums,返回区间和在 [lower, upper] 之间的个数,包含 lower 和 upper。区间和 S(i, j) 表示在 nums 中,位置从 i 到 j 的元素之和,包含 i 和 j (i ≤ j)。 说明:最直观的算法复杂度是 O(n2) ,请在此基础上优化你的算法。 阅读全文

posted @ 2020-10-25 12:54 wsw_seu 阅读(120) 评论(0) 推荐(0)

37 Sudoku Solver
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 阅读全文

posted @ 2020-10-19 22:19 wsw_seu 阅读(67) 评论(0) 推荐(0)

36. Valid Sudoku
摘要:Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the dig 阅读全文

posted @ 2020-10-18 16:12 wsw_seu 阅读(107) 评论(0) 推荐(0)

差分数组(1109. 航班预订统计)
摘要:本文讲一个和前缀和思想非常类似的算法技巧「差分数组」,差分数组的主要适用场景是频繁对原始数组的某个区间的元素进行增减。 比如说,我给你输入一个数组 nums,然后又要求给区间 nums[2..6] 全部加 1,再给 nums[3..9] 全部减 3,再给 nums[0..4] 全部加 2,再给... 阅读全文

posted @ 2020-10-07 15:04 wsw_seu 阅读(167) 评论(0) 推荐(0)

前缀和
摘要:一、什么是前缀和 前缀和的思路是这样的,对于一个给定的数组 nums,我们额外开辟一个前缀和数组进行预处理: int n = nums.length; // 前缀和数组 int[] preSum = new int[n + 1]; preSum[0] = 0; for (int i = 0; i < 阅读全文

posted @ 2020-10-07 14:59 wsw_seu 阅读(339) 评论(0) 推荐(0)

连续子数组的和的绝对值的最大值、最小值(非绝对值的话直接dp动态规划)
摘要:前缀和的思路: sum[i] = num[0]+num[1]+......+num[i-1] sum[j] = num[0]+num[1]+......+num[j-1] 那么:num[i]+num[i+1]+....num[j] = sum[j+1] - sum[i] 所以求连续子数组绝对值的最大 阅读全文

posted @ 2020-09-20 13:35 wsw_seu 阅读(830) 评论(0) 推荐(0)

找出数组中出现次数超过一半的数字(众数)
摘要:题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。 法1:借助hashmap存储数组中每个数出现的次数,最后看是否有数字出现次数超 阅读全文

posted @ 2020-09-20 11:54 wsw_seu 阅读(325) 评论(0) 推荐(0)

消失的两个数字(1-N缺两个数)
摘要:给定一个数组,包含从 1 到 N 所有的整数,但其中缺了两个数字。你能在 O(N) 时间内只用 O(1) 的空间找到它们吗? 以任意顺序返回这两个数字均可。 示例 1: 输入: [1]输出: [2,3]示例 2: 输入: [2,3]输出: [1,4]提示: nums.length <= 30000 阅读全文

posted @ 2020-09-19 18:06 wsw_seu 阅读(697) 评论(0) 推荐(0)

47. Permutations II
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2, 阅读全文

posted @ 2020-09-19 16:52 wsw_seu 阅读(81) 评论(0) 推荐(0)

137. Single Number II
摘要:Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your al 阅读全文

posted @ 2020-09-19 16:05 wsw_seu 阅读(80) 评论(0) 推荐(0)

Single Number III
摘要:Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements tha 阅读全文

posted @ 2020-09-15 22:18 wsw_seu 阅读(70) 评论(0) 推荐(0)

导航