Algorithm
文章平均质量分 68
吴家行hang
这个作者很懒,什么都没留下…
展开
-
LeetCode-561. Array Partition I(Quick Sort快速排序)
561. Array Partition I题目Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from...原创 2019-03-06 08:46:54 · 192 阅读 · 0 评论 -
万能头文件(C++)
#include<bits/stdc++.h>using namespace std;原创 2019-03-01 20:38:44 · 12173 阅读 · 0 评论 -
LeetCode-205. Isomorphic Strings
205. Isomorphic Strings题目Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must ...原创 2019-03-01 20:38:34 · 152 阅读 · 0 评论 -
判断一个数字是否是2的幂次方
这段代码可以说是人生中比较自豪的一段代码了,直接想到了位运算,因为2的幂次方化为二进制一定只有一个1bool isPowerOfTwo(int n) { if(n==0)return false; int bits=0; int k =n; while(k!=0){ if(k&1==1)bits++; if(bits>...原创 2019-03-01 20:38:25 · 652 阅读 · 0 评论 -
LeetCode-141. Linked List Cycle
141. Linked List Cycle题目Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the ...原创 2019-03-01 20:38:13 · 88 阅读 · 0 评论 -
LeetCode - 1017. Convert to Base -2
Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).The returned string must have no leading zeroes, unless the string is “0”.Example 1:...原创 2019-04-01 10:49:54 · 244 阅读 · 0 评论 -
求x的y次方对z取模(x^y)mod z:蒙格马利快速幂模算法
long Montgomery(long a,long b,long m){ long r=1; a %=m; while(b>1) { if((b&1)!=0) r = (r*a)%m; a = (a*a)%m; b/=2; } return (r*a)...原创 2019-04-10 23:29:01 · 2365 阅读 · 0 评论 -
关于c语言的scanf()和gets()输入字符串
scanf() 和 gets() 是有区别的:scanf() 读取字符串时以空格为分隔,遇到空格就认为当前字符串结束了,所以无法读取含有空格的字符串。gets() 认为空格也是字符串的一部分,只有遇到回车键时才认为字符串输入结束,所以,不管输入了多少个空格,只要不按下回车键,对 gets() 来说就是一个完整的字符串。换句话说,gets() 用来读取一整行字符串。...原创 2019-04-11 13:06:10 · 1909 阅读 · 0 评论 -
你爱西加加么?
你爱西加加么?cinint a;cin >> a;coutint a = 5;cout << a << endl; // endl : '\n'vectorvector<int> a;vector<int> b(5); //定义一维动态数组含有五个元素vector<vector<int> >...原创 2019-04-13 16:14:48 · 158 阅读 · 0 评论 -
LeetCode-169. Majority Element
169. Majority Element题目Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and t...原创 2019-03-01 20:38:59 · 152 阅读 · 0 评论 -
LeetCode-172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes题目Given an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3Output: 0Explanation: 3! = 6, no trailing zero.Example 2:Input: 5Output: 1Exp...原创 2019-03-01 20:39:05 · 154 阅读 · 0 评论 -
LeetCode-136. Single Number
136. Single Number题目Given a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you imp...原创 2019-03-01 20:39:10 · 105 阅读 · 0 评论 -
LeetCode-746. Min Cost Climbing Stairs(DP)
746. Min Cost Climbing Stairs题目On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find m...原创 2019-03-04 15:12:29 · 121 阅读 · 0 评论 -
LeetCode-1004. Max Consecutive Ones III【Sliding Window】
1004. Max Consecutive Ones III题目Given an array A of 0s and 1s, we may change up to K values from 0 to 1.Return the length of the longest (contiguous) subarray that contains only 1s.Example 1:Inpu...原创 2019-03-04 12:38:12 · 145 阅读 · 0 评论 -
LeedCode-53. Maximum Subarray
53. Maximum Subarray题目:Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,...原创 2019-03-01 20:40:17 · 93 阅读 · 0 评论 -
LeetCode-14. Longest Common Prefix
14. Longest Common Prefix题目:Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.Example 1:Input: [“flower...原创 2019-03-01 20:40:07 · 101 阅读 · 0 评论 -
ACM中快速将整形转化成字符串
//首先定义一个确定大小的字符数组char str[100];int num = 7;sprintf(str, &amp;amp;quot;%d&amp;amp;quot;, num);原创 2019-03-01 20:39:51 · 262 阅读 · 0 评论 -
LeetCode-108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree题目Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is ...原创 2019-03-01 20:39:40 · 81 阅读 · 0 评论 -
LeetCode-122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II题目:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as ...原创 2019-03-01 20:39:34 · 99 阅读 · 0 评论 -
LeetCode-168. Excel Sheet Column Title
168. Excel Sheet Column Title题目Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -&amp;amp;amp;gt; A2 -&amp;amp;amp;gt; B3 -&amp;amp;amp;gt; C...26 -&amp;amp;am原创 2019-03-01 20:39:28 · 109 阅读 · 0 评论 -
分解质因数算法
题目:将一个正整数分解质因数。例如:输入90,打印出90=2 * 3 * 3 * 5。分析:从1到N先找出最小的质因数,如果等于本身,那么说明只有一个质因数,如果不是,那么将该质因数打印出来,并将N/该质因数作为新的N值进行运算。设计步骤:1、如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。2、如果n!=k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整...原创 2019-04-17 09:24:57 · 3167 阅读 · 1 评论