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 …

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

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

rm命令

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

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

感知器 机器学习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…

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

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

Python操作Mysql实例代码教程在线版(查询手册)_python

实例1、取得MYSQL的版本在windows环境下安装mysql模块用于python开发MySQL-python Windows下EXE安装文件下载 复制代码 代码如下:# -*- coding: UTF-8 -*- #安装MYSQL DB for pythonimport MySQLdb as mdb con None try: #连接mysql的方法&#xff1a;connect(ip,user,pass…

批判性思维_为什么批判性思维技能对数据科学家至关重要

批判性思维As Alexander Pope said, to err is human. By that metric, who is more human than us data scientists? We devise wrong hypotheses constantly and then spend time working on them just to find out how wrong we were.正如亚历山大波普(Alexander Pope)所说…

Manjaro 17 搭建 redis 4.0.1 集群服务

安装Redis在Linux环境中 这里我们用的是manjaro一个小众一些的发行版 我选用的是manjaro 17 KDE 如果你已经安装好了manjaro 那么你需要准备一个redis.tar.gz包 这里我选用的是截至目前最新的redis 4.0.1版本 我们可以在官网进行下载 https://redis.io/download选择Stable &…

快速排序简便记_建立和测试股票交易策略的快速简便方法

快速排序简便记Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without se…

robot:List变量的使用注意点

创建list类型变量&#xff0c;两种方式&#xff0c;建议使用Create List关键字 使用该列表变量时需要变为${}方式&#xff0c;切记切记&#xff01; 转载于:https://www.cnblogs.com/gcgc/p/11429482.html

python基础教程(十一)

迭代器 本节进行迭代器的讨论。只讨论一个特殊方法---- __iter__ &#xff0c;这个方法是迭代器规则的基础。 迭代器规则 迭代的意思是重复做一些事很多次---就像在循环中做的那样。__iter__ 方法返回一个迭代器&#xff0c;所谓迭代器就是具有next方法的对象&#xff0c;在调…

美剧迷失_迷失(机器)翻译

美剧迷失Machine translation doesn’t generate as much excitement as other emerging areas in NLP these days, in part because consumer-facing services like Google Translate have been around since April 2006.如今&#xff0c;机器翻译并没有像其他NLP新兴领域那样…

机器学习中决策树的随机森林_决策树和随机森林在机器学习中的使用

机器学习中决策树的随机森林机器学习 (Machine Learning) Machine learning is an application of artificial intelligence that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. The 3 main categor…

【Python算法】遍历(Traversal)、深度优先(DFS)、广度优先(BFS)

图结构&#xff1a; 非常强大的结构化思维&#xff08;或数学&#xff09;模型。如果您能用图的处理方式来规范化某个问题&#xff0c;即使这个问题本身看上去并不像个图问题&#xff0c;也能使您离解决问题更进一步。 在众多图算法中&#xff0c;我们常会用到一种非常实用的思…

我如何预测10场英超联赛的确切结果

Is there a way to predict the outcome of any soccer game with 100% accuracy? The honest and simplest answer is…. no. Regardless of what your fantasy football friends say, there is absolutely no way to be 100% certain, but there is a proven, mathematical …

深度学习数据自动编码器_如何学习数据科学编码

深度学习数据自动编码器意见 (Opinion) When I first wanted to learn programming, I coded along to a 4 hour long YouTube tutorial.刚开始学习编程时&#xff0c;我编写了长达4个小时的YouTube教程。 “Great,” I thought after finishing the course. “I know how to …