Go语言中文网 为您找到相关结果 8

go 本周周一的获取

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(...阅读全文

博文 2019-03-18 12:34:43 boywus

go源码阅读笔记(math.1)

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...阅读全文

博文 2016-03-16 00:00:00 qq_15437667

【面试题】联合求和

给定一个候选数字数组 (C) (不含重复项) 和目标数字 (T),在 C 中找到所有唯一的组合,使得组合中的数之和为 T。 C 中的同一个数字可以无限制重复使用。 注意: - 所有数字,包括目标数字,都是正数 - 结果数组中不能包含重复的组合 例如:给定一个候选数组: [2, 3, 6, 7] 和 目标数字 7, 结果数组如下: ``` [ [7], [2, 2, 3] ] ``` Go 函数签名如下: ```go func combinationSum(candidates []int, target int) [][]int { } ``...阅读全文

[LeetCode Golang] 566. Reshape the Matrix 的golang实现

用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 ...阅读全文

博文 2017-08-04 03:04:57 miltonsun

和为S的连续正数序列

题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出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指针右移 当...阅读全文

博文 2020-03-03 00:33:08 youzhihua

[用Golang刷LeetCode之 7] 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 c representing the row number and column number of the wanted reshaped matrix, respectively. The resha...阅读全文

博文 2017-08-14 09:04:59 miltonsun

leetcode_1005

Golang: 思路:这题还是贪心解法,如果一个数组中有负数,先翻转负数,如果负数的数目大于等于k,那么把所有最小的负数翻转完即可。如果不是,那么我们就要对此时全部是正数的数组进行翻转,那么翻转的是当前数组中最小的元素。 代码如下: func largestSumAfterKNegations(A []int, K int) int { sort.Ints(A) temp,k:=0,K if A[0]<0{//代表有负数,那么先翻转负数部分 i:=0 for ;i阅读全文

博文 2020-03-13 11:32:56 淳属虚构