LeetCode01:Two Sum Java实现

原题目:
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
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

 对于一个给定的整数数组和一个目标值,求出数组中两个数之和等于目标值,输出数组中这两个数的下标

算法分析:
          算法1.时间复杂度为O(n^2).
          采用两个指针i和j,i和j分别指向数组中前两个值,判断i和j所指值之和是否等于目标函数,若不等于,则向后移动i和j,直到找到两个数之和等于目标函数为止。输出i和j即可


遇到问题及解决方案:
1.没有考虑目标值是0的情况
          如果直接判断两个数相加的值是否等于目标值,则会忽略目标值为0,数组中也同样包含0的情况,所有需要先判断目标值是否为0。 如果目标值为0,则直接找出两个0并输出即可。
if(target == 0 && nums[i] == 0&&nums[j] == 0){        
                    tmp[0] = i;
                    tmp[1] = j;
                    return tmp;
                }  

LeetCode提交源码:

   
   
     
     
     
     
  1. public class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. if(nums == null )
  4. return null;
  5. int[] tmp = new int[2];
  6. // int i = 0;
  7. // int j = 0;
  8. for(int i = 0; i < nums.length; i++){
  9. for(int j = i+1; j < nums.length; j++){
  10. if(target == 0 && nums[i] == 0&&nums[j] == 0){
  11. tmp[0] = i;
  12. tmp[1] = j;
  13. return tmp;
  14. }
  15. if(nums[i] + nums[j] == target){
  16. tmp[0] = j;
  17. tmp[1] = i;
  18. }
  19. }
  20. }
  21. System.out.println(tmp[0] + " " + tmp[1]);
  22. return tmp;
  23. }
  24. }

Submission Details:

  LeetCode01:Two Sum Java实现_第1张图片
完整运行程序:
   
   
     
     
     
     
  1. package org.GuoGuoFighting.LeetCode1;
  2. import java.util.Scanner;
  3. class Solution1{
  4. public int[] twoSum(int[] nums,int target){
  5. if(nums == null )
  6. return null;
  7. int[] tmp = new int[2];
  8. // int i = 0;
  9. // int j = 0;
  10. for(int i = 0; i < nums.length; i++){
  11. for(int j = i+1; j < nums.length; j++){
  12. // if(nums[j] == target && nums[i] == target && i != j){ //如果输入的target包含在数组中,则返回这两个值
  13. if(target == 0 && nums[i] == 0&&nums[j] == 0){ //如果输入的target包含在数组中,则返回这两个值
  14. tmp[0] = i;
  15. tmp[1] = j;
  16. return tmp;
  17. }
  18. if(nums[i] + nums[j] == target){
  19. tmp[0] = i;
  20. tmp[1] = j;
  21. }
  22. }
  23. }
  24. System.out.println(tmp[0] + " " + tmp[1]);
  25. return tmp;
  26. }
  27. }
  28. public class TwoSum {
  29. public static void main(String[] args){
  30. Scanner scanner = new Scanner(System.in);
  31. System.out.println("请输入一个整形数组,用空格隔开:");
  32. String str = scanner.nextLine();
  33. System.out.println("请输入要找的目标k:");
  34. int k = scanner.nextInt();
  35. String[] tmp = str.split(" ");
  36. int[] arrays = new int[tmp.length];
  37. for(int i = 0; i < arrays.length;i++){
  38. arrays[i] = Integer.parseInt(tmp[i]);
  39. }
  40. scanner.close();
  41. Solution1 solution1 = new Solution1();
  42. int[] ansarray = solution1.twoSum(arrays, k); //将输出的数组打印
  43. System.out.println("这两个数是:" + ansarray[0] + "," + ansarray[1]);
  44. }
  45. }


程序运行结果:

        
  LeetCode01:Two Sum Java实现_第2张图片
LeetCode01:Two Sum Java实现_第3张图片
 
 
 

你可能感兴趣的:(LeetCode,leetcode,面试题,算法)