随笔分类 -  算法

2021/3/31日,正式趁着考研仔细学习算法!
摘要:1.两数之和 1.1 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 你可以按任意顺序返回答案。 1.2 示 阅读全文
posted @ 2022-08-19 08:52 nanfengnan 阅读(24) 评论(0) 推荐(0) 编辑
摘要:#### 在一个行和列都是依次递增的矩阵(这里是二维数组)中,如何设计一个时间复杂度为O(n)的算法,判断矩阵中是否存在元素x? int find_x(vector<vector<int>> &m,int x){ // 列数 int c = m[0].size()-1; // 行数 int r = 阅读全文
posted @ 2022-07-24 18:42 nanfengnan 阅读(28) 评论(0) 推荐(0) 编辑
摘要:思想 在B串中查找A串,把B串中A[0]的位置都放到数组里面,此次从数组为之查找即可,成功返回true,否则返回false 代码 Java代码 import java.util.*; public class F{ public static void main(String[] args) { S 阅读全文
posted @ 2022-06-10 07:05 nanfengnan 阅读(36) 评论(0) 推荐(0) 编辑
摘要://求解最小公倍数 输入m,n (m>n) #解法一,循环求解 算法思想: 设置一个循环变量i初始化为m 1.如果m==n,返回最小公倍数为m. 2.否则,当i%m==0 && i%n==0成立时,找到最小公倍数i. 3.否则i++,继续执行步骤2. int gcd(int m,int n) { i 阅读全文
posted @ 2022-01-08 08:12 nanfengnan 阅读(136) 评论(0) 推荐(0) 编辑
摘要:#include<iostream> using namespace std; #include<string> const int MAX = 3000; int a[MAX]; int fac(int n) { memset(a, 0, sizeof(a));//初始化0;下面的a[j] = a 阅读全文
posted @ 2022-01-06 16:55 nanfengnan 阅读(266) 评论(0) 推荐(0) 编辑
摘要:#include <unordered_set> #include <iostream> #include <algorithm> using namespace std; typedef int ElemType; typedef struct { ElemType* data; int leng 阅读全文
posted @ 2021-08-19 23:45 nanfengnan 阅读(60) 评论(0) 推荐(0) 编辑
摘要:#include <iostream> #include <unordered_set> #include <algorithm> using namespace std; typedef int ElemType; typedef struct LinkNode { ElemType data; 阅读全文
posted @ 2021-08-18 00:01 nanfengnan 阅读(41) 评论(0) 推荐(0) 编辑
摘要:1.使用算法头文件中的reserve #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s = "hello"; reverse(s.begin(), 阅读全文
posted @ 2021-07-08 00:24 nanfengnan 阅读(227) 评论(0) 推荐(0) 编辑
摘要:1.采用C++的vector定义二维数 int main() { //matrix.size(); 二维数组有多少行 //matrix[i].size(); 每行有多少列 vector< vector<int> > matrix(3); //vector创建二维数组,多少行 for (int i = 阅读全文
posted @ 2021-07-08 00:07 nanfengnan 阅读(745) 评论(0) 推荐(0) 编辑
摘要:#include <stdio.h> //汉诺塔递归实现 void move(int n,char a,char b,char c) { if (n == 1) printf("\t%c->%c \n",a,c); //一个盘子直接从a挪到c else { move(n-1,a,c,b); //把n 阅读全文
posted @ 2021-04-27 00:55 nanfengnan 阅读(49) 评论(0) 推荐(0) 编辑
摘要:#include <stdio.h> //兔子繁殖问题 /* * 问题描述: * 有一对小兔子,从出生起第3个月起每个月都生一对小兔子. * 小兔子长到三个月后每个月又生一对兔子,按此规律,设第一个月有一对刚出生的兔子, * 问第n个月后有多少对兔子. * * 1.分析 * 第一个月-第10个月: 阅读全文
posted @ 2021-03-31 11:48 nanfengnan 阅读(833) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示