
算法及数据结构
wh_585
这个作者很懒,什么都没留下…
展开
-
leetcode 832 翻转图像
leetcode 832 翻转图像# 解法非常容易说一下这里遇到的问题# 在第一个for循环中想直接改变值来避免第二次循环, 结果未奏效class Solution: def flipAndInvertImage(self, A): if not A: return A for i in A: star...原创 2019-10-17 20:19:06 · 246 阅读 · 0 评论 -
leetcode 771 宝石与石头
leetcode 771 宝石与石头# helloworld 题目class Solution: def numJewelsInStones(self, J: str, S: str) -> int: j = set(J) ret = 0 for i in S: ret = ret + 1 if i in j...原创 2019-10-17 20:16:40 · 287 阅读 · 0 评论 -
leetcode 709 大写字符串转换为小写字符串
leetcode 709 大写字符串转换为小写字符串# ascll 小写字母 在97~122之间# 大写字母在65~97范围class Solution: def toLowerCase(self, str: str) -> str: ret = '' for ch in str: ch = chr(ord(ch) + 3...原创 2019-10-17 20:16:03 · 320 阅读 · 0 评论 -
leetcode 401 二进制手表
# leetcode 401 二进制手表提交一个反面教材# 主要思路在于通过一位灯亮的结果 递推2个灯亮的结果,并递推更多# 题目隐含条件 小时数不大于11 我也是通过观察返回结果发现的# 注:python3提交 不通过没有默认的set数据结构# 注:答案顺序不对 仍然算错 第一种解法无法通过class Solution: def readBinaryWatch(self, n...原创 2019-10-17 20:15:30 · 173 阅读 · 0 评论 -
leetcode 237 删除链表中非末尾结点
leetcode 237 删除链表中非末尾结点# 此做法并非实现结点的真正删除 只是将结点的属性改变# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solutio...原创 2019-10-17 20:15:02 · 154 阅读 · 0 评论 -
leetcode 121 买卖股票最佳时机
leetcode 121 买卖股票最佳时机# 通过将所有结果列出来 判断是否有符合交易的情况,如果有从中取出最大值# 此算法超时class Solution: def maxProfit(self, prices): if not prices: return 0 pre = 0 for index, i in ...原创 2019-10-17 20:14:26 · 276 阅读 · 0 评论 -
leetcode 70 爬楼梯
# leetcode 70 爬楼梯# 解法一 自顶向下的思考方法,自底向上的实现方式,采用递归便是自顶向下的实现方式# 举例: 假设最后剩一个台阶则有一种走法,就是剩n - 1个台阶的走法,剩两个台阶则走法就是n - 2个台阶的走法# 此解法存在冗余,比如变量过多class Solution: def climbStairs(self, n: int) -> int: ...原创 2019-10-17 20:13:43 · 213 阅读 · 0 评论 -
leetcode 53 最大自序和
leetcode 53 最大子序和# 解法一 穷举 i 表示取数组中元素的个数, j表示数组开始的位置# 此解法由于枚举效率过低 超出时间限制class Solution: def maxSubArray(self, nums) -> int: if not nums: return None lmax, larr = -...原创 2019-10-17 20:13:10 · 193 阅读 · 0 评论 -
leetcode 13 罗马数字转整数
# leetcode 13 罗马数字转整数# 可能我对题目的描述有误解,在此并不多说,分享解法# 判断后边的罗马数字和前边一个的关系, 如果前边小就对结果减去前边的值, 反之则加# 比如 'CD' D > Cclass Solution: def romanToInt(self, s: str) -> int: roma_map = { ...原创 2019-10-17 20:12:40 · 221 阅读 · 0 评论 -
leetcode 09 回文数
# leetcode 09 回文数# 解法一: 输入数小于0 直接false,否则将正整数反转 判断是否相等class Solution: def isPalindrome(self, x: int) -> bool: a = x if x < 0: return False res,...原创 2019-10-17 20:11:46 · 193 阅读 · 0 评论 -
leetcode 07 整数反转
#3 leetcode 07 整数反转# 解法一: 取数字最后一位 * 10 + 计算出来的数 判断大小关系class Solution: def reverse(self, x: int) -> int: b = 0 if x < 0: b, x = 1, abs(x) mod, res = 0, 0...原创 2019-10-17 20:11:14 · 200 阅读 · 0 评论 -
leetcode 03 无重复字符的最长子串
#3 leetcode 03 无重复字符的最长子串class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if s == '': m = 0 return m hash_map = {} len_arr = [...原创 2019-10-17 20:10:45 · 119 阅读 · 0 评论 -
leetcode 02 两数相加
leetcode 02 两数相加# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1: ...原创 2019-10-17 20:09:57 · 180 阅读 · 0 评论 -
C++选择排序
void SelectSort(int arr[], int length) //每次从待排记录中选择最小的记录,选择排序基于交换式排序{ if (arr == nullptr || length < 1) { return; } for (int i = 0; i < length; ++i) for (int j = i + 1; j void swap(int &lhs,原创 2017-09-26 09:26:58 · 273 阅读 · 0 评论 -
C++ 一行代码求两个数的最大公约数
#include //m和n的最大公约数等于n和m%n的最大公约数int gcd(int m, int n){return n == 0 ? m : gcd(n,m % n);}int main(){int m = 0, n = 0;std::cout while (std::cin >> m >> n){std::cout原创 2017-09-11 15:51:47 · 3689 阅读 · 1 评论 -
C++判断字符串是否互为变形词
#include #include #include bool isDeformation(const std::string& str1, const std::string& str2);int main(){ std::string str1, str2; std::cin >> str1 >> str2; std::cout << std::boolalpha <<原创 2017-10-07 13:13:14 · 376 阅读 · 0 评论 -
C++删除单链表值为K的结点空间复杂度O(1)
#include struct Node { int value; Node *next; Node(int data) : value(data), next(nullptr) {};};void print(Node * head);Node * delKValueNode(Node * head, int k);int main(){ Node n1(1); No原创 2017-08-11 14:40:18 · 658 阅读 · 0 评论 -
C++字符串数字子串求和
#include #include int SumStr(const std::string& str);int main(){ std::string str; std::cin >> str; std::cout << SumStr(str) << std::endl; system("pause"); return 0;}int SumStr(const st原创 2017-10-08 13:07:06 · 2433 阅读 · 0 评论 -
C++删除字符串中出现的连续的k个0
#include #include std::string& remKZero(std::string& str, const int& k);int main(){ std::string str; std::cin >> str; int k = 0; std::cin >> k; for (auto c : remKZero(str, k)) { std::co原创 2017-10-09 11:59:18 · 648 阅读 · 2 评论 -
C++判断两个字符串是否互为旋转词
#include #include #include bool isRotation(const std::string& lhs, std::string& rhs);int main(){ std::string lhs, rhs; std::cin >> lhs >> rhs; std::cout << std::boolalpha << isRotation(lhs,原创 2017-10-09 13:29:45 · 1051 阅读 · 0 评论 -
C++将整数字符串转化为整数值
#include #include bool isValid(const std::string& str);int convert(const std::string& str);int main(){ std::string str; std::cin >> str; if (isValid(str)) std::cout << convert(str) << std原创 2017-10-09 16:59:39 · 4078 阅读 · 0 评论 -
八大排序算法总结Python实现
排序算法直接插入排序 直接插入排序是一种简单的排序算法,基本操作是将一个记录插入到一个有序结果集中,并且在插入数据过程中将前面大于或者小于(取决于升序或者降序)当前要插入的数据向后移动。def InsertSort(array): j = 0 length = len(array) for i in range(1, length): ...原创 2018-05-31 13:44:30 · 2004 阅读 · 0 评论 -
C++ 生牛递归问题
#include int cowSum(int n);int cowSumPro(int n);int main(){for (int i = 0; i {std::cout std::cout std::cout }system("pause");return 0;}int cowSum(int n){原创 2017-08-30 11:31:48 · 968 阅读 · 0 评论 -
C++将单链表的每K个结点之间逆序改进版空间复杂度O(1)
#include struct Node {int value;Node *next;Node(int data) : value(data), next(nullptr) {}};void print(Node *head);Node * reverse(Node * start, Node * end, Node * left, Node * rig原创 2017-08-10 15:49:17 · 533 阅读 · 0 评论 -
C++ 删除单链表中值重复的结点_类似选择排序的解法
#include struct Node {int value;Node * next;Node(int data) : value(data), next(nullptr) {};};void print(Node *head);void delRepeatNode(Node *head);int main(){Node n原创 2017-08-11 11:24:00 · 952 阅读 · 0 评论 -
C++使用两个栈实现一个可以获取栈中最大值和最小值的栈
#include #include struct stack_pro {std::stack stk_data;std::stack stk_min;};void push(stack_pro &stk, const int &num);void pop(stack_pro &stk, int & tag);void getmin(stack_pro原创 2017-08-09 12:06:50 · 3047 阅读 · 0 评论 -
C++ 递归操作逆序栈
#include #include int getAndRemoveLastElement(std::stack &stk);void reverse(std::stack &st);int main(){std::stack stk;int arr[5] = { 12, 15, 5, 9, 7 };for (int i = 0; i {原创 2017-08-09 12:14:03 · 533 阅读 · 0 评论 -
C++ 使用一个栈实现另一个栈的排序
#include #include void print(std::stack &stk);void sort(std::stack &stack);int main(){std::stack stk; //help 栈底存储数值比较大的, stk栈底存储比较小的int arr[8] = { 2, 5, 1, 15, 19, 11,原创 2017-08-09 12:01:12 · 510 阅读 · 0 评论 -
c++删除单链表指定值结点
#include #include struct Node {int value;Node *next;Node(int data) : value(data), next(nullptr) {};};void print(Node *head);Node * delKValueNode(Node *head, int k);Node * con原创 2017-08-11 13:42:13 · 3124 阅读 · 0 评论 -
C++随机数求圆周率
#include #include #include #include #include using namespace std;int main(){long long i = 0, d = 1000000000, c = 0;float x = 0, y = 0;srand((unsigned int)time(NULL));原创 2017-09-15 17:47:03 · 1638 阅读 · 2 评论 -
C++动态规划 求换取指定数额钱币的最少货币数 普通动态规划和空间压缩方法
#include const int max = 65535;int minCoins(int arr[], int aim, int length);int minCoinsPro(int arr[], int aim, int length);int main(){int arr[7] = { 5, 2, 5, 3, 4, 5, 7};int aim原创 2017-08-28 13:58:02 · 523 阅读 · 0 评论 -
C++冒泡排序递归&&非递归版本
void BubbleSort(int arr[], int length){if (arr == nullptr || length return;for (int i = 0; i {for (int j = 0; j {if (arr[j] > arr[j + 1]){swap(arr[j], arr[j + 1]);}}}}原创 2017-09-04 11:41:42 · 1203 阅读 · 0 评论 -
C++二叉树非递归遍历大合集1种前序和中序遍历以及两种后序遍历
#include #include struct BinTreeNode {int value;BinTreeNode * leftChild, *rightChild;BinTreeNode(int data) : value(data), leftChild(nullptr), rightChild(nullptr) {};};void pr原创 2017-08-22 17:10:44 · 312 阅读 · 0 评论 -
C++ 换钱最少货币数 动态规划 + 空间优化 o(n)
#include const int jud = 65535; //32位 int型最大整数 用来进行数组的赋值int minCoins(const int arr[], const int &length, const int &aim);int minCoins_pro(const int arr[], const int &length, const int &aim);原创 2017-08-21 13:40:03 · 821 阅读 · 0 评论 -
C++换钱的最少货币数 动态规划
#include const int jud = 65535; //32位 int型最大整数 用来进行数组的赋值int minCoins(const int arr[], const int &length, const int &aim);int main(){int arr[10];int aim = 0, n = 0, num = 0;std::c原创 2017-08-21 12:55:47 · 998 阅读 · 0 评论 -
C++求矩阵最小路径和进阶方法空间复杂度O(min {row, col})
#include int minPathSum_pro(const int(*pArr) [10], const int &row, const int &col);int main(){int row = 0, col = 0, num = 0;int arr[10][10];std::cin >> row >> col;for (int i原创 2017-08-20 11:54:20 · 865 阅读 · 0 评论 -
C++单链表选择排序
#include struct Node {int value;Node *next;Node(int data) : value(data), next(nullptr) {};};Node * ListSelectSort(Node * head);Node * getSmallestNode(Node * head);void prin原创 2017-08-13 12:34:08 · 1037 阅读 · 0 评论 -
C++ 求矩阵最短路问题最简单最暴力的dp解法
#include //输入一个矩阵数组,求出从arr[0][0]到arr[row - 1][col - 1]的最短路径int minPathSum(int (*arr)[10], const int &row, const int &col); //数组并不完全是指针,传参数需要注意int main(){int arr[10][10], num, row =原创 2017-08-19 17:46:17 · 1016 阅读 · 0 评论