LeetCode第五天

leetcode 第五天

2018年1月6日

22.(566) Reshape the Matrix

1192699-20180106092453174-158033009.png
1192699-20180106092511768-1722004403.png

JAVA
class Solution {public int[][] matrixReshape(int[][] nums, int r, int c) {int[][] newNums = new int[r][c];int size = nums.length*nums[0].length;if(r*c != size)return nums;for(int i=0;i<size;i++){newNums[i/c][i%c] = nums[i/nums[0].length][i%nums[0].length];}return newNums;}
}

23.(268) Missing Number

1192699-20180106100821628-1846082373.png

JAVA
class Solution {/*数列求和思想*/public int missingNumber(int[] nums) {int expectSum = nums.length*(nums.length+1)/2;int actualSum = 0;for(int num : nums) actualSum += num;return expectSum - actualSum;}
}

24.(243) ==Shortest Word Distance==

1192699-20180106103722409-2105847597.png

JAVA
class Solution {public int shortestDistance(String[] words,String word1,String word2) {int idx1 = -1,idx2 = -1;int minDistance = words.length;int currentDistance;for(int i =0;i<words.length;i++){if(words[i].equals(word1))idx1=i;else if(words[i].equals(word2))idx2=i;if(idx1 != -1 && idx2 != -1){minDistance = Math.min(minDistance,Math.abs(idx1-idx2));}}return minDistance;}
}

25.(561) Array Partition I

1192699-20180106105515096-123981321.png

JAVA
class Solution {public int arrayPairSum(int[] nums) {Arrays.sort(nums);int minSum = 0;for(int i = 0;i<nums.length;i+=2){minSum += Math.min(nums[i],nums[i+1]);}return minSum;}
}

26.(746) ==Min Cost Climbing Stairs==

==新知识点:动态规划(有点难度)==
1192699-20180106120332971-613292929.png

JAVA
class Solution {public int minCostClimbingStairs(int[] cost) {int f1=0;int f2=0;int f3=0;//f3为到达每一个楼层所需要花费的最小钱数(此楼层花费不算)for(int i = 2;i <= cost.length;i++){f3 = Math.min(f1+cost[i-2],f2+cost[i-1]);f1 = f2;f2 = f3;}return f3;}
}

27.(724) Find Pivot Index

1192699-20180106121417346-935617264.png

JAVA
class Solution {public int pivotIndex(int[] nums) {int sum = 0,leftSum = 0;for(int num : nums) sum+=num;for(int i = 0;i < nums.length;i++){if(leftSum == sum - leftSum - nums[i])return i;leftSum += nums[i];}return -1;}
}

28.(66) Plus One

1192699-20180106122930331-1729052128.png

JAVA
class Solution {public int[] plusOne(int[] digits) {int n = digits.length;for(int i = n-1;i>=0;i--){if(digits[i]<9){digits[i]++;return digits;}digits[i] = 0;}//此处是为了防止原始数字为999...的情况int[] result = new int[n+1];result[0] = 1;return result;}
}

29.(1) Two Sum

==注意Map/HashMap的声明、get()/containsKey()/put()等操作==
1192699-20180106124145081-1906901766.png

JAVA
class Solution {public int[] twoSum(int[] nums, int target) {Map<Integer,Integer> map = new HashMap<Integer,Integer>();int[] result;for(int i = 0;i<nums.length;i++){if(map.containsKey(target - nums[i])){return new int[] {map.get(target-nums[i]),i};}else{map.put(nums[i],i);}}return null;}
}

转载于:https://www.cnblogs.com/guoyaohua/p/8215722.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391531.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

matplotlib可视化_使用Matplotlib改善可视化设计的5个魔术技巧

matplotlib可视化It is impossible to know everything, no matter how much our experience has increased over the years, there are many things that remain hidden from us. This is normal, and maybe an exciting motivation to search and learn more. And I am sure …

adb 多点触碰_无法触及的神话

adb 多点触碰On Twitter, in Slack, on Discord, in IRC, or wherever you hang out with other developers on the internet, you may have heard some formulation of the following statements:在Twitter上&#xff0c;在Slack中&#xff0c;在Discord中&#xff0c;在IRC…

robot:循环遍历数据库查询结果是否满足要求

使用list类型变量{}接收查询结果&#xff0c;再for循环遍历每行数据&#xff0c;取出需要比较的数值 转载于:https://www.cnblogs.com/gcgc/p/11424114.html

leetcode 74. 搜索二维矩阵(二分)

编写一个高效的算法来判断 m x n 矩阵中&#xff0c;是否存在一个目标值。该矩阵具有如下特性&#xff1a; 每行中的整数从左到右按升序排列。 每行的第一个整数大于前一行的最后一个整数。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,3,5,7],[10,11,16,20],[23,30,34…

rm命令

命令 ‘rm’ &#xff08;remove&#xff09;&#xff1a;删除一个目录中的一个或多个文件或目录&#xff0c;也可以将某个目录及其下属的所有文件及其子目录均删除掉 语法&#xff1a;rm&#xff08;选项&#xff09;&#xff08;参数&#xff09; 默认会提示‘是否’删除&am…

javascript消除字符串两边空格的两种方式,面向对象和函数式编程。python oop在调用时候的优点...

主要是javascript中消除字符串空格&#xff0c;比较两种方式的不同 //面向对象&#xff0c;消除字符串两边空格 String.prototype.trim function() { return this.replace(/(^\s*)|(\s*$)/g, ""); };//去左右空格的函数; function trim(s){return s.replace(/(^\s*)…

如何使用Retrofit,OkHttp,Gson,Glide和Coroutines处理RESTful Web服务

Kriptofolio应用程序系列-第5部分 (Kriptofolio app series — Part 5) These days almost every Android app connects to internet to get/send data. You should definitely learn how to handle RESTful Web Services, as their correct implementation is the core knowle…

leetcode 90. 子集 II(回溯算法)

给你一个整数数组 nums &#xff0c;其中可能包含重复元素&#xff0c;请你返回该数组所有可能的子集&#xff08;幂集&#xff09;。 解集 不能 包含重复的子集。返回的解集中&#xff0c;子集可以按 任意顺序 排列。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,2] 输…

robot:linux下安装robot环境

https://www.cnblogs.com/lgqboke/p/8252488.html 转载于:https://www.cnblogs.com/gcgc/p/11425588.html

感知器 机器学习_机器学习感知器实现

感知器 机器学习In this post, we are going to have a look at a program written in Python3 using numpy. We will discuss the basics of what a perceptron is, what is the delta rule and how to use it to converge the learning of the perceptron.在本文中&#xff0…

JS解析格式化Json插件,Json和XML互相转换插件

Json对象转换为XML字符串插件 http://www.jsons.cn/Down/jquery.json2xml.js var xml_content $.json2xml(json_object);XML字符串转换为Json对象插件 http://www.jsons.cn/Down/jquery.xml2json.js var json_obj $.xml2json(xml_content);json序列化和反序列化方法插件 …

Python之集合、解析式,生成器,函数

一 集合 1 集合定义&#xff1a; 1 如果花括号为空&#xff0c;则是字典类型2 定义一个空集合&#xff0c;使用set 加小括号使用B方式定义集合时&#xff0c;集合内部的数必须是可迭代对象&#xff0c;数值类型的不可以 其中的值必须是可迭代对象&#xff0c;其中的元素必须是可…

深度神经网络课程总结_了解深度神经网络如何工作(完整课程)

深度神经网络课程总结Even if you are completely new to neural networks, this course from Brandon Rohrer will get you comfortable with the concepts and math behind them.即使您是神经网络的新手&#xff0c;Brandon Rohrer的本课程也会使您熟悉其背后的概念和数学。 …

leetcode 1006. 笨阶乘

通常&#xff0c;正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积。例如&#xff0c;factorial(10) 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1。 相反&#xff0c;我们设计了一个笨阶乘 clumsy&#xff1a;在整数的递减序列中&#xff0c;我们以一个固定顺序的操作符序列来…

python:如何传递一个列表参数

转载于:https://www.cnblogs.com/gcgc/p/11426356.html

curl的安装与简单使用

2019独角兽企业重金招聘Python工程师标准>>> windows 篇&#xff1a; 安装篇&#xff1a; 我的电脑版本是windows7,64位&#xff0c;对应的curl下载地址如下&#xff1a; https://curl.haxx.se/download.html 直接找到下面的这个版本&#xff1a; curl-7.57.0.tar.g…

gcc 编译过程

gcc 编译过程从 hello.c 到 hello(或 a.out)文件&#xff0c; 必须历经 hello.i、 hello.s、 hello.o&#xff0c;最后才得到 hello(或a.out)文件&#xff0c;分别对应着预处理、编译、汇编和链接 4 个步骤&#xff0c;整个过程如图 10.5 所示。 这 4 步大致的工作内容如下&am…

虎牙直播电影一天收入_电影收入

虎牙直播电影一天收入“美国电影协会(MPAA)的首席执行官J. Valenti提到&#xff1a;“没有人能告诉您电影在市场上的表现。 直到电影在黑暗的剧院里放映并且银幕和观众之间都散发出火花。 (“The CEO of Motion Picture Association of America (MPAA) J. Valenti mentioned th…

邮箱如何秘密发送多个人邮件_如何发送秘密消息

邮箱如何秘密发送多个人邮件Cryptography is the science of using codes and ciphers to protect messages, at its most basic level. Encryption is encoding messages with the intent of only allowing the intended recipient to understand the meaning of the message.…

leetcode 面试题 17.21. 直方图的水量(单调栈)

给定一个直方图(也称柱状图)&#xff0c;假设有人从上面源源不断地倒水&#xff0c;最后直方图能存多少水量?直方图的宽度为 1。 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的直方图&#xff0c;在这种情况下&#xff0c;可以接 6 个单位的水&#xff08;蓝色部分表示水&a…