基本数据结构与算法
pacmoc
用代码托起我的不切实际
展开
-
gcd算法
原理: 两个整数的最大公约数等于其中较小的数和两数的差的最大公约数。即一步步的降低两个数的值,直到其中一个变成零,这时所剩下的还没有变成零的数就是两个数的最大公约数。伪代码:function gcd(a,b) while b != 0 t = b b = a mod b a = t return a...原创 2018-07-14 10:29:39 · 1192 阅读 · 0 评论 -
斐波拉契数相关
求斐波拉契数斐波拉契数为,Fib(N) = Fib(N-1)+Fib(N-2) F(0)=F(1)=1 用Java编写能求Fib(N)的程序 输入为N,须输出Fib(N) 如输入 3 输出: 3import java.util.Scanner;public class Main { public static void main(String[] args) { ...原创 2018-07-12 21:06:04 · 403 阅读 · 0 评论 -
求素数的算法
素数:大于1的自然数中,只能被1和它本身整除的数。#include<stdio.h>#include<time.h>#include<math.h>int IsPrim原创 2018-07-15 14:54:32 · 9793 阅读 · 0 评论 -
求逆序数
#求逆序数n = eval(input("Enter a number: "))m = ns = 0while n != 0: s = s * 10 + n % 10 n //= 10print("reverse({:d}) = {:d}".format(m,s))#字符串的format()方法中参数输出的位置和格式等由其前面字符串中的{}位置和格式(d表示十进制整数)...原创 2018-09-23 10:58:41 · 609 阅读 · 0 评论 -
分解质因数
#分解质因数n = eval(input("Enter a number: "))print(n, "=", end = " ")i = 2while n != 1: while n % i == 0: n //= i if n == 1: print("{:d}".format(i)) else: ...原创 2018-09-23 11:06:46 · 214 阅读 · 0 评论 -
常用排序算法
选择排序def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1, len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i...原创 2018-11-24 17:36:04 · 114 阅读 · 0 评论 -
4 树
1 树的定义树是递归定义的,所以和树相关的很多操作,用递归更容易实现。2 二叉树的遍历2.1 递归实现https://pintia.cn/problem-sets/15/problems/7322.1.1 前序遍历void PreorderTraversal( BinTree BT ){ if (BT) { printf(" %c&am原创 2018-11-24 17:37:09 · 151 阅读 · 0 评论 -
用位运算进行加减法 leetcode371
题目描述:leetcode371Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example 1:Input: a = 1, b = 2Output: 3Example 2:Input: a = -2, b = 3Output: 1分析:...原创 2019-01-03 16:19:28 · 222 阅读 · 0 评论 -
面试常见排序问题
文章目录3.1 排序3.1.0 堆排序3.1.1 归并排序 √3.1.2 快速排序 √3.1.3 插入排序 √3.1.4 选择排序 √手撸快排居然失败,可怕。3.1 排序leetcode 9123.1.0 堆排序class Solution {public: vector<int> sortArray(vector<int>& nums) { ...原创 2019-09-17 15:03:05 · 854 阅读 · 0 评论