LeetCode第五天

leetcode 第五天

2018年1月6日

22.(566) Reshape the Matrix

1192699-20180106092453174-158033009.png
1192699-20180106092511768-1722004403.png

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

1192699-20180106100821628-1846082373.png

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==

1192699-20180106103722409-2105847597.png

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

1192699-20180106105515096-123981321.png

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==

==新知識點:動態規劃(有點難度)==
1192699-20180106120332971-613292929.png

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

1192699-20180106121417346-935617264.png

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

1192699-20180106122930331-1729052128.png

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()等操作==
1192699-20180106124145081-1906901766.png

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391531.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391531.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391531.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

matplotlib可視化_使用Matplotlib改善可視化設計的5個魔術技巧

matplotlib可視化It is impossible to know everything, no matter how much our experience has increased over the years, there are many things that remain hidden from us. This is normal, and maybe an exciting motivation to search and learn more. And I am sure …

adb 多點觸碰_無法觸及的神話

adb 多點觸碰On Twitter, in Slack, on Discord, in IRC, or wherever you hang out with other developers on the internet, you may have heard some formulation of the following statements:在Twitter上&#xff0c;在Slack中&#xff0c;在Discord中&#xff0c;在IRC…

robot:循環遍歷數據庫查詢結果是否滿足要求

使用list類型變量{}接收查詢結果&#xff0c;再for循環遍歷每行數據&#xff0c;取出需要比較的數值 轉載于:https://www.cnblogs.com/gcgc/p/11424114.html

leetcode 74. 搜索二維矩陣(二分)

編寫一個高效的算法來判斷 m x n 矩陣中&#xff0c;是否存在一個目標值。該矩陣具有如下特性&#xff1a; 每行中的整數從左到右按升序排列。 每行的第一個整數大于前一行的最后一個整數。 示例 1&#xff1a; 輸入&#xff1a;matrix [[1,3,5,7],[10,11,16,20],[23,30,34…

rm命令

命令 ‘rm’ &#xff08;remove&#xff09;&#xff1a;刪除一個目錄中的一個或多個文件或目錄&#xff0c;也可以將某個目錄及其下屬的所有文件及其子目錄均刪除掉 語法&#xff1a;rm&#xff08;選項&#xff09;&#xff08;參數&#xff09; 默認會提示‘是否’刪除&am…

javascript消除字符串兩邊空格的兩種方式,面向對象和函數式編程。python oop在調用時候的優點...

主要是javascript中消除字符串空格&#xff0c;比較兩種方式的不同 //面向對象&#xff0c;消除字符串兩邊空格 String.prototype.trim function() { return this.replace(/(^\s*)|(\s*$)/g, ""); };//去左右空格的函數; function trim(s){return s.replace(/(^\s*)…

如何使用Retrofit,OkHttp,Gson,Glide和Coroutines處理RESTful Web服務

Kriptofolio應用程序系列-第5部分 (Kriptofolio app series — Part 5) These days almost every Android app connects to internet to get/send data. You should definitely learn how to handle RESTful Web Services, as their correct implementation is the core knowle…

leetcode 90. 子集 II(回溯算法)

給你一個整數數組 nums &#xff0c;其中可能包含重復元素&#xff0c;請你返回該數組所有可能的子集&#xff08;冪集&#xff09;。 解集 不能 包含重復的子集。返回的解集中&#xff0c;子集可以按 任意順序 排列。 示例 1&#xff1a; 輸入&#xff1a;nums [1,2,2] 輸…

robot:linux下安裝robot環境

https://www.cnblogs.com/lgqboke/p/8252488.html 轉載于:https://www.cnblogs.com/gcgc/p/11425588.html

感知器 機器學習_機器學習感知器實現

感知器 機器學習In this post, we are going to have a look at a program written in Python3 using numpy. We will discuss the basics of what a perceptron is, what is the delta rule and how to use it to converge the learning of the perceptron.在本文中&#xff0…

JS解析格式化Json插件,Json和XML互相轉換插件

Json對象轉換為XML字符串插件 http://www.jsons.cn/Down/jquery.json2xml.js var xml_content $.json2xml(json_object);XML字符串轉換為Json對象插件 http://www.jsons.cn/Down/jquery.xml2json.js var json_obj $.xml2json(xml_content);json序列化和反序列化方法插件 …

Python之集合、解析式,生成器,函數

一 集合 1 集合定義&#xff1a; 1 如果花括號為空&#xff0c;則是字典類型2 定義一個空集合&#xff0c;使用set 加小括號使用B方式定義集合時&#xff0c;集合內部的數必須是可迭代對象&#xff0c;數值類型的不可以 其中的值必須是可迭代對象&#xff0c;其中的元素必須是可…

深度神經網絡課程總結_了解深度神經網絡如何工作(完整課程)

深度神經網絡課程總結Even if you are completely new to neural networks, this course from Brandon Rohrer will get you comfortable with the concepts and math behind them.即使您是神經網絡的新手&#xff0c;Brandon Rohrer的本課程也會使您熟悉其背后的概念和數學。 …

leetcode 1006. 笨階乘

通常&#xff0c;正整數 n 的階乘是所有小于或等于 n 的正整數的乘積。例如&#xff0c;factorial(10) 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1。 相反&#xff0c;我們設計了一個笨階乘 clumsy&#xff1a;在整數的遞減序列中&#xff0c;我們以一個固定順序的操作符序列來…

python:如何傳遞一個列表參數

轉載于:https://www.cnblogs.com/gcgc/p/11426356.html

curl的安裝與簡單使用

2019獨角獸企業重金招聘Python工程師標準>>> windows 篇&#xff1a; 安裝篇&#xff1a; 我的電腦版本是windows7,64位&#xff0c;對應的curl下載地址如下&#xff1a; https://curl.haxx.se/download.html 直接找到下面的這個版本&#xff1a; curl-7.57.0.tar.g…

gcc 編譯過程

gcc 編譯過程從 hello.c 到 hello(或 a.out)文件&#xff0c; 必須歷經 hello.i、 hello.s、 hello.o&#xff0c;最后才得到 hello(或a.out)文件&#xff0c;分別對應著預處理、編譯、匯編和鏈接 4 個步驟&#xff0c;整個過程如圖 10.5 所示。 這 4 步大致的工作內容如下&am…

虎牙直播電影一天收入_電影收入

虎牙直播電影一天收入“美國電影協會(MPAA)的首席執行官J. Valenti提到&#xff1a;“沒有人能告訴您電影在市場上的表現。 直到電影在黑暗的劇院里放映并且銀幕和觀眾之間都散發出火花。 (“The CEO of Motion Picture Association of America (MPAA) J. Valenti mentioned th…

郵箱如何秘密發送多個人郵件_如何發送秘密消息

郵箱如何秘密發送多個人郵件Cryptography is the science of using codes and ciphers to protect messages, at its most basic level. Encryption is encoding messages with the intent of only allowing the intended recipient to understand the meaning of the message.…

leetcode 面試題 17.21. 直方圖的水量(單調棧)

給定一個直方圖(也稱柱狀圖)&#xff0c;假設有人從上面源源不斷地倒水&#xff0c;最后直方圖能存多少水量?直方圖的寬度為 1。 上面是由數組 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的直方圖&#xff0c;在這種情況下&#xff0c;可以接 6 個單位的水&#xff08;藍色部分表示水&a…