1. Ikki sonning yig'indisi
Topshiriq:
Berilgan butun sonlar qatoridan shunday ikki sonning indekslarini qaytaringki, ularning yig'indisi berilgan songa teng bo'lsin.
Har bir elementni faqat bir marta ishlata olasiz!
Misol:
#algoritm #problem #solution #leetcode #contest #interview
  
  Topshiriq:
Berilgan butun sonlar qatoridan shunday ikki sonning indekslarini qaytaringki, ularning yig'indisi berilgan songa teng bo'lsin.
Har bir elementni faqat bir marta ishlata olasiz!
Misol:
raqamlar_qatori = [2, 7, 11, 15], berilgan_son = 9,Yechim:
Chunki: raqamlar_qatori[0] + raqamlar_qatori[1] = 2 + 7 = 9,
Qaytadi: [0, 1].
class Solution:
... def twoSum(self, nums, target):
... """
... :type nums: List[int]
... :type target: int
... :rtype: List[int]
... """
... for index, current_value in enumerate(nums):
... searching_value = target - current_value
... try:
... searching_value_index = nums.index(searching_value, index + 1)
... if not searching_value_index <= 0 and searching_value_index <= len(nums):
... return [index, searching_value_index]
... except ValueError:
... continue
...
>>> print(Solution().twoSum([1, 3, 4], 7))
[1, 2]
@uzpythonlogs#algoritm #problem #solution #leetcode #contest #interview
LeetCode
  
  Two Sum - LeetCode
  Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not…
  You may assume that each input would have exactly one solution, and you may not…