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由随机混 ...
随机推荐
- cookie sessionStorage localStorage 之间的关系
先说一个cookie 因为HTTP是无状态的 所以cookie诞生 用于保存会话信息 大小 4096b 一般在4095b以内 数量限制 20 -50 根据浏览器不同 操作的是一个字符串 可以设置参数 ...
- JavaScript之图片懒加载的实现
图片懒加载指的是在浏览过程中随着需要才被加载出来,例如某宝上面浏览商品时,会伴随很多的图片,如果一次全部加载出来的话,显然资源有些浪费,并且加载速度也会相对降低,那么懒加载的实现很重要.即随着浏览翻阅 ...
- redis五大类型用法
Redis五大类型:字符串(String).哈希/散列/字典(Hash).列表(List).集合(Set).有序集合(sorted set)五种Controller:@Resource RedisTe ...
- elasticsearch的集中常见操作
1.引入dependency <dependency> <groupId>org.springframework.data</groupId> <artifa ...
- mybatis传参的几种方式
1,@Param @参考文章 @Select("select s_id id,s_name name,class_id classid from student where s_name= ...
- jdk源码->集合->HashSet
类的属性 public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, ...
- android adapter 中添加OnClickListener事件
public class SearchAutoAdapter extends BaseAdapter { private OnClickListener mOnClickListener; publi ...
- HTML5入门要点
要点 HTML5是HTML的最新版本.通过引入心的标签.新的语义和媒体元素,同时要依赖一组支持Web应用的JavaScript库 XHTML不再是Web页面开发标准.开发人员和W3C组织觉决定还是继续 ...
- Android开发之漫漫长途 Fragment番外篇——TabLayout+ViewPager+Fragment
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Python基础篇(八)
key words:私有变量,类静态变量,生成器,导入Python模块,r查看模块可以使用的函数,查看帮助信息,启动外部程序,集合,堆,时间模块,random模块,shelve模块,文件读取等 > ...