1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加

/*
哇,LeetCode的第一题...啧啧
*/
public int [] twoSum(int[] nums, int target) {
/*
两个数配合组成target
建立值和下标的映射,遍历数组时一边判断是否有另一半,一边添加新映射到哈希表
*/
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int a = target-nums[i];
if (map.containsKey(a))
return new int[]{i,map.get(a)};
map.put(nums[i],i);
}
return null;
}

2.顺序数组,双指针分别靠向中间

public int[] twoSum(int[] num, int target) {
/*
two sum第二题,排序好的数组,双指针
*/
int left = 0,right = num.length-1;
while (left<right)
{
int a = num[left] + num[right];
if (a == target)
return new int[]{left+1,right+1};
if (a > target)
right--;
else left++;
}
return null;
}

3.BST,中序遍历后中上一题的做法做

public boolean findTarget(TreeNode root, int k) {
/*
中序遍历得到排序数组,然后就是two sum2的做法
*/
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty()||root!=null)
{
if (root!=null)
{
stack.push(root);
root = root.left;
}
else {
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
//下面开始挑选
int l = 0,r = list.size()-1;
while (l < r)
{
int a = list.get(l) + list.get(r);
if (a==k) return true;
if (a>k) r--;
else l++;
}
return false;
}

[leetcode]TwoSum系列问题的更多相关文章

  1. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

  2. LeetCode——single-number系列

    LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...

  3. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  4. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  5. [Leetcode] Sum 系列

    Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...

  6. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  7. leetCode:twoSum 两数之和 【JAVA实现】

    LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...

  8. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  9. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

    题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

随机推荐

  1. Guava中EventBus分析

    EventBus 1. 什么是EventBus 总线(Bus)一般指计算机各种功能部件之间传送信息的公共通信干线,而EventBus则是事件源(publisher)向订阅方(subscriber)发送 ...

  2. day2(APlview+Serializers使用)

    1.APIview使用 ModelVIewSet 是对 APIView 封装  ModelSerializer是对Serializeer 1.1 在user/urls.py中添加路由 urlpatte ...

  3. 第二十二章、 Model/View便利类树型部件QTreeWidget

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 树部件(Tree Widget)是Qt Designer中 Item Widgets(It ...

  4. 第15.25节 PyQt(Python+Qt)入门学习:Model/View开发实战--使用QTableView展示Excel文件内容

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在前面的订阅专栏<第十九章.Model/View开发:QTableView的功能及属 ...

  5. PyQt(Python+Qt)学习随笔:Qt Designer中部件的mouseTracking和tabletTracking属性

    在Qt Designer中的部件属性设置中,有mouseTracking和tabletTracking两个属性,这两个属性是跟踪鼠标或平板触控笔的移动轨迹的. 一.mouseTracking mous ...

  6. PyQt学习随笔:QStandardItemModel使用注意事项

    老猿Python博文目录 老猿Python博客地址 在使用QStandardItemModel或其派生类作为view对象的数据存储时,有如下几点需要注意: 1.如果是多行多列的数据存储,对应视图如果没 ...

  7. 自动化运维工具之Puppet master/agent模型、站点清单和puppet多环境设定

    前文我们了解了puppe中模块的使用,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/14086315.html:今天我来了解下puppet的master/age ...

  8. 利用flask框架实现对用户的注册登录

    ------------------------------------(分割线)----------------------------------------------------------- ...

  9. 团队作业4-Day1

    团队作业4-Day1 1. 各个成员在 Alpha 阶段认领的任务 Alpha任务分配 人员 小程序样式实现 吴安冬+吴梓华 小程序js代码及云数据实现 庾艺锋+白军强 项目测试 王泽鑫+赵玮锋 2. ...

  10. v-clickoutsides

    //点击目标元素外侧触发特定事件 使用 v-clickoutsides="clickHandler" import Vue from 'vue' Vue.directive('cl ...