[LeetCode C++实现]560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:

Input:nums = [1,1,1], k = 2

Output: 2

Constraints:

The length of the array is in range [1, 20,000].

The range of numbers in the array is [-1000, 1000] and the ran......

[LeetCode C++实现]532. K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

Example 1:

Input: [3, 1, 4, 1, 5], k = 2

Output: 2

E......

Linux中踩栈浅析

调试时必需的栈知识

栈(stack)是程序存放数据的内存区域之一,其特征是LIFO(Last In First Out, 后进先出)式数据结构,即后放进的数据最先被取出。向栈中存储数据的操作称为PUSH(压入),从栈中取出数据称为POP(弹出)。在保存动态分配的自动变量时要使用栈。此外在函数调用时,栈还用于传递函数参数,以及用于保存返回地址和返回值。

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#define MAX (1UL << 20)

typedef unsi......

rc4算法 C语言实现

定位问题居然查到项目中引入的rc4 c算法中的一个bug,也算是大开眼界。

学习rc4 c实现,结合维基百科中的例子,修改网上的例子rc4 c example

/* RC4 Encrypt/Decrypt

RC4 was developped by RSA labs, however this code may not be exact.

Written in portable Micro-C by Tom St Denis.

Notes:

1. I found the 'source' on a discussion website. The user

posting c......

pthread_join函数使用误区

pthread_join函数项目里一般使用第二个参数都使用NULL,不关心线程执行后返回值,有同事发我一段程序,运行后程序segment,却找不到原因,鉴于此本文记录pthread_join的基本用法,文章末尾会给出这段程序,并分析原因。stackoverflow上有一个比较好的问答,介绍了pthread_exit和pthread_join组合使用的习惯用法:

#include <stdio.h>

#include <pthread.h>

#include <stdlib.h>

#include <string.h>

void* thre......

日常开发笔记总结(七)

5月份迎来了今年最忙的一个时期,看mbp本周屏幕使用时间仅55分钟,今天难得不用跑装修相关的事情,得以坐在书桌前,随手翻几页书。

函数指针数组

//seq_array是个数组,内放函数指针

const vector<int *> (*seq_array[])(int) = {

fibon_seq, lucase_seq, pell_seq,

triang_seq, sequare_seq, pent_seq

};

这里其实可以使用typedef进行优化:

typedef const vector<unsigned int>* (*pfunc)(int);......

[LeetCode C++实现]238. Product of Array Except Self

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input: [1,2,3,4]

Output: [24,12,8,6]

Constraint: It's guaranteed that the product of the elements of any prefix or suffix of t......

[LeetCode C++实现]740. Delete and Earn

这道题讲道理是我最近做的收获最大的一道题,最开始我的解法考虑比较片面,没考虑到存在重复元素场景,后面看了讨论区万万没想到这道题居然会和打家劫舍系列问题扯上关系,真的是万万想不到。

最开始的实现代码:

class Solution {

public:

int deleteAndEarn(vector<int>& nums) {

unordered_set<int> hash(nums.begin(),nums.end());

int scores = 0;

helper(hash,scores);

return scores;

}

private:

voi......

[LeetCode C++实现]105. Construct Binary Tree

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

For example, given

preorder = [3,9,20,15,7]

inorder = [9,3,15,20,7]

Return the following binary tree:

3

/ \

9 20

/ \

15 7

记得考研的时候学习数据结构,经常会遇到根据某两种序列求另一种序......

[LeetCode C++实现]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,4],

Output: 6

Explanation: [4,-1,2,1] has the largest sum = 6.

刷到这道题的时候我想起来原来在理工大读书的时候有门课叫算法设计与分析,当时有个0 1背包问题,虽然当时不会写代码,但......