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 ...
随机推荐
- Elasticsearch Internals: Networking Introduction An Overview of the Network Topology
This article introduces the networking part of Elasticsearch. We look at the network topology of an ...
- elasticsearch 口水篇(8)分词 中文分词 ik插件
先来一个标准分词(standard),配置如下: curl -XPUT localhost:9200/local -d '{ "settings" : { "analys ...
- JSON: 介绍、应用
ylbtech-JSON: 介绍.应用 JSONP(JSON with Padding)是 JSON 的一种“使用模式”,可以让网页从别的域名(网站)那获取资料,即跨域读取数据. 为什么我们从不同的 ...
- 学习笔记之Bokeh Data Visualization | DataCamp
Bokeh Data Visualization | DataCamp https://www.datacamp.com/courses/interactive-data-visualization- ...
- go语言学习--map的并发
go提供了一种叫map的数据结构,可以翻译成映射,对应于其他语言的字典.哈希表.借助map,可以定义一个键和值,然后可以从map中获取.设置和删除这个值,尤其适合数据查找的场景.但是map的使用有一定 ...
- jquery.autocomplete 搜索文字提示
function GetJobTitle(obj) { $(obj).autocomplete("GetJobTitle.ashx", { max: 12, //列表里的条目数 m ...
- webview之总结2
21,js与androud交互之javascript调用本地之方法一(接口类): ========= 21,js与androud交互之javascript调用本地之方法一(接口类): Android4 ...
- java基础阶段关于密码或账号字符数字的判断总结
将字符串转成字符数组 首字母判断 思路:应该如何获取首字母 arr[0]为数组第一个元素即是首字母 数字判断true为数字false为非数字 "0123456789".contai ...
- Vue百度搜索
<!DOCTYPE html><html lang="en"><head> <meta charset="GBK"&g ...
- (转)C# WebApi 身份认证解决方案:Basic基础认证
原文地址:http://www.cnblogs.com/landeanfen/p/5287064.html 阅读目录 一.为什么需要身份认证 二.Basic基础认证的原理解析 1.常见的认证方式 2. ...