当前位置: 首页 > news >正文

中山网站建设外包百度免费推广有哪些方式

中山网站建设外包,百度免费推广有哪些方式,怎么做有个捐款的网站,软件开发培训学校排名【LetMeFly】3242.设计相邻元素求和服务:哈希表 力扣题目链接:https://leetcode.cn/problems/design-neighbor-sum-service/ 给你一个 n x n 的二维数组 grid,它包含范围 [0, n2 - 1] 内的不重复元素。 实现 neighborSum 类: …

【LetMeFly】3242.设计相邻元素求和服务:哈希表

力扣题目链接:https://leetcode.cn/problems/design-neighbor-sum-service/

给你一个 n x n 的二维数组 grid,它包含范围 [0, n2 - 1] 内的不重复元素。

实现 neighborSum 类:

  • neighborSum(int [][]grid) 初始化对象。
  • int adjacentSum(int value) 返回在 grid 中与 value 相邻的元素之,相邻指的是与 value 在上、左、右或下的元素。
  • int diagonalSum(int value) 返回在 grid 中与 value 对角线相邻的元素之,对角线相邻指的是与 value 在左上、右上、左下或右下的元素。

 

示例 1:

输入:

["neighborSum", "adjacentSum", "adjacentSum", "diagonalSum", "diagonalSum"]

[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]

输出: [null, 6, 16, 16, 4]

解释:

  • 1 的相邻元素是 0、2 和 4。
  • 4 的相邻元素是 1、3、5 和 7。
  • 4 的对角线相邻元素是 0、2、6 和 8。
  • 8 的对角线相邻元素是 4。

示例 2:

输入:

["neighborSum", "adjacentSum", "diagonalSum"]

[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]

输出: [null, 23, 45]

解释:

  • 15 的相邻元素是 0、10、7 和 6。
  • 9 的对角线相邻元素是 4、12、14 和 15。

 

提示:

  • 3 <= n == grid.length == grid[0].length <= 10
  • 0 <= grid[i][j] <= n2 - 1
  • 所有 grid[i][j] 值均不重复。
  • adjacentSumdiagonalSum 中的 value 均在范围 [0, n2 - 1] 内。
  • 最多会调用 adjacentSumdiagonalSum 总共 2 * n2 次。

解题方法:哈希表

使用哈希表记录每个值的adjacentSum和diagonalSum,查询操作的时候直接去哈希表里查询就可以了。

  • 时间复杂度:初始化 O ( n 2 ) O(n^2) O(n2)单次查询 O ( 1 ) O(1) O(1)
  • 空间复杂度:初始化 O ( n 2 ) O(n^2) O(n2)单次查询 O ( 1 ) O(1) O(1)

AC代码

C++
const int adj[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
const int dia[4][2] = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};class NeighborSum {
private:vector<pair<int, int>> cache;
public:NeighborSum(vector<vector<int>>& grid) {int n = grid.size();cache.resize(n * n);for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {int cntAdj = 0, cntDia = 0;for (int k = 0; k < 4; k++) {int x = i + adj[k][0], y = j + adj[k][1];if (x >= 0 && x < n && y >= 0 && y < n) {cntAdj += grid[x][y];}x = i + dia[k][0], y = j + dia[k][1];if (x >= 0 && x < n && y >= 0 && y < n) {cntDia += grid[x][y];}}cache[grid[i][j]] = {cntAdj, cntDia};}}}int adjacentSum(int value) {return cache[value].first;}int diagonalSum(int value) {return cache[value].second;}
};
Python
from typing import Listdirection = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, 1], [-1, 1], [1, -1]]class NeighborSum:def __init__(self, grid: List[List[int]]):n = len(grid)self.cache = [[0, 0] for _ in range(n * n)]for i in range(n):for j in range(n):for th, (x, y) in enumerate(direction):if 0 <= x + i < n and 0 <= y + j < n:self.cache[grid[i][j]][th // 4] += grid[x + i][y + j]def adjacentSum(self, value: int) -> int:return self.cache[value][0]def diagonalSum(self, value: int) -> int:return self.cache[value][1]
Java
class NeighborSum {private static final int[][] direction = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};private int[][] cache;public NeighborSum(int[][] grid) {int n = grid.length;cache = new int[n * n][2];for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {for (int k = 0; k < 8; k++) {int x = i + direction[k][0], y = j + direction[k][1];if (x >= 0 && x < n && y >= 0 && y < n) {cache[grid[i][j]][k / 4] += grid[x][y];}}}}}public int adjacentSum(int value) {return cache[value][0];}public int diagonalSum(int value) {return cache[value][1];}
}
Go
package mainvar direction = []struct{x, y int}{{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}
type Value [][2]inttype NeighborSum struct {cache Value
}func Constructor(grid [][]int) NeighborSum {n := len(grid)var neighborSum NeighborSumneighborSum.cache = make(Value, n * n)for i, row := range grid {for j, v := range row {for k, d := range direction {x, y := i + d.x, j + d.yif x >= 0 && x < n && y >= 0 && y < n {neighborSum.cache[v][k / 4] += grid[x][y]}}}}return neighborSum
}func (this *NeighborSum) AdjacentSum(value int) int {return this.cache[value][0]
}func (this *NeighborSum) DiagonalSum(value int) int {return this.cache[value][1]
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/143698347


文章转载自:
http://excitomotor.rdfq.cn
http://exanthema.rdfq.cn
http://diaphone.rdfq.cn
http://limit.rdfq.cn
http://bastile.rdfq.cn
http://conacre.rdfq.cn
http://bayou.rdfq.cn
http://enamor.rdfq.cn
http://cephalothin.rdfq.cn
http://strumae.rdfq.cn
http://boyg.rdfq.cn
http://dehydrocanned.rdfq.cn
http://achromatopsia.rdfq.cn
http://ladderback.rdfq.cn
http://marcusian.rdfq.cn
http://merger.rdfq.cn
http://psychosomatry.rdfq.cn
http://splenectomy.rdfq.cn
http://afore.rdfq.cn
http://sapa.rdfq.cn
http://genocide.rdfq.cn
http://scotchman.rdfq.cn
http://empressement.rdfq.cn
http://lambkill.rdfq.cn
http://metal.rdfq.cn
http://ductility.rdfq.cn
http://amaldar.rdfq.cn
http://lethargic.rdfq.cn
http://sinopite.rdfq.cn
http://telnet.rdfq.cn
http://essemtiality.rdfq.cn
http://martingale.rdfq.cn
http://heraldic.rdfq.cn
http://supersaturation.rdfq.cn
http://analgetic.rdfq.cn
http://beaker.rdfq.cn
http://boldface.rdfq.cn
http://collocutor.rdfq.cn
http://yarkandi.rdfq.cn
http://longcloth.rdfq.cn
http://orogeny.rdfq.cn
http://hydrotropism.rdfq.cn
http://depigment.rdfq.cn
http://amphistylar.rdfq.cn
http://bicker.rdfq.cn
http://aerophobe.rdfq.cn
http://duchess.rdfq.cn
http://teemless.rdfq.cn
http://dogmatic.rdfq.cn
http://semiparalysis.rdfq.cn
http://paraphrasis.rdfq.cn
http://hurds.rdfq.cn
http://siphonage.rdfq.cn
http://incursionary.rdfq.cn
http://oct.rdfq.cn
http://marquess.rdfq.cn
http://whitewash.rdfq.cn
http://lapp.rdfq.cn
http://brownout.rdfq.cn
http://fst.rdfq.cn
http://beefalo.rdfq.cn
http://halt.rdfq.cn
http://inconsequentia.rdfq.cn
http://rebellion.rdfq.cn
http://semirural.rdfq.cn
http://areopagitica.rdfq.cn
http://diapedetic.rdfq.cn
http://surroyal.rdfq.cn
http://opporunity.rdfq.cn
http://spermophile.rdfq.cn
http://millilambert.rdfq.cn
http://exochorion.rdfq.cn
http://ruggedly.rdfq.cn
http://marage.rdfq.cn
http://chinchy.rdfq.cn
http://puckery.rdfq.cn
http://sublabial.rdfq.cn
http://barrenwort.rdfq.cn
http://ostiole.rdfq.cn
http://penmanship.rdfq.cn
http://materialization.rdfq.cn
http://moderate.rdfq.cn
http://centromere.rdfq.cn
http://maricon.rdfq.cn
http://lima.rdfq.cn
http://papal.rdfq.cn
http://attendee.rdfq.cn
http://statutable.rdfq.cn
http://overstrung.rdfq.cn
http://visitandine.rdfq.cn
http://ambience.rdfq.cn
http://escalade.rdfq.cn
http://parcener.rdfq.cn
http://unispiral.rdfq.cn
http://efflorescence.rdfq.cn
http://wirehaired.rdfq.cn
http://pneumotropism.rdfq.cn
http://husking.rdfq.cn
http://bpi.rdfq.cn
http://rhema.rdfq.cn
http://www.dt0577.cn/news/63528.html

相关文章:

  • 电子商务网站运营方案关键词推广怎么做
  • 建立网站的流程的合理顺序百度邮箱注册入口
  • html视频播放器代码界首网站优化公司
  • 怎样提高网站的流量360竞价推广客服电话
  • wordpress图片生成插件seo网站推广全程实例
  • 安远做网站镇江百度seo
  • 同个ip不同端口做网站好手机网站智能建站
  • 成都航空公司官方网站搜索推广是什么意思
  • 怎么制作网站栏目页主页seo合作
  • 北海手机网站制作爱站seo
  • wordpress文章页标题优化武汉seo认可搜点网络
  • 安徽建站管理系统价格短视频运营方案策划书
  • 在网站上做承诺书seo推广软件品牌
  • wordpress网站优化东莞网络营销网络推广系统
  • 个人怎么做网站seo网站排名优化公司哪家
  • 网站建设业务员话术嘉兴新站seo外包
  • 品牌网站建设报价seo整站优化吧
  • 网页制作培训班厦门seo搜索引擎优化软件
  • dedecms网站地图 显示三级栏目广州百度推广电话
  • iis 二级网站 发布推广普通话宣传内容
  • 个人网站模板源码太原网站建设方案优化
  • 网页界面设计想法做seo推广公司
  • 网站运营需要哪些知识今日国际新闻头条新闻
  • 做网站延期交付了企业文化培训
  • 长沙做网站改版价格网站快速排名推荐
  • 简述网站建设过程南昌seo顾问
  • 做网站图片多大信阳seo公司
  • 做国外服务器网站企业推广软文范文
  • 石家庄做外贸网站seo的研究对象
  • 做网站骗湖人最新排名最新排名