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;
    }
}

原文地址:https://www.cnblogs.com/guoyaohua/p/8215722.html

时间: 2024-07-28 16:02:24

LeetCode第五天的相关文章

LeetCode第五题,Longest Palindromic Substring

题目原文: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 题意解析: 最长回文子串.就是给定一个字符串S,找出其中的最长回文子串,并返回该子串. 解法: 第一种方法显然是循环暴力枚举,复杂度为O(

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

leetcode第五题--Longest Palindromic Substring

Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 找出最大的回文子串,回文就是指字符串倒过来和之前的一样.例如aba  倒过来还是aba.abac中的最大回文子串就是aba. 我一开始

LeetCode第五十一题-N皇后

N-Queens 问题简介:给定一个n*n的棋盘,求解如何将n个皇后放置在棋盘上, 保证其之间不互相攻击,即给定一个整数 n,返回所有不同的解决方案,用 ‘Q’ 和 ‘.’ 分别代表了皇后和空位 注:例如下图为n = 8 时的一个解 举例: 输入: 4 输出: [ [".Q…", // Solution 1 “…Q”, “Q…”, “…Q.”], ["…Q.", // Solution 2 “Q…”, “…Q”, “.Q…”] ] 解法一: 利用递归,首先从第一行元

LeetCode第五十二题-N皇后二(JAVA)

N-Queens II 问题简介:给定一个n*n的棋盘,求解如何将n个皇后放置在棋盘上, 保证其之间不互相攻击,即给定一个整数 n,返回所有不同的解决方案的个数 注:例如下图为n = 8 时的一个解 举例: 输入: 4 输出:2 解释: [ [".Q…", // Solution 1 “…Q”, “Q…”, “…Q.”], ["…Q.", // Solution 2 “Q…”, “…Q”, “.Q…”] ] 解法一: 利用递归,首先从第一行元素开始,逐行扫描判断,主

谈谈思维固化这件事

# 何为思维固化 我个人觉得狭义上来说,就是某个问题,你的想法只有一条思路,觉得别的思路不可行. 举个例子,就比如我,在做LeetCode第五题时候,要求一个字符串最长的回文子串.我的想法是先取出字符串,然后再判断是不是回文串.测试时,这种方法超时了. 我就想,如何加速判断回文串呢?想了若干种方法,尝试之后还是超时. 其实最快的方法就不该取出字符串,而是对每一个字符尝试才是最快的. 我思维固化的一点就在于,由于处理字符串处理的比较多,固化思维觉得字符串处理一定要取出一定规模的字符串做处理.实际上

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

这是悦乐书的第214次更新,第227篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第82题(顺位题号是389).给定两个字符串s和t,它们只包含小写字母.字符串t由随机混洗字符串s生成,然后在随机位置再添加一个字母.找到t中添加的字母.例如: 输入:s ="abcd", t ="abcde" 输出:'e' 说明:'e'是添加的字母. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Jav

LeetCode开心刷题五十一天——118. Pascal&#39;s Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal&#39;s Triangle II

118. Pascal's Triangle Easy 87984FavoriteShare Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1],

LeetCode每日一题(五):加一

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3]输出: [1,2,4]解释: 输入数组表示数字 123.示例 2: 输入: [4,3,2,1]输出: [4,3,2,2]解释: 输入数组表示数字 4321 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/plus-one 思路: