day3——两数之和
// 小白一名,0算法基础,艰难尝试算法题中,若您发现本文中错误,
或有其他见解,往不吝赐教,感激不尽,拜谢。
领扣 第2题 今日算法
题干
//给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
//
// 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
//
// 示例:
//
// 给定 nums = [2, 7, 11, 15], target = 9
//
// 因为 nums[0] + nums[1] = 2 + 7 = 9
// 所以返回 [0, 1]
初次尝试
/*
* 第一思路,仍是暴力解题
* 每个元素与除自身外的元素相加
* 何时与目标值相等,何时打破循环
* 不出意外 只超过了 17.2%的玩家
* 一组数据时间约为25ms
* */
public int[] twoSum1(int[] nums, int target) {
int first;
int second;
int a;
int b=1;
//外循环 假设第一个值为索引为a
out:
for (a = 0; a < nums.length; a++) {
first=nums[a];
//内循环 假设第二个值索引为b
for(b=a+1;b<nums.length;b++){
second=nums[b];
//判断时候相等 相等跳出所有循环
if(first+second==target){
break out;
}
}
}
//返回值
return new int[]{a,b};
}
初次尝试
/*
* 想了下 之前的代码约为n方次
* 一组数据两两组合且不重复为为n*(n-1)/2
* 按照获得此公式的逻辑
* 改动下了代码 因为代码本身很简单 便没有写注释
* 其实是头疼,太累了
* 本次用时约为 12ms 快了一倍
* */
public int[] twoSum2(int[] nums, int target) {
int first;
int second;
for(int a=1;a<nums.length;a++){
first= nums[a];
second=target-first;
for(int b=0;b<a;b++){
if(second==nums[b]){
return new int[]{a,b};
}
}
}
return null;
}
第二次尝试
/*
* 下面是 目前的最优代码之一
* 因为我认为这个网站的数据量并不够大
* 可能会存在误差
* 思路跟我的一样,但是使用了HashMap
* 使用是否包含代替了我的一次循环
* 不太明白为什么会比我快了这么多
* 应该是因为 HashMap的底层查找要优于循环吧
* 明天再看看吧
* */
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>(nums.length);
for(int i = 0; i < nums.length; i++){
if(map.containsKey(target - nums[i])){
return new int[]{i, map.get(target - nums[i])};
} else {
map.put(nums[i], i);
}
}
return null;
}
第三次尝试
今天学servlet用到了druid 才发现之前的连接池这块已经忘了好多
想要复习,可又想去学习概率论和算法
心情复杂 取舍难定
年轻时我觉得钱是最重要的,到老发现,的确如此。
When I was young,I used to think that money was the most important thing in life,now I am old,I know it is.
写于 2018.11.18
day3——两数之和的更多相关文章
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数
问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- Polynomial_0
@[注]两个一元多项式按照指数由大到小的顺序输入! #include <stdio.h> #define MAXSIZE 50 struct PolyNode { int coeffici ...
- Oracle 10046
[10046 SQL]conn username/passwordalter session set tracefile_identifier = 'id_10046';alter session s ...
- 个人对stm32ADC编程关键点的理解
平时在做项目或者参加比赛的过程中,个人觉得,有些东西写出来可能会帮助到新手少走弯路.(也很可能是错误的,欢迎大家纠错) 如果只是采集一路信号,直接用ADC独立模式,单通道就可以了. 如果需要同时采集多 ...
- face++静态库转为动态库之二
上一篇的时候,已经介绍了如何将carthage转为动态库.这一篇,我们是单纯的建一个动态库.还是以face++为例 查看上一篇: face++静态库转为动态库 制作动态库 1.创建一个工程MGLive ...
- java的冒泡排序
public interface Sorter{ public <T extends Comparable<T>> void sort(T[] list); //定义两个待排序 ...
- Redishelp
/** * @author yanming.zhang * @date 2019/1/25 21:15 */ @Component public class RedisHelp { @Autowire ...
- 能否显示pdf?
<iframe src='http://km.shengaitcm.com/ADC/_layouts/15/WopiFrame.aspx?sourcedoc=%2FADC%2FDocLib16% ...
- parcel 在js中导入 html 文件
parcel不支持将html文件导入为字符串,如果您对parcel使用熟练,直接使用 parcel-plugin-phtml 插件即可,此插件使用 .phtml 后缀 为什么用parcel? 因为从我 ...
- LR参数化取值规则总结
我想使用参数化输入设置10个并发用户循环1000次,第一个用户使用参数列表中的前1000个参数(第依次循环使用第一个参数.第二次循环使用第二个参数,依次类推).第二个用户使用参数列表中的2001-30 ...
- python locust 性能测试:HOOKS<钩子方法>
为locust中不同类型的事件,提供的钩子方法: from locust import TaskSet, task, events, Locust from locust.clients import ...