当前位置:网站首页>【leetcode】day1
【leetcode】day1
2022-07-07 23:48:00 【Orange moon and a half meow】
Start to brush the questions !
List of articles
001 : Sum of two numbers
Given an array of integers nums And an integer target value target, Please find... In the array and For the target target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer . You can return the answers in any order .
For specific questions, see website
001. Violence solution ( double for loop )
001. Violence solution ( double for loop ) java edition
The time complexity of the solution is about O ( n 2 ) O(n^2) O(n2)
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result=new int[2];
for (int i=0;i<(nums.length-1);i++){
//java Medium length by nums.length
for (int j=i+1;j<nums.length;j++){
if ((nums[i]+nums[j])==target)
{
result[0]=i;
result[1]=j;
return result;
}
}
}
return result; // If only the above return It will cause some loops to have no return value , Result in an error
}
}
001. Violence solution ( double for loop ) pyhton edition
class Solution(object):
def twoSum(self, nums, target):
result=[];
for i in range(0,len(nums)): # Be careful python for loop if Back plus “:”
for j in range(i+1,len(nums)):
if ((nums[i]+nums[j])==target):
result.append(i);
result.append(j);
return result
001.hash solution
utilize hash Solve by table , The key lies in hash The lookup speed of the table is particularly fast . Besides python A dictionary of hash The table is similar
001.hashmap java edition
import java.util.HashMap;
import java.util.Map;
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
if(nums == null || nums.length == 0){
return result ;
}
Map<Integer, Integer> map = new HashMap<>(); // establish k-v , One to one hash table
// Be careful hash surface , The value of the array is used as key value , The subscript of the array is used as value value
//( Why? ? When looking for an array, we focus on the value of the array , Instead of Subscripts , We need to take value as “ guide ” To find the required array ).
// Don't worry about array values as key The value repeats , Because if you repeat , Cover can
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
// Judge hash Is there... In the table target - nums[i] This value , If there is a direct return
result [1] = i;
result [0] = map.get(temp);
return result;
}
// If it doesn't exist, it will Key value pairs are stored , In case you want to find ( These key value pairs have been determined 、 There is no required sum between any two numbers target Array of )
map.put(nums[i], i);
}
return result ;
}
001.dict pyhton
class Solution(object):
def twoSum(self, nums, target):
dict1={
};
for index,num in enumerate(nums):
temp=target-num;
if temp in dict1:
return [dict1[temp],index]; # Be careful With key visit Dictionary time Use square brackets !!!
dict1[num]=index;
return None;
边栏推荐
- One click installation with fishros in blue bridge ROS
- Display the server hard disk image to the browser through Servlet
- What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method
- archery安装测试
- Enumeration, simulation, and sorting
- 平衡二叉樹【AVL樹】——插入、删除
- @Detailed introduction of configuration annotation
- HDU - 1260 Tickets(线性DP)
- 2022.7.7-----leetcode.648
- SAP memory parameter tuning process
猜你喜欢
![P1067 [noip2009 popularity group] polynomial output (difficult, pit)](/img/1f/a798879a0d65eccefa339b288f2102.jpg)
P1067 [noip2009 popularity group] polynomial output (difficult, pit)

Take you hand in hand to build Eureka server with idea

C method question 1

10 schemes to ensure interface data security
![[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code](/img/55/ab50ead2564498cb214d98ac5b9c3d.jpg)
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code

保证接口数据安全的10种方案

Interface

0-1 knapsack problem

光流传感器初步测试:GL9306

AITM3.0005 烟雾毒性测试
随机推荐
【7.5】15. Sum of three numbers
Codeworks 5 questions per day (average 1500) - day 8
Magic fast power
postgres timestamp转人眼时间字符串或者毫秒值
平衡二叉樹【AVL樹】——插入、删除
数据分析系列 之3σ规则/依据拉依达准则来剔除异常值
Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
Dependency injection 2 advantage lifecycle
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
c—线性表
P1055 [noip2008 popularization group] ISBN number
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
Jisuan Ke - t3104
Flash encryption process and implementation of esp32
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
C - linear table
通达信买基金安全吗?
Get started with mongodb
One of the anti climbing methods
@Configuration注解的详细介绍