自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

夏洛克福尔摩斯

读万卷书,行万里路

  • 博客(174)
  • 收藏
  • 关注

原创 C语言中qsort()库函数的排序应用

C语言中不像C++一样有sort(arr.begin(),arr.end(),cmp)排序接口,但是也提供类似的库函数。1. 首先要包含头文件<stdlib.h>2. 对于一维整型数组函数指针定义:int cmp_int(const void *a, const void *b) { int *_a = (int *)a; int *_b = (int *)b; return *_a - *_b;}main函数调用:int main(){

2020-06-05 15:01:34 585 1

原创 整数对查找

题目描述请设计一个高效算法,找出数组中两数之和为指定值的所有整数对。给定一个int数组A和数组大小n以及需查找的和sum,请返回和为sum的整数对的个数。保证数组大小小于等于3000。测试样例:[1,2,3,4,5],5,6返回:2//用红黑树最大的陷阱就是可能sum可以分为两个相同的数之和class FindPair {public: int count...

2019-09-04 23:03:53 358

原创 最长递增子序列

这类算法与 堆箱子 和叠罗汉都是同一类型题目。解法一致叠罗汉I:题目描述叠罗汉是一个著名的游戏,游戏中一个人要站在另一个人的肩膀上。同时我们应该让下面的人比上面的人更高一点。已知参加游戏的每个人的身高,请编写代码计算通过选择参与游戏的人,我们最多能叠多少个人。注意这里的人都是先后到的,意味着参加游戏的人的先后顺序与原序列中的顺序应该一致。给定一个int数组men,代表依次来的每个人...

2019-09-01 11:40:20 220

原创 元素查找(移动有序数组)

题目描述有一个排过序的数组,包含n个整数,但是这个数组向左进行了一定长度的移位,例如,原数组为[1,2,3,4,5,6],向左移位5个位置即变成了[6,1,2,3,4,5],现在对于移位后的数组,需要查找某个元素的位置。请设计一个复杂度为log级别的算法完成这个任务。给定一个int数组A,为移位后的数组,同时给定数组大小n和需要查找的元素的值x,请返回x的位置(位置从零开始)。保证数组中元...

2019-09-01 10:45:37 357

原创 堆箱子

题目描述有一堆箱子,每个箱子宽为wi,长为di,高为hi,现在需要将箱子都堆起来,而且为了使堆起来的箱子不倒,上面的箱子的宽度和长度必须小于下面的箱子。请实现一个方法,求出能堆出的最高的高度,这里的高度即堆起来的所有箱子的高度之和。给定三个int数组w,l,h,分别表示每个箱子宽、长和高,同时给定箱子的数目n。请返回能堆成的最高的高度。保证n小于等于500。测试样例:[1,1,1...

2019-08-31 22:04:23 499

原创 二叉树的中序遍历(非递归)

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *///非...

2019-08-31 16:46:42 159

转载 课程表(拓扑排序)

现在你总共有 n 门课需要选,记为 0 到 n-1。在选修某些课程之前需要一些先修课程。 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及它们的先决条件,判断是否可能完成所有课程的学习?示例 1:输入: 2, [[1,0]]输出: true解释: 总共有 2 门课程。学习课程 1 之前,你需要完成课程 0。所以这是可能的。...

2019-08-26 21:09:35 767

原创 洗牌程序(面试常问)

Solution 1(时间复杂度约为O(n^2),空间复杂度为O(n))#include <iostream>#include<cstdlib>#include<vector>#define N 54using namespace std;void func(vector<int>&num){ int hash[N+...

2019-08-26 20:06:01 420

转载 内存拷贝函数memcpy

memcpy(拷贝内存内容) 定义函数:void * memcpy( void * dest, const void *src, size_t n );memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束'/0'而结束。memcpy()函数可以拷贝任意类型的数据。memc...

2019-08-25 20:31:21 887

原创 爬楼梯2

题目描述在你面前有一个n阶的楼梯(n>=100且n<500),你一步只能上1阶或3阶。请问计算出你可以采用多少种不同的方式爬完这个楼梯(到最后一层为爬完)。输入描述:一个正整数,表示这个楼梯一共有多少阶输出描述:一个正整数,表示有多少种不同的方式爬完这个楼梯示例1输入100输出24382819596721629#include<...

2019-08-22 20:15:36 361

转载 由rand7()均匀生成rand10()

转载至:https://blog.csdn.net/BIT_666/article/details/84936793概述已知随机数生成函数rand7()可以生成整数1-7之间的均匀分布,如何使用rand7()构造rand10(),使rand10()可以生成整数1-10的均匀分布分析要保证rand10()生成的随机数是1-10的均匀分布,可以先产生1-10*n的均匀分布,假设x是1-1...

2019-08-18 11:10:20 235

转载 三维形体的表面积

在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体。每个值 v = grid[i][j] 表示 v 个正方体叠放在对应单元格 (i, j) 上。请你返回最终形体的表面积。示例 1:输入:[[2]]输出:10示例 2:输入:[[1,2],[3,4]]输出:34示例 3:输入:[[1,0],[0,2]]输出:16示例 4:输入:[[1,...

2019-08-16 07:55:19 303

原创 迷宫bfs应用(华为OJ题目)

'S'是开始位置,'#'表示墙,'.'是通路,'E'是终点,求最短路径长度#include <iostream>#include<vector>#include<string>#include<utility>#include<queue>using namespace std;int offset[4][2]={...

2019-07-24 19:59:01 294

原创 等价多米诺骨牌对的数量

给你一个由一些多米诺骨牌组成的列表dominoes。如果其中某一张多米诺骨牌可以通过旋转 0度或 180 度得到另一张多米诺骨牌,我们就认为这两张牌是等价的。形式上,dominoes[i] = [a, b]和dominoes[j] = [c, d]等价的前提是a==c且b==d,或是a==d 且b==c。在0 <= i < j < dominoe...

2019-07-21 15:58:21 228

原创 N皇后问题(C++)

n皇后问题研究的是如何将 n个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。(即任意两个皇后都不能处于同一行、同一列或同一斜线上).上图为 8 皇后问题的一种解法。给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。示例:输入: 4输出: [...

2019-07-13 11:26:28 3645

原创 洪水问题(bfs应用)

题目描述在一个nxm矩阵形状的城市里爆发了洪水,洪水从(0,0)的格子流到这个城市,在这个矩阵中有的格子有一些建筑,洪水只能在没有建筑的格子流动。请返回洪水流到(n - 1,m - 1)的最早时间(洪水只能从一个格子流到其相邻的格子且洪水单位时间能从一个格子流到相邻格子)。给定一个矩阵map表示城市,其中map[i][j]表示坐标为(i,j)的格子,值为1代表该格子有建筑,0代表没有建筑。...

2019-06-23 20:45:26 301

原创 rotate-list(循环移动链表)

题目描述Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.Solutio...

2019-06-20 20:51:11 228

原创 LeetCode---unique-paths-ii(路径个数)

题目描述A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach ...

2019-06-19 21:23:05 260

转载 LeetCode--set-matrix-zeroes(很巧妙的解法)

题目描述Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(...

2019-06-15 15:45:06 173

原创 LeetCode--subsets(数组的所有可能子集合)

题目描述Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.For example,...

2019-06-14 21:12:57 405

原创 minimum-window-substring(最小区间子串)

题目描述Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S="ADOBECODEBANC"T="ABC"Minimum window is"BANC".No...

2019-06-12 19:45:40 677

转载 内存对齐问题的一些总结

要了解为什么要内存对齐,首先我们要了解什么是内存对齐什么是内存对齐关于什么是内存对齐,我们先来看几个例子typedef struct { int a; double b; short c;}A;typedef struct { int a; short b; double c;}B;分别对他们求大小,sizeof(A),s...

2019-06-11 15:44:43 459

原创 LeetCode--combinations(求一个数组指定个数所有组合)

题目描述Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4...

2019-06-11 15:18:35 985

原创 LeetCode--word-search(矩阵方格中单词的查找bfs应用)

Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically n...

2019-06-11 14:56:41 275

原创 LeetCode--remove-duplicates-from-sorted-list-ii(链表去重)

题目描述Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1-&...

2019-06-10 20:29:14 194

转载 LeetCode-maximal-rectangle(最大矩阵面积)

题目描述Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. ...

2019-06-09 20:04:59 1279

原创 partition-list(链表根据值分割两部分)

题目描述Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in eac...

2019-06-09 09:16:43 242

转载 LeetCode--subsets-ii(所有可能的子集合)

题目描述Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain dup...

2019-06-04 16:42:16 240

原创 LeetCode---reverse-linked-list-ii(一段区间内的链表逆序)

题目描述Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.No...

2019-06-03 20:10:53 409

原创 LeetCode---restore-ip-addresses(IP地址转换)

题目描述Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order d...

2019-06-03 18:53:03 343

原创 interleaving-string(交叉字符串)

题目描述Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return fals...

2019-06-03 09:21:58 205

转载 distinct-subsequences(字符串中相同子序列个数)

题目描述Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be...

2019-05-28 21:52:57 944

原创 单链表转换为平衡搜索树

题目描述Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for singly-linked list. * struct ListNode { * int val; * ...

2019-05-28 21:02:52 154

原创 字符串排列(全排列)

题目描述编写一个方法,确定某字符串的所有排列组合。给定一个stringA和一个intn,代表字符串和其长度,请返回所有该字符串字符的排列,保证字符串长度小于等于11且字符串中字符均为大写英文字符,排列中的字符串按字典序从大到小排序。(不合并重复字符串)测试样例:"ABC"返回:["CBA","CAB","BCA","BAC","ACB","ABC"]Solution...

2019-05-24 21:32:46 1358

原创 字节跳动-笔试(聪明的编辑)

我叫王大锤,是一家出版社的编辑。我负责校对投稿来的英文稿件,这份工作非常烦人,因为每天都要去修正无数的拼写错误。但是,优秀的人总能在平凡的工作中发现真理。我发现一个发现拼写错误的捷径:1. 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello2. 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 hell...

2019-05-20 14:40:45 947

转载 两个字符串的最长公共子串

问题:有两个字符串str和str2,求出两个字符串中最长公共子串长度。比如:str=acbcbcef,str2=abcbced,则str和str2的最长公共子串为bcbce,最长公共子串长度为5。算法思路:1、把两个字符串分别以行和列组成一个二维矩阵。2、比较二维矩阵中每个点对应行列字符中否相等,相等的话值设置为1,否则设置为0。3、通过查找出值为1的最长对角线就能找到最长公共...

2019-05-18 17:00:17 944

转载 计算从1到n包含1的个数

薯队长在平时工作中需要经常跟数字打交道,某一天薯队长收到了一个满是数字的表格,薯队长注意到这些数字里边很多数字都包含1,比如101里边包含两个1,616里包含一个1。请你设计一个程序帮薯队长计算任意一个正整数n(0<n<=2147483647),从1到n(包括n)的所有整数数字里含有多少个1。#include<iostream>using namespace...

2019-05-17 11:50:11 1598

原创 二叉树由中序遍历序列加层序遍历序列构建二叉树

思路:由二叉树的层序遍历的第一个元素seq[0],我们可以知道它是根节点,所以我们在中序遍历中vector<int>inorder查找层序遍历序列的第一个位置 ,可以将中序遍历序列分成左右子树。然后我们构建左右子树的层序遍历序列。如下代vector<int>left;vector<int>right;左右子树的层次遍历。#include<ios...

2019-05-17 11:09:27 1210

原创 机器人走方格

题目描述有一个XxY的网格,一个机器人只能走格点且只能向右或向下走,要从左上角走到右下角。请设计一个算法,计算机器人有多少种走法。给定两个正整数intx,inty,请返回机器人的走法数目。保证x+y小于等于12。测试样例:2,2返回:2class Robot {public: int countWays(int x, int y) { i...

2019-05-15 21:01:51 164

原创 LeetCode(path-sum)

题目描述Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and...

2019-05-12 19:33:21 149

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除