[LeetCode]sum合集
LeetCode很喜欢sum,里面sum题一堆。
1.Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
1 /**
2 * Note: The returned array must be malloced, assume caller calls free().
3 */
4 int* twoSum(int* nums, int numsSize, int target) {
5 int* ret = (int*)malloc(2*sizeof(int));
6 int j = 1;
7 for(int i = 0;i < numsSize - 1;){
8 if(nums[i] + nums[j] == target){
9 ret[0] = i;
10 ret[1] = j;
11 break;
12 }
13 j++;
14 if(j == numsSize){
15 i++;
16 j = i + 1;
17 }
18 }
19 return ret;
20 }
用hash_map使其复杂度减小到O(n)
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ret;
hash_map<int, int>arr;
for (int i = 0; i < nums.size(); i++){
int rem = target - nums[i];
if (arr.find(rem) != arr.end()){
ret.push_back(i);
ret.push_back(arr.at(rem));
return ret;
}
arr[nums[i]] = i;
}
return ret;
}
2.3Sum
题目:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
找3个数和为0的所有不同组合,数组元素只能用1次
1.不知道具体有多少种可能,用链表来存储,再转成数组
2.暴力搜索,可以先排序,加快速度
3.注意跳过重复情况
1 /*************************************************
2 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
3 Note: The solution set must not contain duplicate triplets.
4 For example, given array S = [-1, 0, 1, 2, -1, -4],
5 A solution set is:
6 [
7 [-1, 0, 1],
8 [-1, -1, 2]
9 ]
10 *************************************************/
11 #include<stdio.h>
12 #include<stdlib.h>
13
14 int cmp(const void *a , const void *b){
15 return *(int *)a > *(int *)b ? 1 : -1;
16 }
17
18 struct linklist{
19 int *val;
20 struct linklist *next;
21 };
22
23 /**
24 * Return an array of arrays of size *returnSize.
25 * Note: The returned array must be malloced, assume caller calls free().
26 */
27 int** threeSum(int* nums, int numsSize, int* returnSize) {
28 struct linklist *ts = NULL;
29 int i,n = 0,head,tail,sum = 0;
30 //排序加快搜索
31 qsort(nums,numsSize,sizeof(int),cmp);
32 //i、head、tail三个指针,暴力搜索所有可能
33 for(i = 0;i < numsSize;i++){
34 head = i + 1;
35 tail = numsSize - 1;
36 while (head < tail) {
37 sum = nums[i] + nums[head] + nums[tail];
38 if(sum == 0){//找到一种可能
39 n++;
40 struct linklist *p = (struct linklist *)malloc(sizeof(struct linklist));
41 p->val = (int *)malloc(3*sizeof(int));
42 p->val[0] = nums[i];
43 p->val[1] = nums[head];
44 p->val[2] = nums[tail];
45 p->next = ts;
46 ts = p;
47 head++;
48 tail--;
49 while(nums[head] == nums[head - 1])head++;//跳过重复的情况
50 while(nums[tail] == nums[tail + 1])tail--;
51 }else if(sum > 0){//过大
52 while(nums[tail] == nums[tail - 1])tail--;
53 tail--;
54 }else{//过小
55 while(nums[head] == nums[head + 1])head++;
56 head++;
57 }
58 }
59 while(nums[i] == nums[i + 1])i++;//跳过重复的情况
60 }
61
62 struct linklist *p = NULL;
63 int **as = (int **)malloc(n*sizeof(int *));
64 for(i = 0;i < n;i++)
65 as[i] = (int *)malloc(3*sizeof(int));
66 i = 0;
67 while(ts != NULL){
68 as[i][0] = ts->val[0];
69 as[i][1] = ts->val[1];
70 as[i++][2] = ts->val[2];
71 p = ts;
72 ts = ts->next;
73 p->next = NULL;
74 free(p->val);
75 free(p);
76 }
77
78 *returnSize = n;
79 return as;
80 }
81
82 int main(){
83 int a[] = {-1,0,1,2,-1,-4};
84 int n = 0;
85 int **as = threeSum(a,sizeof(a)/sizeof(int),&n);
86 printf("%d\n",n);
87 for(int i = 0;i < n;i++){
88 for(int j = 0;j < 3;j++)
89 printf("%d ",as[i][j]);
90 printf("\n");
91 }
92
93 for(int i = 0;i < n;i++){
94 free(as[i]);
95 }
96 }
3.3Sum Closest
题目:
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
找3个数的和最接近目标数的组合。
1 /***************************************************************************************************
2 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
3 For example, given array S = {-1 2 1 -4}, and target = 1.
4 The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
5 **************************************************************************************************/
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 int cmp(const void *a , const void *b){
10 return *(int *)a > *(int *)b ? 1 : -1;
11 }
12
13 int threeSumClosest(int* nums, int numsSize, int target) {
14 int bestdif = 2147483647,bestsum;
15 int dif,sum,i,head,tail;
16
17 qsort(nums,numsSize,sizeof(int),cmp);
18
19 for(i = 0;i < numsSize;i++){
20 head = i + 1;
21 tail = numsSize - 1;
22 while(head < tail){
23 sum = nums[i] + nums[head] + nums[tail];
24 dif = sum - target;//求差值
25 if(dif == 0)return sum;
26 else if(dif > 0){
27 tail--;
28 while(nums[tail] == nums[tail + 1])tail--;
29 }else{
30 head++;
31 while(nums[head] == nums[head - 1])head++;
32 }
33 dif = abs(dif);
34 if(dif < bestdif){//比较是否比已知的结果好
35 bestdif = dif;
36 bestsum = sum;
37 }
38 }
39 while(nums[i] == nums[i + 1])i++;
40 }
41 return bestsum;
42 }
43
44 void main(){
45 int nums[] = {-1,2,1,-4,0};
46 int target = 1;
47 int sum = threeSumClosest(nums,sizeof(nums)/sizeof(int),target);
48 printf("%d\n",sum);
49 }
4.4Sum
题目:
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
找4个数和为目标数的所有不同组合,数组元素只能用1次
接着暴力搜索。
/***************************************************************************************************
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
***************************************************************************************************/
#include<stdio.h> int cmp(const void *a , const void *b){
return *(int *)a > *(int *)b ? 1 : -1;
} struct linklist{
int *val;
struct linklist *next;
}; /**
* Return an array of arrays of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int** fourSum(int* nums, int numsSize, int target, int* returnSize) {
struct linklist *fs = NULL;
int i,j,head,tail,dif = 0,length = 0; qsort(nums,numsSize,sizeof(int),cmp);//排序 //i,j,head,tail四个游标,暴力搜索
for(i = 0;i < numsSize;i++){
for(j = i + 1;j < numsSize;j++){
head = j + 1;//head从j+1开始
tail = numsSize - 1;
while(head < tail){
dif = nums[i] + nums[j] + nums[head] + nums[tail] - target;
if(dif == 0){
length++;
struct linklist *p = (struct linklist *)malloc(sizeof(struct linklist));
p->val = (int *)malloc(4*sizeof(int));
p->val[0] = nums[i];
p->val[1] = nums[j];
p->val[2] = nums[head++];
p->val[3] = nums[tail--];
p->next = fs;
fs = p;
while(nums[head] == nums[head - 1])head++;
while(nums[tail] == nums[tail + 1])tail--;
}else if(dif > 0){//过大
while(nums[tail] == nums[tail - 1])tail--;
tail--;
}else{//过小
while(nums[head] == nums[head + 1])head++;
head++;
}
}
while(nums[j] == nums[j + 1])j++;
}
while(nums[i] == nums[i + 1])i++;
} int **ret = (int **)malloc(length*sizeof(int *));
struct linklist *p = NULL;
i = 0;
while(fs != NULL){
p = fs;
fs = fs->next;
p->next = NULL;
ret[i] = (int *)malloc(4*sizeof(int));
ret[i][0] = p->val[0];
ret[i][1] = p->val[1];
ret[i][2] = p->val[2];
ret[i++][3] = p->val[3];
free(p->val);
free(p);
} *returnSize = length;
return ret;
} void main(){
int a[] = {-1,0,1,2,-1,-4,0};
int n = 0,target = 0;
int **as = fourSum(a,sizeof(a)/sizeof(int),target,&n);
printf("%d\n",n);
for(int i = 0;i < n;i++){
for(int j = 0;j < 4;j++)
printf("%d ",as[i][j]);
printf("\n");
} for(int i = 0;i < n;i++){
free(as[i]);
}
}
5.Combination Sum
题目:
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7]
and target 7
,
A solution set is:
[
[7],
[2, 2, 3]
]
从数组中找和是目标数的所有组合,数组元素可以使用多次
用递归的思想求解:首先排序,然后搜索数组所有元素,目标数减去当前元素作为新的目标数,再递归搜索新的目标数,知道目标数为0.
1.多种不同组合中可能有部分元素重合,所以,搜索到一种结果后,还要继续搜索。
2.找到符合情况的组合后才能给数组分配大小,所以要记录递归的层数,返回时,每层给数组附上各自的值。
3.注意链表头特殊处理。
/*******************************************************************************
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
*******************************************************************************/
#include<stdio.h>
#include<stdbool.h> struct linklist{
int *val;//存储符合要求的数的组合
int size;//数组大小
struct linklist *next;
}; int cmp(const void *a , const void *b){
return *(int *)a > *(int *)b ? 1 : -1;
} struct linklist *getTargetArray(int *candidates,int candidatesSize,int surplus,int start,int length){
struct linklist *head = NULL;
struct linklist *tail = head;
int subStrSize;
//递增排序,目标数比当前值还小,后面的就更加不可能了
for(int i = start;i < candidatesSize && surplus >= candidates[i];i++){
if(surplus - candidates[i] == 0){//找到一种情况
struct linklist *p = (struct linklist *)malloc(sizeof(struct linklist));
p->size = length + 1;
p->val = (int *)malloc(p->size*sizeof(int));
p->val[length] = candidates[i];
p->next = head;
head = p;
}else{
if(tail == NULL){//第一个符合情况的出现时,得到链表头
tail = getTargetArray(candidates,candidatesSize,surplus - candidates[i],i,length + 1);
if(tail != NULL){//如果找到了符合的情况,添加当前的数
head = tail;
tail->val[length] = candidates[i];
}
}else{
tail->next = getTargetArray(candidates,candidatesSize,surplus - candidates[i],i,length + 1);
}
while(tail != NULL && tail->next != NULL){
tail->next->val[length] = candidates[i];
tail = tail->next;
}
}
} return head;
} /**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
struct linklist *cs = NULL;
struct linklist *tail = cs;
int **retStr = NULL;
int *subStr = NULL;
int subStrSize,count = 0; qsort(candidates,candidatesSize,sizeof(int),cmp); //递增排序,目标数比当前值还小,后面的就更加不可能了
for(int i = 0;i < candidatesSize && target >= candidates[i];i++){
if(target - candidates[i] == 0){
struct linklist *p = (struct linklist *)malloc(sizeof(struct linklist));
p->size = 1;
p->val = (int *)malloc(sizeof(int));
p->val[0] = candidates[i];
p->next = cs;
cs = p;
count++;
}else{
if(tail == NULL){//第一个符合情况的出现时,得到链表头
tail = getTargetArray(candidates,candidatesSize,target - candidates[i],i,1);
if(tail != NULL){//如果找到了符合的情况,添加当前的数
cs = tail;
tail->val[0] = candidates[i];
count++;
}
}else{
tail->next = getTargetArray(candidates,candidatesSize,target - candidates[i],i,1);
}
while(tail != NULL && tail->next != NULL){//如果找到了符合的情况,添加当前的数
tail->next->val[0] = candidates[i];
tail = tail->next;
count++;
}
}
} retStr = (int **)malloc(count*sizeof(int *));
*columnSizes = (int *)malloc(count*sizeof(int));
struct linklist *p = NULL;
int i = 0;
while(cs != NULL){
p = cs;
cs = cs->next;
p->next = NULL;
retStr[i] = (int *)malloc(p->size*sizeof(int));
columnSizes[0][i] = p->size;
for(int j = 0;j < p->size;j++)retStr[i][j] = p->val[j];
free(p->val);
free(p);
i++;
} *returnSize = count;
return retStr;
} void main(){
int a[] = {2, 3};
int target = 6,size;
int *length = NULL;
int **ret = combinationSum(a,sizeof(a)/sizeof(int),target,&length,&size);
printf("%d\n",size);
for(int i = 0;i < size;i++){
for(int j = 0;j < length[i];j++){
printf("%d ",ret[i][j]);
}
free(ret[i]);
printf("\n");
} free(length);
free(ret);
}
6.Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
从数组中找和是目标数的所有组合,数组元素可以使用1次
1 /***************************************************************************************************
2 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
3 Each number in C may only be used once in the combination.
4 Note:
5 All numbers (including target) will be positive integers.
6 The solution set must not contain duplicate combinations.
7 For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
8 A solution set is:
9 [
10 [1, 7],
11 [1, 2, 5],
12 [2, 6],
13 [1, 1, 6]
14 ]
15 ***************************************************************************************************/
16 #include<stdio.h>
17 #include<stdbool.h>
18
19 struct linklist{
20 int *val;
21 int size;
22 struct linklist *next;
23 };
24
25 int cmp(const void *a , const void *b){
26 return *(int *)a > *(int *)b ? 1 : -1;
27 }
28
29 struct linklist *getTargetArray(int *candidates,int candidatesSize,int surplus,int start,int length){
30 struct linklist *head = NULL;
31 struct linklist *tail = head;
32 int subStrSize;
33 for(int i = start + 1;i < candidatesSize && surplus >= candidates[i];i++){
34 if(surplus - candidates[i] == 0){
35 struct linklist *p = (struct linklist *)malloc(sizeof(struct linklist));
36 p->size = length + 1;
37 p->val = (int *)malloc(p->size*sizeof(int));
38 p->val[length] = candidates[i];
39 p->next = head;
40 head = p;
41 }else{
42 if(tail == NULL){
43 tail = getTargetArray(candidates,candidatesSize,surplus - candidates[i],i,length + 1);
44 if(tail != NULL){
45 head = tail;
46 tail->val[length] = candidates[i];
47 }
48 }else{
49 tail->next = getTargetArray(candidates,candidatesSize,surplus - candidates[i],i,length + 1);
50 }
51 while(tail != NULL && tail->next != NULL){
52 tail->next->val[length] = candidates[i];
53 tail = tail->next;
54 }
55 }
56 while(candidates[i] == candidates[i+1])i++;
57 }
58
59 return head;
60 }
61
62 /**
63 * Return an array of arrays of size *returnSize.
64 * The sizes of the arrays are returned as *columnSizes array.
65 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
66 */
67 int** combinationSum2(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
68 struct linklist *cs = NULL;
69 struct linklist *tail = cs;
70 int **retStr = NULL;
71 int *subStr = NULL;
72 int subStrSize,count = 0;
73
74 qsort(candidates,candidatesSize,sizeof(int),cmp);
75
76 for(int i = 0;i < candidatesSize && target >= candidates[i];i++){
77 if(target - candidates[i] == 0){
78 struct linklist *p = (struct linklist *)malloc(sizeof(struct linklist));
79 p->size = 1;
80 p->val = (int *)malloc(sizeof(int));
81 p->val[0] = candidates[i];
82 p->next = cs;
83 cs = p;
84 count++;
85 }else{
86 if(tail == NULL){
87 tail = getTargetArray(candidates,candidatesSize,target - candidates[i],i,1);
88 if(tail != NULL){
89 cs = tail;
90 tail->val[0] = candidates[i];
91 count++;
92 }
93 }else{
94 tail->next = getTargetArray(candidates,candidatesSize,target - candidates[i],i,1);
95 }
96 while(tail != NULL && tail->next != NULL){
97 tail->next->val[0] = candidates[i];
98 tail = tail->next;
99 count++;
100 }
101 }
102 while(candidates[i] == candidates[i+1])i++;
103 }
104
105 retStr = (int **)malloc(count*sizeof(int *));
106 *columnSizes = (int *)malloc(count*sizeof(int));
107 struct linklist *p = NULL;
108 int i = 0;
109 while(cs != NULL){
110 p = cs;
111 cs = cs->next;
112 p->next = NULL;
113 retStr[i] = (int *)malloc(p->size*sizeof(int));
114 columnSizes[0][i] = p->size;
115 for(int j = 0;j < p->size;j++)retStr[i][j] = p->val[j];
116 free(p->val);
117 free(p);
118 i++;
119 }
120
121 *returnSize = count;
122 return retStr;
123 }
124
125 void main(){
126 int a[] = {2,2,2};
127 int target = 4,size;
128 int *length = NULL;
129 int **ret = combinationSum2(a,sizeof(a)/sizeof(int),target,&length,&size);
130 printf("%d\n",size);
131 for(int i = 0;i < size;i++){
132 for(int j = 0;j < length[i];j++){
133 printf("%d ",ret[i][j]);
134 }
135 free(ret[i]);
136 printf("\n");
137 }
138
139 free(length);
140 free(ret);
141 }
题目:Combination Sum III
从1-9中找k个不同的数保证其和为n。返回所有符合要求的组合。
思路:
通过递归找k个元素求和,和不等于n或个数大于k,则回溯。
void LeetCode::combinationSum(vector<vector<int>>&result, vector<int>& arr, int k, int n){
if (arr.size() == k && !n){//找到一个可行的结果
result.push_back(arr);
return;
}
if (arr.size() >= k)return;//没有合适的
int i = arr.size() ? arr.at(arr.size() - ) + : ;//从最后一个元素的后一个数开始
for (; i <= && n - i >= ; ++i){
arr.push_back(i);//装入
combinationSum(result, arr, k, n - i);//递归找下一个
arr.pop_back();
}
} vector<vector<int>> LeetCode::combinationSum3(int k, int n){
vector<vector<int>>result;
vector<int>arr;
combinationSum(result, arr, k, n);
return result;
}
持续更新...
[LeetCode]sum合集的更多相关文章
- Javascript 语言精粹 代码片段合集
Javascript 语言精粹 代码片段合集 标签:Douglas-Crockford Javascript 最佳实践 原文链接 更好的阅读体验 使用一个method 方法定义新方法 Function ...
- [题解+总结]动态规划大合集II
1.前言 大合集总共14道题,出自江哥之手(这就没什么好戏了),做得让人花枝乱颤.虽说大部分是NOIP难度,也有简单的几道题目,但是还是做的很辛苦,有几道题几乎没思路,下面一道道边看边分析一下. 2. ...
- 11、js 数组详细操作方法及解析合集
js 数组详细操作方法及解析合集 前言 在开发中,数组的使用场景非常多,平日中也涉及到很多数组的api/相关操作,一直也没有对这块内容进行一块整理总结,很多时候就算用过几次这个api,在开发中也很容易 ...
- 9.15 DP合集水表
9.15 DP合集水表 显然难了一些啊. 凸多边形的三角剖分 瞄了一眼题解. 和蛤蛤的烦恼一样,裸的区间dp. 设f[i][j]表示i~j的点三角剖分最小代价. 显然\(f[i][i+1]=0,f[i ...
- 网络流合集:bzoj1433,1934,1854 题解
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23992205 网络流/二分图大合集 [NO.1*原题] 1433: [ZJOI200 ...
- 面试必备:高频算法题终章「图文解析 + 范例代码」之 矩阵 二进制 + 位运算 + LRU 合集
Attention 秋招接近尾声,我总结了 牛客.WanAndroid 上,有关笔试面经的帖子中出现的算法题,结合往年考题写了这一系列文章,所有文章均与 LeetCode 进行核对.测试.欢迎食用 本 ...
- 动态规划大合集II
1.前言 大合集总共14道题,出自江哥之手(这就没什么好戏了),做得让人花枝乱颤.虽说大部分是NOIP难度,也有简单的几道题目,但是还是做的很辛苦,有几道题几乎没思路,下面一道道边看边分析一下. 2. ...
- Java学习、面试、求职、干货资源精品合集
本系列文章整合了本号发表和转载过的,有关Java学习.进阶.面试.做项目.求职经验等方面的文章,希望对想要找工作,以及正在找工作的你,能够有所帮助. 原创Java学习专题文章: 如何才能够系统地学习J ...
- 技巧&注意事项合集
技巧&注意事项合集 杂项 OI Wiki有很多实用的东西 编程环境 打开Dev-C++中工具-编译选项-代码生成/优化-代码警告-显示最多警告信息的开关,可以检查出一堆傻逼错误 define ...
随机推荐
- egret项目发布为微信小程序
1.更改游戏的缩放模式 因为微信小游戏不支持showAll,如果你使用了showAll模式.可以改成fixedWidth或fixedHeight.在index.html里修改,如下图 但这个又涉及到了 ...
- GLFW+GLAD OpenGL Mac开发环境搭建
前言 OpenGL 是什么?The Industry Standard for High Performance Graphics 这是官方解释.说白了他就是一套标准接口.对,是接口,并没有实现具体的 ...
- 用java自制简易线程池(不依赖concurrent包)
很久之前人们为了继续享用并行化带来的好处而不想使用进程,于是创造出了比进程更轻量级的线程.以linux为例,创建一个进程需要申请新的自己的内存空间,从父进程拷贝一些数据,所以开销是比较大的,线程(或称 ...
- Python之读取用户指令和格式化打印
Python之读取用户指令和格式化打印 一.读取用户指令 当你的程序要接收用户输入的指令时,可以用input函数: name = input("请输入你的名字:") print(& ...
- Egret白鹭开发小游戏之自定义load加载界面
刚接触不久就遇到困难------自定义loading.想和其他获取图片方式一样获取加载界面的图片,结果发现资源还没加载就需要图片,在网上百度了许多,都没有找到正确的方式,通过自己的摸索,终于,,,我成 ...
- Codeforces 936B
题意略. 思路: 图论里掺杂了一些动态规划. 有几个注意点: 1.dp时状态的设计:因为我们要寻求的是出度为0并且可以从起点走奇数步抵达的点,由于同一个点可以通过多种方式到达. 并且我们在获得奇数步点 ...
- 分布式日志收集系统 —— Flume
一.Flume简介 Apache Flume 是一个分布式,高可用的数据收集系统.它可以从不同的数据源收集数据,经过聚合后发送到存储系统中,通常用于日志数据的收集.Flume 分为 NG 和 OG ( ...
- Mybatis与SQL Server类型转换遇到的坑
一. MyBatis SQL语句遇到的性能问题 1. 场景还原 假设我们有一张User表,其中包含userId.userName.gender字段,其中userId的数据类型为char(20),此时我 ...
- springboot-vue前后端分离session过期重新登录
springboot-vue前后端分离session过期重新登录 简单回顾cookie和session cookie和session都是回话管理的方式 Cookie cookie是浏览器端存储信息的一 ...
- 关于turtle画蟒蛇小实例
import turtle turtle.setup(800,600) turtle.pensize(25) turtle.pencolor('blue') turtle.penup() #抬笔 tu ...