Letcode Java語言計算兩數之和

2021-09-23 10:23:10 字數 469 閱讀 2023

先看下問題:

給定乙個整數陣列 nums 和乙個目標值 target,請你在該陣列中找出和為目標值的那 兩個 整數,並返回他們的陣列下標。

你可以假設每種輸入只會對應乙個答案。但是,你不能重複利用這個陣列中同樣的元素。

示例:給定 nums = [2, 7, 11, 15], target = 9

因為 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]

這是乙個陣列根據值找對應索引的問題,解決這種題目的一種比較好的方式就是可以去將陣列的元素與對應的索引轉換為map集合,這樣就可以通過值找到其對應的索引了,此題較為簡單就不多做介紹就直接上**:

class solution 

map.put(nums[i],i);

}int arr=new int;

return arr;

} }

c語言 計算積分

程式執行結果示例 y1 1.333350 y2 1.151212 程式 include float fun1 float x float fun2 float x float integral float f float float a,float b int main 函式功能 計算函式1 x x...

C語言時間計算

1.clock函式 include include include intmain printf sum is d n s t end clock printf diff is f n double t end t begin printf total cost f s n double t end...

R語言計算moran I

install.packages maptools 畫地圖的包 install.packages spdep 空間統計,moran i install.packages tripack install.packages rann library maptools library spdep libr...