SHARE
TWEET

Untitled

a guest May 20th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //1-Java
  2. //2- String not bigger than integer, numbers not bigger than integer
  3. //3- Check char by char if number, if number add to string, when encounter not number while string size > 0 convert String
  4. // to Integer add to list, sort list in the end
  5. //5- O(N^2) we have a second loop in the loop in form of returnList.contains()
  6. //6- Integer to check if value already exist in list, StringBuilder to create number String, List to return
  7. class ExtractNumbers {
  8.   public static List<Integer> getNumbers(String text) {
  9.     List<Integer> returnList = new ArrayList<Integer>();
  10.     StringBuilder numberStr = new StringBuilder();
  11.     for(char c:text.toCharArray()){
  12.         if(Character.isDigit(c){
  13.             numberStr.append(c);
  14.         }else if(number.size()>0){
  15.             Integer number = new Integer(numberStr.toString());
  16.             if(!returnList.contains(number)){
  17.                 returnList.add(number);
  18.             }
  19.             numberStr.setLength(0);
  20.         }
  21.     }
  22.     Collections.sort(returnList);
  23.     return returnList;
  24.   }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top