大学学了一堆理论,编码水平渣。果然应该刷刷题了。
正好最近看java ,试着就java写一写。
水平略渣。。
题目:
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input:numbers={2, 7, 11, 15}, target=9
Output:index1=1, index2=2
代码:1
public static int[] twoSum(int[] numbers, int target) {
int[] temp = (int[]) numbers.clone();
Arrays.sort(numbers);
int []index = new int[2];
int length = numbers.length;
int left = 0;
int right = length-1;
int sum = 0;
int tt = 0;
int pp = 0;
while(left < right)
{
sum = numbers[left]+numbers[right];
if(sum == target)
{
for(int i = 0;i<length;i++)
{
if(temp[i] == numbers[left]&&tt==0)
{
index[0] = i+1;
tt = 1;
}
else if(temp[i] == numbers[right]&&pp==0)
{
index[1] = i+1;
pp = 1;
}
if(pp==1&&tt==1)
{
if(index[0]>index[1])
{
int t = index[0];
index[0] = index[1];
index[1] = t;
return index;
}
}
}
return index;
}
else if(sum < target)
{
left++;
}
else
right--;
}
return index;
}
public static void main(String[] args) {
int []numbers ={-1,-2,-3,-4,-5};
int []index = new int[2];
index = twoSum(numbers,-8);
System.out.print(index[0]+"\t\t\t\t"+index[1]);
}
代码:2
public class hello {
public static int[] twoSum(int[] numbers, int target) {
int[] temp = (int[]) numbers.clone();
Arrays.sort(numbers);
int []index = new int[2];
int length = numbers.length;
int left = 0;
int right = length-1;
int sum = 0;
// int tt = 0;
// int pp = 0;
ArrayList<Integer> in = new ArrayList();
while(left < right)
{
sum = numbers[left]+numbers[right];
if(sum == target)
{
for(int i = 0;i<length;i++)
{
if(temp[i] == numbers[left])
{
in.add(i+1);
}
else if(temp[i] == numbers[right])
{
in.add(i+1);
}
if(in.size()==2)
{
break;
}
}
break;
}
else if(sum < target)
{
left++;
}
else
right--;
}
index[0] = in.get(0);
index[1] = in.get(1);
return index;
}
public static void main(String[] args) {
int []numbers ={-1,-2,-3,-4,-5};
int []index = new int[2];
index = twoSum(numbers,-8);
System.out.print(index[0]+"\t\t\t\t"+index[1]);
}
}
分享到:
相关推荐
Leetcode two sum java 解法
leetcode 2 leetcode-训练 算法训练。 java $: javac hello.java java ...java ...leetcode ...leetcode ...leetcode ...1.two-sum.cpp leetcode test 1.two-sum.cpp leetcode test 1.two-sum.cpp -t '[1,2,3]\n3'
leetcode-1-Two-Sum 这是我在 Github 的第一天。 我只是 AC leetcode 中的第一个问题。 从现在开始,我将使用Github在leetcode中记录我的生活。 我擅长 C++,但对 java 和 Python 不是很专业,我将使用最流行的 3 种...
java代码-1. 两数之和 [简单] https://leetcode-cn.com/problems/two-sum
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number 题号表示 LeetCode 面试题。 :receipt: 目录 统计 ...
leetcode题目的java解决方案还有一些自用的练习,个人练习用,也可以拿来参考:) 缓慢更新 尽量每道题进行翻译,给出多种解决方法,加上思路的注释 代码也会尽量修改成易于理解的代码 更新记录 2017-09-10 更新了01.Two ...
twoSum.java | | | | |-- twoSum.kt | | | | |-- twoSum.md | |-- ...... 非常欢迎讨论和代码审查。 TwoSum.java在 Java 中提供了 OJ 接受的解决方案。 TwoSum.kt在 Kotlin 中提供 OJ 接受的解决方案 TwoSum.md提供...
TwoSum test only gradle -Dtest.single=TestTwoSum test # run TwoSum test only with some informnation gradle -Dtest.single=TestTwoSum test --info 问题清单 问题 笔记 缩写 缩写用于命名标识符。 缩写 意义 ...
leetcode 分类 Leetcode Introduction 本文旨在将leetcode的题型分类 Pattern: Sliding window 滑动窗口类型的题目经常是用来执行数组或是链表上某个区间(窗口)上的操作。比如找最长的全为1的子数组长度。滑动窗口...
leetcode 答案 leetcode日记 ...twoSum(int[] nums, int target) { Map map = new HashMap<>(); for(int i = 0; i < nums.length; i++) { int a = target - nums[i]; if (map.containsKey(a)) { re
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. ...
LeetCode/1.TwoSum/two_sum.py 请继续受到启发。 尝试考虑算法的每种可能实现的复杂性。 请注意,消耗的时间和内存来自: 或者通常,中文网站会提供更糟糕的时间以及更糟糕的内存消耗。 但百分位排名可以高于美国...
1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best ...
leetcode 答案 leetcode,sorting... # Title Solution 1 Java 2 Java 3 Java 4 Java 5 Java 1.两数之和 题目描述 ...nums[1] ...1] ...,空间复杂度O(1) ...twoSum(int[] nums, int target) { int[] numIndex=new
(./Java/Two Sum.java) 2 两个数相加 中等的 [C++] (./C++/Add Two Numbers.cpp), [Java] (./Java/Add Two Numbers.java) 3 无重复字符的最长子串 中等的 [C++] (./C++/最长子串无重复字符.cpp), [Java] (./Java/...
Leetcode-Java Use Java to solve Leetcode&JianZhiOffer problems. Leetcode Num Title Difficulty Tag 1 Two Sum (两数之和) Easy Hash Table 2 Add Two Numbers (两数相加) Medium Linked List 3 Longest ...
java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 双指针 编号 题目 LeetCode 11 Container With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode...
#Leetcode-In-Java 代码并不全是本人写的,有的参考了网络上其他前辈的想法,但都能在OJ上AC。 ###索引 1 . Two-Sum 要点: - 利用java中Array对象的sort方法排序,使得整个数组呈升序状态 - 再利用两段取点...