[Algorithm learning] Given an integer array, find two integers for the specified integer and (3)

Source: Internet
Author: User

Problem Description:
Design a class that contains the following two member functions:
Save (int input) inserts an integer into an integer set.
Test (int target) checks if there are two numbers and is the input value. Returns true if there are two numbers, otherwise false
An element that allows the same value in an integer collection

Analysis:
and [algorithm learning] Given an integer array, find out two integers for the specified integer and (2) different, here need to figure out that there is no existence of these two numbers, you can modify the data structure on the basis of the previous article, HashMap where key is the value, value is the number of values, Then you need to make a two-step decision, twice times the number of numbers in the map is equal to the target number, which requires value=2 to return true

Rationale for a code of thought

(1). Write save (int input). This is simple, just to determine if there is input as key, there is value+1, no value=1. The code is as follows:

  
 
  1. public void Save(int input)
  2. {
  3. int count = 0;
  4. if (map.containsKey(input))
  5. {
  6. count = map.get(input);
  7. }
  8. map.put(input, count + 1);
  9. }

(2). Checks if there are two numbers and is the input value. The above analysis has been said to be similar, here directly affixed to the code. The code is as follows:

  
 
  1. public boolean Test(int target)
  2. {
  3. Iterator<Integer> iterator = map.keySet().iterator();
  4. while (iterator.hasNext())
  5. {
  6. int one = iterator.next();
  7. int two = target - one;
  8. System.out.println("one:"+one+" two:"+two);
  9. if (map.containsKey(two))
  10. {
  11. // two<<1等价于two*2
  12. if (!(target ==two<<1 && map.get(two) == 1))
  13. {
  14. return true;
  15. }
  16. }
  17. }
  18. return false;
  19. }
The integration code ends up as follows
  
 
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. public class TwoNumOfSum3
  4. {
  5. // key:数值,value:数值对应的个数
  6. HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  7. /**
  8. * 插入一个整数到一个整数集合里
  9. * @param input
  10. */
  11. public void Save(int input)
  12. {
  13. int count = 0;
  14. if (map.containsKey(input))
  15. {
  16. count = map.get(input);
  17. }
  18. map.put(input, count + 1);
  19. }
  20. /**
  21. * 检查是否存在两个数和为输入值
  22. * @param target
  23. * @return 如果存在着两个数,则返回true,否则返回false
  24. */
  25. public boolean Test(int target)
  26. {
  27. Iterator<Integer> iterator = map.keySet().iterator();
  28. while (iterator.hasNext())
  29. {
  30. int one = iterator.next();
  31. int two = target - one;
  32. System.out.println("one:"+one+" two:"+two);
  33. if (map.containsKey(two))
  34. {
  35. if (!(target ==two<<1 && map.get(two) == 1))
  36. {
  37. return true;
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43. /**
  44. * @param args
  45. */
  46. public static void main(String[] args)
  47. {
  48. TwoNumOfSum3 t=new TwoNumOfSum3();
  49. t.Save(5);
  50. t.Save(10);
  51. t.Save(4);
  52. t.Save(7);
  53. System.out.println(t.Test(12));
  54. }
  55. }


From for notes (Wiz)

[Algorithm learning] Given an integer array, find two integers for the specified integer and (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.