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. JVM-垃圾收集的过程

    JDK1.7 JVM的垃圾收集算法有 1. 标记-清除算法: 2. 复制算法:在商业虚拟机都是使用这种算法来回收新生代的 3. 标记-整理算法: JDK1.7 JVM的垃圾收集器有 1. Serial ...

  2. [转]CentOS Apache 性能调试!

    查看Apache的并发请求数及其TCP连接状态: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 返回结果示例 ...

  3. linux_通配符

    通配符和正则表达式区别? 通配符用在用户命令行bash环境,而正则表达式用于linux三剑客(awk, sed, grep) 那,有哪些通配符? * 所有字符    五星 ls *.txt # 列举目 ...

  4. python2.7.5 安装pip

    1 先安装setuptools 下载地址:https://pypi.python.org/pypi/setuptools#downloads 将下载后的tar文件解压,用CMD模式进入到解压后的文件所 ...

  5. Spark 读写hive 表

    spark 读写hive表主要是通过sparkssSession 读表的时候,很简单,直接像写sql一样sparkSession.sql("select * from xx") 就 ...

  6. 我是这么配置mariadb的。 为了能够操作汉字数据~

    为了能够操作汉字数据- 以下是步骤: 1. 找到my.cnf /etc/my.cnf 2. 打开它,在[client]和[mysql]下输入以下指令 default-character-set=utf ...

  7. sap中Excel的模版上传和下载

    一:事物码smw0 二:上传步骤 注:"包"为项目的包的名称. 三:下载代码 l_filename = 'XX.xls'. l_muban = 'z123'. *&---下 ...

  8. 安装RRDtool 1.4.5

    安装rrdtoolRrdtool安装需要cairo.libxml2.pango库支持,可通过yum安装安装libart_lgpl-devel这个包yum -y install libart_lgpl- ...

  9. BZOJ 1192: [HNOI2006]鬼谷子的钱袋 [娱乐]

    题意: n个数分组,使得小于n的每个数都能表示出来,最少几组 就是“最优集合”的超级弱化版.....每次+=now+1 然后一个貌似科学的方法是n二进制拆分 #include <iostream ...

  10. [Python Study Notes] 抉择--Python2.x Or Python 3.x

    In summary : Python 2.x is legacy, Python 3.x is the present and future of the language Python 3.0 w ...