两个数的和

public class Solution_1 {
	
	// 蛮力法
	 public int[] twoSum_1(int[] nums, int target) {
		 int[] result = {-1, -1};
		 
		 // 从0位置去每一个数来和后面的是相加
		 for(int i=0; ii; j--){
				 if(nums[i]+nums[j]==target){
					 result[0] = i;
					 result[1] = j;
				 }
			 }
		 }
		 
	     return result;
	 }
	 
	 // 通过map来降低复杂度
	 public int[] twoSum(int[] nums, int target) {
		 int[] result = {-1, -1};
		 Map map = new HashMap<>();
		 
		 for(int i=0; i

1、ArrayList 有序集合底层为数组按下标查找快 增删慢 按元素查找、增删都慢。

2、LinkedList 有序集合底层为链表按下标查找慢 增删快 按元素查找慢 增删比arrayList快。

3、HashMap 无序哈希表 底层哈希表 按下标查找一般比LinkedList快 增删快跟主体大小有关。

      按元素查找快 增删快跟主体大小有关,越大越慢。

所以在第二种方法中使用中选择存放在HashMap中。

你可能感兴趣的:(java练习题)