【Leetcode】1. 兩數之和

給定一個整數數組 nums?和一個目標值 target,請你在該數組中找出和為目標值的那?兩個?整數,并返回他們的數組下標。

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

示例:

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

因為 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

解題代碼:

class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int, int> mp;for(int i = 0; i < nums.size(); ++i){if(mp.find(target - nums[i]) != mp.end())return {i, mp[target - nums[i]]};elsemp[nums[i]] = i;}return {};     }
};

?

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

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

相關文章

read和write函數的使用

都需要包含頭文件&#xff1a; <unistd.h> read系統函數從打開的設備或文件中讀取數據&#xff0c;即將數據從外設上經過內核讀到用戶空間&#xff1b;write系統函數相反&#xff0c;向打開的設備或文件中寫入數據&#xff0c;即將數據從用戶空間&#xff08;I/O緩沖&am…

1091. Acute Stroke (30)

One important factor to identify acute stroke (急性腦卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core. Input Speci…

lseek函數的使用

需要包含頭文件&#xff1a;<sys/types.h> <unistd.h> off_t lseek(int fd, off_t offset, int whence)&#xff1b; 函數原型 函數功能&#xff1a;移動文件讀寫指針&#xff1b;獲取文件長度&#xff1b;拓展文件空間。 在使用該函數之前需要將文件打開&…

19. 刪除鏈表的倒數第N個節點

給定一個鏈表&#xff0c;刪除鏈表的倒數第 n 個節點&#xff0c;并且返回鏈表的頭結點。 示例&#xff1a; 給定一個鏈表: 1->2->3->4->5, 和 n 2. 當刪除了倒數第二個節點后&#xff0c;鏈表變為 1->2->3->5. 說明&#xff1a; 給定的 n 保證是有效的。…

文件操作相關的系統函數

重點學習&#xff1a;stat&#xff08;fstat、lstat 獲取文件屬性&#xff09;、access&#xff08;測試指定文件是否擁有某種權限&#xff09;、chmod&#xff08;改變文件的權限&#xff09;、chown&#xff08;改變文件的所屬主和所屬組&#xff09;、truncate&#xff08;截…

stat函數(stat、fstat、lstat)

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> //需包含頭文件 有如下三個函數的函數原型&#xff1a; int stat(const char *path, struct stat *buf); 第一個形參&#xff1a;指出文件&#xff08;文件路徑&#xff09;&…

1062. Talent and Virtue (25)

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about peoples talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage&#xff08;圣人&#xff09;"…

access、strtol函數的使用(后者為C庫函數)

#include <unistd.h> int access(const char *pathname, int mode); 作用&#xff1a;檢查調用該函數的進程是否可以對指定的文件執行某種操作。 第一個形參&#xff1a;文件名&#xff1b;第二個形參&#xff1a;R_OK&#xff08;是否可讀&#xff09;、W_OK&#xf…

chmod、chown函數的使用

#include <sys/stat.h> int chmod(const char *path, mode_t mode); int fchmod(int fd, mode_t mode); 作用&#xff1a;改變指定文件的權限。第二個參數&#xff1a;mode必須為一個8進制數&#xff1b;返回值為0表示成功&#xff0c;-1表示失敗。 //代碼 #include…

606. 根據二叉樹創建字符串

你需要采用前序遍歷的方式&#xff0c;將一個二叉樹轉換成一個由括號和整數組成的字符串。 空節點則用一對空括號 "()" 表示。而且你需要省略所有不影響字符串與原始二叉樹之間的一對一映射關系的空括號對。 示例 1: 輸入: 二叉樹: [1,2,3,4] 1 / \ …

truncate、rename函數的使用

#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); 作用&#xff1a;用于拓展或截斷文件。將參數path 指定的文件大小改為參數length 指定的大小。如果原來的文件大小比參數le…

【Leetcode】112. 路徑總和

給定一個二叉樹和一個目標和&#xff0c;判斷該樹中是否存在根節點到葉子節點的路徑&#xff0c;這條路徑上所有節點值相加等于目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹&#xff0c;以及目標和 sum 22&#xff0c; 5 / \ …

link、symlink、readlink、unlink函數的使用

#include <unistd.h> int link(const char *oldpath, const char *newpath); 作用&#xff1a;創建一個硬鏈接 0成功 -1 失敗 //代碼 #include <stdio.h> #include <stdlib.h> #include <unistd.h>int main(int argc, char* argv[]) {if(ar…

【Leetcode】113. 路徑總和 II

給定一個二叉樹和一個目標和&#xff0c;找到所有從根節點到葉子節點路徑總和等于給定目標和的路徑。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹&#xff0c;以及目標和 sum 22&#xff0c; 5 / \ 4 8 / / \ …

目錄操作相關的系統函數

主要介紹幾個常用函數的使用方法&#xff1a;chdir&#xff08;改變進程的當前工作目錄&#xff09;、getcwd&#xff08;獲取當前進程的工作目錄&#xff09;、mkdir&#xff08;創建目錄&#xff09;、rmdir&#xff08;刪除空目錄&#xff09;、opendir&#xff08;打開一個…

1079. Total Sales of Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;經銷商&#xff09;, and suppliers&#xff08;供應商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

chdir、getcwd、mkdir、rmdir函數

#include <unistd.h> int chdir(const char *path); int fchdir(int fd); 作用&#xff1a;改變調用這一函數的進程&#xff08;即程序執行&#xff09;的當前工作目錄&#xff0c;注意不是shell的當前工作目錄。 返回值&#xff1a;0成功 -1失敗 #include <unis…

【Leetcode | 235】 235. 二叉搜索樹的最近公共祖先

給定一個二叉搜索樹, 找到該樹中兩個指定節點的最近公共祖先。 百度百科中最近公共祖先的定義為&#xff1a;“對于有根樹 T 的兩個結點 p、q&#xff0c;最近公共祖先表示為一個結點 x&#xff0c;滿足 x 是 p、q 的祖先且 x 的深度盡可能大&#xff08;一個節點也可以是它自己…

1090. Highest Price in Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;經銷商&#xff09;, and suppliers&#xff08;供應商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

opendir、readdir和closedir函數

注意&#xff1a;在Linux中&#xff0c;目錄的輸入格式&#xff1a;/mnt//fghs、/mnt/fghs、/mnt/fghs和/mnt/fghs//是等效的&#xff0c;都一樣。 #include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd); 返回…