1. 程式人生 > >1. Two Sum(兩數之和) —— Java

1. Two Sum(兩數之和) —— Java

自己刷題,只是為了存下來,別吐槽

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0
] + nums[1] = 2 + 7 = 9, return [0, 1].

classSolution {
    public int[] twoSum(int[] nums, int target){
        int[] result = new int[2];
        for(int i=0;i<nums.length-1;i++){
            for(intj=i+1;j<nums.length;j++){
                if(nums[i] + nums[j] ==target){                                       
                        result[0] = i;
                        result[1] = j;                
                        break;                   
                } 
           }
        }
        return result;
    }
}


相關推薦

1. Two Sum之和 —— Java

自己刷題,只是為了存下來,別吐槽 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may

LeetCode 1. Two Sum 之和

ret desc rip twosum 關鍵點 earch pub ++ num Given an array of integers, return indices of the two numbers such that they add up to a specif

【LeetCode】1. Two Sum之和-C++實現的種方法

本題是一下公司的面試題: 問題描述:   問題求解: 使用無序容器unorder_map實現: #include <iostream> #include <vector> #include <cassert> #inclu

leetcode-1.two sum之和

前言: 元旦放假實在無聊,寫點程式碼娛樂自己吧,平時工作負責專案管理方面的東西相對多了一些,作為一個熱愛程式設計的人實在是手癢,趁著假期練練手,當然了,不是為了刷題,就是為了讓自己保持一個程式設計的狀態。個人覺得為了刷題而刷題會失去很多寫程式碼的樂趣,由於自己水平有限,如果程式碼有問題或者有什麼

1. Two Sum之和

給定一個整數數列,找出其中和為特定值的那兩個數。 你可以假設每個輸入都只會有一種答案,同樣的元素不能被重用。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因為

[Leetcode] 1Two sum之和題解

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would h

LeetCode1. Two Sum之和

Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exa

【LeetCode】- Two Sum相加

[ 問題: ] Given an array of integers, find two numbers such that they add up to a specific target nu

LeetCode刷題——第一題 之和

1.兩數之和 題目描述 思路 程式碼實現 可能出現的問題 題目描述 給定一個整數陣列 nums 和一個目標值 target,請你在該陣列中找出和為目標值的那 兩個 整數,並返回他們的陣列下標。 你可以假設每種輸入只會對應一

LeetCode: Two Sum 求解之和及雜湊演算法

=======題目描述======= 題目連結:https://leetcode.com/problems/two-sum/ 題目內容: Two Sum Given an array of integers, return indices of the two n

2. Add Two Numbers相加

給定兩個非空連結串列來代表兩個非負數,位數按照逆序方式儲存,它們的每個節點只儲存單個數字。將這兩數相加會返回一個新的連結串列。 你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。 示例:

每日一題--LeetCode 2相加java

題目描述: 程式碼如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x)

LeetCode 167. Two Sum II - Input array is sorted 之和之二 - 輸入的是有序數組

point find leetcode algorithm 個數 tar div solution runtime Given an array of integers that is already sorted in ascending order, find two

leetcode 1. Two Sum(之和

解題方案 思路 1 ******- 時間複雜度: O(N^2)******- 空間複雜度: O(1)****** 暴力解法,兩輪遍歷 beats 27.6% class Solution(object): def twoSum(self, nums, targe

LeetCode—1Two Sum雜湊表的使用

題目 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input wo

LeetCode第29題 Divide Two Integers相除

class Solution { public:     int divide(int dividend, int divisor) {         if(!divisor || (dividend == INT_MIN && divisor == -1)

【python3/c++】leetcode 1. Two Sum easy

1. Two Sum (easy) Given an array of integers, return indices of the two numbers such that they add up to a specific target. You m

【LeetCode】1. Two SumHash Table

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input

leetcode 刷題題解c++ 1.Two Sum hash表,排序+二分查詢

題目描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return i

Lettcode | Two Sum個數的和等於定值

題目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should ret