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第五天的更多相关文章

  1. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  2. leetcode第五题--Longest Palindromic Substring

    Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...

  3. LeetCode第五十八题

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  4. LeetCode 第五题 最长的回文字符串 (JAVA)

    Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...

  5. LeetCode第五题:Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. 【LeetCode每日一题 Day 5】5. 最长回文子串

    大家好,我是编程熊,今天是LeetCode每日一题的第五天,一起学习LeetCode第五题<最长回文子串>. 题意 给你一个字符串 s,找到 s 中最长的回文子串. 示例 输入:s = & ...

  7. LeetCode算法题-Number Complement(Java实现-五种解法)

    这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...

  8. LeetCode算法题-Longest Palindrome(五种解法)

    这是悦乐书的第220次更新,第232篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第87题(顺位题号是409).给定一个由小写或大写字母组成的字符串,找到可以用这些字母构 ...

  9. LeetCode算法题-Find the Difference(Java实现-五种解法)

    这是悦乐书的第214次更新,第227篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第82题(顺位题号是389).给定两个字符串s和t,它们只包含小写字母.字符串t由随机混 ...

随机推荐

  1. Linux指令--watch,at

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  2. Inner Join and Left Join 与条件的结合

    在使用关系数据库时,表连接和对结果集的筛选是必不可少的查询技能,对于他们的用法你都搞清楚了么?请让我们一起来过一遍. 表创建与初始化: Inner Join 结果集: 对于Inner Join, 条件 ...

  3. Asp.net core 2.0.1 Razor 的使用学习笔记(三)

    ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(二)—用户账户及cookie配置 修改用户账户及cookie配置 一.修改密码强度和用户邮箱验证规则 ...

  4. Makefile 的使用

    第一篇: OBJS = ./persion.o all : persion @echo "version 03" persion : $(OBJS) g++ -o $@ $^ ./ ...

  5. C++——函数重载

    C++允许功能相近的函数在相同的作用域内以相同函数名声明,从而形成重载,方便使用,便于记忆. /*形参类型不同*/ int add(int x,int y); float add(float x,fl ...

  6. JAVA并发编程学习笔记------线程的三种创建方式

    创建线程一般有如下几个方式: 1. 通过继承Thread类来创建一个线程: /** * 步骤1:定义一个继承Thread类的子类 * 步骤2:构造子类的一个对象 * 步骤3:启动线程: * */ pu ...

  7. Effective Java 之 --- 用私有构造器或者枚举类型强化Singleton属性

    Singleton指仅仅被实例化一次的类,通常用来代表那些本质上唯一的系统组件,实现Singleton有三种方法: 1)公有静态成员是个final域,享有特权的用户可以调用AccessibleObje ...

  8. Core Animation 文档翻译 (第八篇)—提高动画的性能

    前言 核心动画是提高基于APP动画帧率的好方式,但是核心动画的使用不代表性能的提升的保证.尤其在OSX,当使用核心动画时,我们仍需选择最有效的方式.和所有的性能相关的问题一样,我们应该使用工具时时的评 ...

  9. 豆瓣爬虫小记(lowB版)

    爬虫小记 学习玩python正则之后,想利用正则知识学学网络爬虫. 需求分析 按照自己平时浏览的网页,留意下哪个网页的信息对自己有价值,分析要爬取哪些网页信息.这里我先爬取简单的静态网页,豆瓣网经典电 ...

  10. Java设计模式——代理模式

    public interface People { public void work(); } public class RealPeople implements People { public v ...