LeetCode第五天
leetcode 第五天
2018年1月6日
22.(566) Reshape the Matrix


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

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

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

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
新知识点:动态规划(有点难度)

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

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

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()等操作

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;
}
}
LeetCode第五天的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- leetcode第五题--Longest Palindromic Substring
Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...
- LeetCode第五十八题
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- LeetCode 第五题 最长的回文字符串 (JAVA)
Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...
- LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 【LeetCode每日一题 Day 5】5. 最长回文子串
大家好,我是编程熊,今天是LeetCode每日一题的第五天,一起学习LeetCode第五题<最长回文子串>. 题意 给你一个字符串 s,找到 s 中最长的回文子串. 示例 输入:s = & ...
- LeetCode算法题-Number Complement(Java实现-五种解法)
这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...
- LeetCode算法题-Longest Palindrome(五种解法)
这是悦乐书的第220次更新,第232篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第87题(顺位题号是409).给定一个由小写或大写字母组成的字符串,找到可以用这些字母构 ...
- LeetCode算法题-Find the Difference(Java实现-五种解法)
这是悦乐书的第214次更新,第227篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第82题(顺位题号是389).给定两个字符串s和t,它们只包含小写字母.字符串t由随机混 ...
随机推荐
- JSTL遇到的问题
1.jstl 中不可以用关键字命名 例如class new. 2.jstl取值的问题 如果jstl通过对象.属性取值 属性值中包括特殊字符(例如:31/20180131195356867.txt&qu ...
- 8_python连接数据库
如何用python操作数据库? -- 导入pymysql -- import pymysql -- 创建连接 - ...
- lvs_dr
lvs_dr 实验需求(4台虚拟机) eth0 192.168.1.110 单网卡 client(可以使用windows浏览器代替,但会有缓存影响) eth0 192.168.1.186 单网卡 di ...
- Struts2中实现随机验证码
一.创建RandomNum类 1: import java.awt.Color; 2: import java.awt.Font; 3: import java.awt.Graphics; 4: im ...
- git常用命令,学git代码管理
下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一. ...
- 在Web.config中配置handler
在Web.config中配置handler节点时发现用vs2010和用vs2015竟然不一样,经过多次测试发现了一些倪端: <configuration> <!--vs2010中需要 ...
- js结合方式:
1. 在页面中使用script标签,在标签中书写js代码.<head><script type="text/javascript"> alert(&q ...
- 使用document.execCommand复制内容至剪贴板
API https://developer.mozilla.org/zh-CN/docs/Web/API/Document/execCommand 兼容性 http://caniuse.com/#se ...
- elasticsearch的percolator操作
es的普通查询是通过某些条件来查询满足的文档,percolator则不同,先是注册一些条件,然后查询一条文档是否满足其中的某些条件. es的percolator特性在数据分类.数据路由.事件监控和预警 ...
- CenOS 上安装 Redis 服务器
1.构建 Redis 因为 Redis 官方没提供 RPM 安装包,所以需要编译源代码,则需要安装 GCC & MAKE. 终端输入: yum install gcc make 从官网下载 t ...