Golang生成-100,100之间随机整数
如果是0,100的整数,可以这样生成 ``` func GenerateRangeNum(min, max int) int { rand.Seed(time.Now().Unix()) randNum := rand.Intn(max - min) + min return randNum } ``` 但是要生成一个负数到正数之间的随机数呢,怎么样写比较好...阅读全文
如果是0,100的整数,可以这样生成 ``` func GenerateRangeNum(min, max int) int { rand.Seed(time.Now().Unix()) randNum := rand.Intn(max - min) + min return randNum } ``` 但是要生成一个负数到正数之间的随机数呢,怎么样写比较好...阅读全文
time.Weekday类型可以做运算,强制转int,会得到偏差数。默认是 Sunday 开始到 Saturday 算 0,1,2,3,4,5,6 所以只有Monday减去Sunday的时候是正数,特殊处理下就可以了。 package date import ( "fmt" "time" ) func WeekDayTest() { now := time.Now() offset := int(time.Monday - now.Weekday()) if offset > 0 { offset = -6 } weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(...阅读全文
go源码阅读笔记(math.1) abs.go func Abs(x float64) float64 package math // Abs returns the absolute value of x. // // Special cases are: // Abs(±Inf) = +Inf // Abs(NaN) = NaN func Abs(x float64) float64 { // TODO: once golang.org/issue/13095 is fixed, change this to: // return Float64frombits(Float64bits(x) &^ (1 << 63)) // But for now, this generates bet...阅读全文
用golang写这道题的做法完全不同,用到了gorutine, channel,写着挺有意思的 题目描述: LeetCode 566. Reshape the Matrix In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and crepresenting the ...阅读全文
题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck! 示例: 输入:target = 9 输出:[[2,3,4],[4,5]] 思路 1.这道题可以使用“滑动窗口”+“双指针”的思想解决。 2.设置两个指针,这两个指针用于标识目前所属的范围。 当前范围内的和,可以通过等差数列的求和公式 sum=(low+high)(high-low+1)/2* 求出 当sum>target时,low指针右移 当...阅读全文
题目 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively. The resha...阅读全文
Golang: 思路:这题还是贪心解法,如果一个数组中有负数,先翻转负数,如果负数的数目大于等于k,那么把所有最小的负数翻转完即可。如果不是,那么我们就要对此时全部是正数的数组进行翻转,那么翻转的是当前数组中最小的元素。 代码如下: func largestSumAfterKNegations(A []int, K int) int { sort.Ints(A) temp,k:=0,K if A[0]<0{//代表有负数,那么先翻转负数部分 i:=0 for ;i