leetcode1006
func clumsy(N int) int {
var ary []int
for n := N; n >= ; n-- {
ary = append(ary, n)
}
re := N % //4个数字一组
firstgroup := true
//最后一组如果不够4个,就补充到4个值,*和/补充1,+和-补充0,这样结果不变
if re == {
//不用补充
} else if re == {
//补充3个
ary = append(ary, ) //补充*1
ary = append(ary, ) //补充/1
ary = append(ary, ) //补充+0
} else if re == {
ary = append(ary, ) //补充/1
ary = append(ary, ) //补充+0
} else if re == {
ary = append(ary, ) //补充+0
}
n := len(ary) //长度是4的倍数
var sum int
for a := ; a <= n-; a += {
temp := ary[a] * ary[a+] / ary[a+]
if firstgroup { //第一组的G1*G2/G3是正
firstgroup = false
} else {
temp = temp * - //非第一组的G1*G2/G3是负
}
temp += ary[a+]
sum += temp
}
return sum
}
这道题的思路就是把运算映射到数组的index上,形成一种规律的运算方式,在循环中进行处理。
leetcode1006的更多相关文章
- leetcode-1006 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [Swift]LeetCode1006. 笨阶乘 | Clumsy Factorial
Normally, the factorial of a positive integer n is the product of all positive integers less than or ...
随机推荐
- 将字符串表示的IP地址转变为整形表示
当时面试上机的想法是,直接使用uint32_t变量来存ip地址,遍历字符串带".",然后去值,利用移位来将这个值填到uint32_t对应的位置上.这样的麻烦之处在于: 1,遍历字符 ...
- 了解轮询、长轮询、长连接、websocket
业务开发中我们往往会有一些需要即时通信的场景,比如微信扫码登录.聊天功能. 下面这四种方式都可以实现即时通信. 轮询: 浏览器通过定时器每隔一段时间向服务器端发送请求,服务器端收到请求并响应请求.没有 ...
- MySQL学习----多版本并发mvcc
MySQL中的大多数事务性存储引擎实现的都不是简单的行级锁.基于提升并发性能的考虑,他们一般实现了多版本并发控制(mvcc).不仅是mysql,包括oracle,postgresql等其他数据库也实现 ...
- IP网络设计
一.总体规划 网络设计的分层思想 按照网络设计的分层思想,通常将网络分为:核心层.汇聚层和接入层三个部分.这三部分在功能上有明显差别 ,因此在IP设计上,有必要对这三个部分区别对待. 二.核心层 核心 ...
- [UE4]Math Expression计算数学公式,可以接受参数
- [UE4]用Blenspace混合空间动画代替AimOffset动画偏移
- MySQL 迁移并搭建主从(实践)
第一阶段 一.数据的初始化 1.老主库 关闭sql_log_binset sql_log_bin = off; 创建导出用户grant all privileges on *.* to 'dump'@ ...
- mono_image_open_from_data_with_name原型
mono4.5 https://github.com/Unity-Technologies/mono 查看mono源码: //PATH: /mono/metadata/image.c MonoImag ...
- (转)C#操作Word文档
原文1地址:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 原文2地址: http://www.cnblogs.com ...
- mysql中min和max查询优化
mysql max() 函数的需扫描where条件过滤后的所有行: 在测试环境中重现: 测试版本:Server version: 5.1.58-log MySQL Community ...