三数之和_

3.给你一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c,使得a+b+c=0?请你找出所有和为0 且不重复的三元组,

        注意: 答案中不可以包含重复的三元组

 

 

 

 

 

2.无重复字符的第一个子串

示例 1:

输入: s = "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

提示:

  • 0 <= s.length <= 5 * 104
  • s 由英文字母、数字、符号和空格组成
package com.leige.week01;

public class Day01 {
	public static void main(String[] args) {
		//0 <= s.length <= 5 * 104
		String s = "abcdabcbb";
		int i = s.length();
		char[] c = new char[i];
		for (int j = 0; j < i; j++) {
			char charAt = s.charAt(j);
			for (char d : c) {
				if (d == charAt) {
					return;
				}
			}
			System.out.println(charAt);
			c[j] = charAt;	
		}
		for (char d : c) {
			System.out.println(d);
		}
	}
}


/**
		 * Given nums = [2, 7, 11, 15], target = 9,
			
			Because nums[0] + nums[1] = 2 + 7 = 9,
			return [0, 1]

		 
		int[] nums = new int[] {2, 7, 11, 15};
		int tag = -87;
		int[] test = test(nums, tag);
		if (test != null) {
			for (int i : test) {
				System.out.print(i + " ");
			}
		}else {
			System.out.println("没有该数据");
		}
		
	}
	
	public static int[] test(int[] nums , int x) {
		int[] tag = new int[2];
		for (int i = 0; i < nums.length - 1; i++) {
			for (int j = i + 1; j < nums.length; j++) {
				if ((nums[i] + nums[j]) == x) {
					tag[0] = i;
					tag[1] = j;
					return tag;
				}
			}
		}
		return null;
*/

1.Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

给定一个整数数组,返回两个数字的索引,使它们相加到一个特定的目标。
您可以假设每个输入将恰好有一个解决方案,并且您可能不会两次使用同一个元素。

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1]

 题目大致意思:

        给定一个数组,和一个目标数字,在数组中找两个数字,使得两个数字的和等于目标数字,最后返回两个数字的下标

解决:

        1.取出数组中的任意一个数字

        2.取出数组中任意一个数字,但是不包含第一次的数字

        3.将两次的数据相加

        4.判断是否与目标数字相等

        5.将下标放入新的数组中,用来存储下标的数组,大小为二即可

        

package com.leige.week01;

public class Day01 {
	public static void main(String[] args) {
		int[] nums = new int[] {2, 7, 11, 15};
		int tag = -87;
		int[] test = test(nums, tag);
		if (test != null) {
			for (int i : test) {
				System.out.print(i + " ");
			}
		}else {
			System.out.println("没有该数据");
		}
		
	}
	
	public static int[] test(int[] nums , int x) {
		int[] tag = new int[2];
		for (int i = 0; i < nums.length - 1; i++) {
			for (int j = i + 1; j < nums.length; j++) {
				if ((nums[i] + nums[j]) == x) {
					tag[0] = i;
					tag[1] = j;
					return tag;
				}
			}
		}
		return null;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值