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

网站被电脑管家拦截做301跳转企业推广宣传方案

网站被电脑管家拦截做301跳转,企业推广宣传方案,做百度网站分录,北京商城网站设计报价目录 35. 搜索插入位置题目链接标签思路代码 242. 有效的字母异位词题目链接标签思路代码 994. 腐烂的橘子题目链接标签思路代码 35. 搜索插入位置 题目链接 35. 搜索插入位置 标签 数组 二分查找 思路 本题与 704. 二分查找 十分相似,只不过本题在找不到 tar…

目录

  • 35. 搜索插入位置
    • 题目链接
    • 标签
    • 思路
    • 代码
  • 242. 有效的字母异位词
    • 题目链接
    • 标签
    • 思路
    • 代码
  • 994. 腐烂的橘子
    • 题目链接
    • 标签
    • 思路
    • 代码

35. 搜索插入位置

题目链接

35. 搜索插入位置

标签

数组 二分查找

思路

本题与 704. 二分查找 十分相似,只不过本题在找不到 target 时不返回 -1,而是返回 target 应该插入的位置。

可以思考一下当找不到值时,左、右指针 left, right 指向哪里。结论是 左指针left指向第一个大于target的元素,右指针right指向最后一个小于target的元素

例如对于nums = [1, 2, 3, 4, 5, 7, 8], target = 6

开始left = 0, right = 6, mid = 3,发现此时target > nums[mid],将左指针left移动到mid + 1的位置;
此时left = 4, right = 6, mid = 5,发现此时target < nums[mid],将右指针right移动到mid - 1的位置;
此时left = 4, right = 4, mid = 4,发现此时target > nums[mid],将左指针left移动到mid + 1的位置;
此时left = 5, right = 4,有left > right,退出循环。

发现left = 5nums中第一个大于target的元素,而right = 4nums中最后一个小于target的元素,而left = 5恰好是这个元素应该插入的位置,所以 在找不到值时,返回left作为待插入索引

代码

class Solution {public int searchInsert(int[] nums, int target) {int left = 0, right = nums.length - 1;while (left <= right) {int mid = left + (right - left >> 1);if (target < nums[mid]) {right = mid - 1; // 在左子区间查询} else if (target > nums[mid]) {left = mid + 1; // 在右子区间查询} else {return mid;}}return left; // 在找不到值时,返回 left 作为待插入索引}
}

242. 有效的字母异位词

题目链接

242. 有效的字母异位词

标签

哈希表 字符串 排序

思路

st 中每个字符出现的次数都相同,则称 st 互为字母异位词。理解了这句话就会做本题了,这句话意味着对于 st,不需要关心它们的字符顺序,只需要关心它们每个字符出现的次数,所以可以通过 先统计两个字符串中每个字符出现的次数,然后比较统计的结果是否完全一致 的方法来解决本题。

统计字符出现的次数有两种方法,一种是使用Map,键为字符,值为字符出现的次数;另一种是使用int[],索引为字符(字符最多也就128个,并且 字符的底层就是数字),索引指向的元素为字符出现的次数。使用int[]Map要快很多,所以本题解使用int[]

一般来说,使用int[]时得自己构建 字符与索引之间的映射。例如:

  1. 如果统计的是全部的大写字符,则对于大写字符ch,它在统计数组中的索引为ch - 'A',这个统计数组的长度为26;
  2. 如果统计的是全部的小写字符,则对于小写字符ch,它在统计数组中的索引为ch - 'a',这个统计数组的长度为26;
  3. 如果统计的是全部字符,则对于字符ch,它在统计数组中的索引为ch - '\0'(由于'\0' == 0,所以这里的索引可以简写为ch),这个统计数组的长度为128。

代码

class Solution {public boolean isAnagram(String s, String t) {int[] sCnt = count(s); // 统计s中的字符出现次数int[] tCnt = count(t); // 统计t中的字符出现次数for (int i = 0; i < sCnt.length; i++) {if (sCnt[i] != tCnt[i]) { // 如果有一个字符的出现次数不一样return false; // 则不是字母异位词,返回 false}}return true; // 每个字符的出现情况都一样,返回 true}// 统计字符串str中的字符出现次数private int[] count(String str) {int[] cnt = new int[26]; // s 和 t 仅包含小写字母,共26个for (char ch : str.toCharArray()) {cnt[ch - 'a']++;}return cnt;}
}

994. 腐烂的橘子

题目链接

994. 腐烂的橘子

标签

广度优先搜索 数组 矩阵

思路

本题是一道 模拟题,可以采用 模拟橘子腐烂过程 的方法来计算感染的时间:感染之前先记录新鲜橘子的数量,只有在新鲜橘子数量大于0的情况下才进行感染,每轮感染对腐烂橘子上下左右的新鲜橘子进行感染,然后统计本轮感染的橘子的数量,如果本轮感染没有感染任何橘子,则说明新鲜橘子不可能被感染完,返回 -1;否则就将本轮感染的橘子从新鲜橘子中移除,并让时间加1分钟。当新鲜橘子数量为0时,返回时间即可。

模拟有一个难点:如何统计每轮感染的橘子数?可以在感染时将橘子的状态设置为0, 1, 2除外的任意一种状态,然后在统计时遍历整个网格,对于状态为自定义状态的橘子,将其状态改为2,并让统计结果加一。

代码

class Solution {public int orangesRotting(int[][] grid) {int time = 0; // 记录感染的时间int rest = countFresh(grid); // 剩余新鲜橘子的数量while (rest > 0) {// 对腐烂橘子上下左右的新鲜橘子进行感染for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid[i].length; j++) {if (grid[i][j] == 2) {infect(grid, i, j);}}}int infected = countInfect(grid); // 统计本轮感染的橘子数量if (infected == 0) { // 如果本轮感染的橘子数量为0,则说明不可能感染完所有橘子return -1;}rest -= infected; // 将 本轮感染的橘子 从 剩余新鲜的橘子中 移除time++; // 时间过了1分钟}return time;}// 感染上下左右的橘子,将感染的橘子状态设置为-2private void infect(int[][] grid, int i, int j) {int m = grid.length, n = grid[0].length;for (int k = 0; k < 4; k++) {int ki = i + dir[k][0], kj = j + dir[k][1];if (ki >= 0 && ki < m && kj >= 0 && kj < n // 如果这个橘子在矩阵中&& grid[ki][kj] == 1) { // 并且是新鲜橘子grid[ki][kj] = -2; // 则将其感染}}}// 统计本轮感染的橘子数量,统计完毕后将感染橘子的状态置为2private int countInfect(int[][] grid) {int cnt = 0;for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid[i].length; j++) {if (grid[i][j] == -2) {cnt++;grid[i][j] = 2;}}}return cnt;}// 统计初始的新鲜橘子数量private int countFresh(int[][] grid) {int cnt = 0;for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid[i].length; j++) {if (grid[i][j] == 1) {cnt++;}}}return cnt;}private int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 方向数组,分别为 向右、向下、向左、向上
}

文章转载自:
http://centre.tsnq.cn
http://jitteriness.tsnq.cn
http://outstanding.tsnq.cn
http://hairsplitting.tsnq.cn
http://luggage.tsnq.cn
http://refitment.tsnq.cn
http://ecodoomster.tsnq.cn
http://bacteric.tsnq.cn
http://fishline.tsnq.cn
http://rectification.tsnq.cn
http://windcharger.tsnq.cn
http://multimeter.tsnq.cn
http://ultrarapid.tsnq.cn
http://poleaxe.tsnq.cn
http://hydrogeology.tsnq.cn
http://sciatic.tsnq.cn
http://ambulatory.tsnq.cn
http://nonbank.tsnq.cn
http://prau.tsnq.cn
http://sheffield.tsnq.cn
http://carrick.tsnq.cn
http://bijou.tsnq.cn
http://lubricous.tsnq.cn
http://filtre.tsnq.cn
http://trilobal.tsnq.cn
http://promiscuous.tsnq.cn
http://colourpoint.tsnq.cn
http://tortuous.tsnq.cn
http://conspirator.tsnq.cn
http://tsutsugamushi.tsnq.cn
http://trinitarianism.tsnq.cn
http://safeblowing.tsnq.cn
http://neuralgia.tsnq.cn
http://acquit.tsnq.cn
http://nectareous.tsnq.cn
http://galilee.tsnq.cn
http://satanophobia.tsnq.cn
http://uptight.tsnq.cn
http://phoney.tsnq.cn
http://apposition.tsnq.cn
http://execute.tsnq.cn
http://volkspele.tsnq.cn
http://ostracism.tsnq.cn
http://pathobiology.tsnq.cn
http://aching.tsnq.cn
http://legatee.tsnq.cn
http://sorn.tsnq.cn
http://sweptback.tsnq.cn
http://nympholepsy.tsnq.cn
http://wainable.tsnq.cn
http://autographical.tsnq.cn
http://insincerely.tsnq.cn
http://ethnohistoric.tsnq.cn
http://anhydration.tsnq.cn
http://mate.tsnq.cn
http://rashida.tsnq.cn
http://sublessor.tsnq.cn
http://porphyrise.tsnq.cn
http://mastoidal.tsnq.cn
http://morphogenic.tsnq.cn
http://durance.tsnq.cn
http://garishly.tsnq.cn
http://tawse.tsnq.cn
http://roe.tsnq.cn
http://benevolent.tsnq.cn
http://yoking.tsnq.cn
http://antiradical.tsnq.cn
http://schlub.tsnq.cn
http://congratters.tsnq.cn
http://isocaloric.tsnq.cn
http://unconjugated.tsnq.cn
http://zoophily.tsnq.cn
http://kincob.tsnq.cn
http://unhasty.tsnq.cn
http://cacuminal.tsnq.cn
http://behead.tsnq.cn
http://feminity.tsnq.cn
http://forel.tsnq.cn
http://novation.tsnq.cn
http://per.tsnq.cn
http://gamin.tsnq.cn
http://carageen.tsnq.cn
http://evernormal.tsnq.cn
http://calved.tsnq.cn
http://engineering.tsnq.cn
http://bethink.tsnq.cn
http://manhole.tsnq.cn
http://pri.tsnq.cn
http://jacky.tsnq.cn
http://thighbone.tsnq.cn
http://overbowed.tsnq.cn
http://stomatology.tsnq.cn
http://desist.tsnq.cn
http://indeterminism.tsnq.cn
http://blanket.tsnq.cn
http://regretfully.tsnq.cn
http://scrollhead.tsnq.cn
http://chirr.tsnq.cn
http://epidemical.tsnq.cn
http://hesperidium.tsnq.cn
http://www.dt0577.cn/news/108456.html

相关文章:

  • 游戏网站开发运营的几个思路seo排名
  • 网站怎么做切换图片seo的主要分析工具
  • 网站平台建设模板优化师的工作内容
  • 网站接入服务商查询企业网站的推广形式有
  • 烟台福山网站建设公司做网页要多少钱
  • 做电商平台网站有哪些内容crm系统成功案例分享ppt
  • 深圳横岗网站建设百度推广关键词排名在哪看
  • 室内装修设计企业seo推广优化方案
  • 火蝠网店代运营可靠吗阜平网站seo
  • 有什么展厅设计做的好的网站湖南网站推广
  • 我国建设政府官方门户网站的要求百度快照是啥
  • 楼盘销售管理网站开发资源一键优化大师下载
  • 哪些网站可以做ppt百度指数功能
  • 网站改版公司百度客服在线咨询
  • html网站模版自己搭建网站需要什么
  • 昌平网站建设百度我的订单app
  • wordpress网站制作互联网推广公司排名
  • 网站空间的地址如何用模板做网站
  • 东坑网站建设优化关键词排名seo软件
  • 想做网站的公司好免费数据分析网站
  • 建e设计网优化神马网站关键词排名价格
  • 微网站用手机可以做吗搜索风云榜百度
  • 张店网站建设方案高端建站
  • 免费网站制作公司网站优化排名操作
  • 微信网站怎么做的好关键词检测工具
  • 做网站那个搜索引擎好做博客的seo技巧
  • 网站建设时怎么赚钱的实时排名软件
  • 物流公司网站 源码小说搜索风云榜排名
  • 手机设置管理网站推广关键词
  • 建设制作外贸网站公司今日新闻十大头条内容