public List<List<String>> groupAnagrams(String[] strs) {
    List<List<String>> rst = new ArrayList<List<String>>();
    if (strs == null) {
      return rst;
    }

    List<String> order = new ArrayList<String>();
    HashMap<String, List<String>> map = new HashMap<String, List<String>>();
    for (String str : strs) {
      char[] arr = str.toCharArray();
      Arrays.sort(arr);
      String s = new String(arr);
      if (!map.containsKey(s)) {
        List<String> l = new ArrayList<String>();
        l.add(str);
        order.add(s);
        map.put(s, l);
      } else {
        map.get(s).add(str);
      }
    }
    Collections.sort(order);
    for (String str : order) {
      Collections.sort(map.get(str));
      rst.add(map.get(str));
    }
    return rst;
  }
  public void bfs(
      UndirectedGraphNode node,
      Map<UndirectedGraphNode, Boolean> visited,
      List<List<Integer>> result) { // BFS+queue

    List<Integer> row = new ArrayList<>();

    Queue<UndirectedGraphNode> queue = new LinkedList<>(); // 这个的写法 是神马意思啊  左边是队列右边是linkedlist = =
    visited.put(node, true);
    queue.offer(node);

    while (!queue.isEmpty()) {
      UndirectedGraphNode u = queue.poll();
      row.add(u.label);

      for (UndirectedGraphNode v : u.neighbors) {
        if (visited.get(v) == false) {
          visited.put(v, true);
          queue.offer(v);
        }
      }
    }

    Collections.sort(row);
    result.add(row);
  }
示例#3
0
 public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
   // Start typing your Java solution below
   // DO NOT write main() function
   Collections.sort(
       intervals,
       new Comparator<Interval>() {
         public int compare(Interval a, Interval b) {
           return new Integer(a.start).compareTo(new Integer(b.start));
         }
       });
   ArrayList<Interval> res = new ArrayList<Interval>();
   if (intervals.size() == 0) return res;
   Interval first = new Interval(intervals.get(0).start, intervals.get(0).end);
   if (intervals.size() == 1) {
     res.add(first);
     return res;
   }
   int i = 1;
   while (i < intervals.size()) {
     if (intervals.get(i).start > first.end) {
       res.add(first);
       first = intervals.get(i);
     } else if (intervals.get(i).end > first.end) {
       first.end = intervals.get(i).end;
     }
     i++;
   }
   res.add(first);
   return res;
 }
  public List<Interval> merge(List<Interval> intervals) {
    if (intervals.size() <= 1) return intervals;

    // Sort by ascending starting point using an anonymous Comparator
    Collections.sort(
        intervals,
        new Comparator<Interval>() {
          @Override
          public int compare(Interval i1, Interval i2) {
            return Integer.compare(i1.start, i2.start);
          }
        });

    List<Interval> result = new LinkedList<Interval>();
    int start = intervals.get(0).start;
    int end = intervals.get(0).end;

    for (Interval interval : intervals) {
      if (interval.start <= end) // Overlapping intervals, move the end if needed
      end = Math.max(end, interval.end);
      else { // Disjoint intervals, add the previous one and reset bounds
        result.add(new Interval(start, end));
        start = interval.start;
        end = interval.end;
      }
    }

    // Add the last interval
    result.add(new Interval(start, end));
    return result;
  }
  public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
    // Start typing your Java solution below
    // DO NOT write main() function

    ArrayList<Interval> res = new ArrayList<Interval>();
    if (intervals.size() == 0) return res;
    Collections.sort(intervals, new MyC());
    int start = 0;
    int end = 0;
    for (int i = 0; i < intervals.size(); i++) {
      if (i == 0) {
        start = intervals.get(i).start;
        end = intervals.get(i).end;
      } else {
        if (intervals.get(i).start <= end) {
          end = Math.max(end, intervals.get(i).end);
        } else {
          res.add(new Interval(start, end));
          start = intervals.get(i).start;
          end = intervals.get(i).end;
        }
      }
    }
    res.add(new Interval(start, end));
    return res;
  }
示例#6
0
 public ArrayList<ArrayList<Integer>> subsetsWithDup(ArrayList<Integer> S) {
   // write your code here
   ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
   if (S == null || S.size() == 0) return res;
   ArrayList<Integer> cur = new ArrayList<Integer>();
   Collections.sort(S);
   helper(res, cur, 0, S);
   return res;
 }
示例#7
0
 private void sort() {
   Collections.sort(
       array_,
       new Comparator /*<RubyValue>*/() {
         public int compare(Object /*RubyValue*/ arg0, Object /*RubyValue*/ arg1) {
           RubyValue v =
               RubyAPI.callPublicOneArgMethod(
                   (RubyValue) arg0, (RubyValue) arg1, null, RubyID.unequalID);
           return ((RubyFixnum) v).toInt();
         }
       });
 }
示例#8
0
  private void sort_with_block(final RubyBlock block) {
    final RubyArray self = this;
    Collections.sort(
        array_,
        new Comparator /*<RubyValue>*/() {

          public int compare(Object /*RubyValue*/ arg0, Object /*RubyValue*/ arg1) {
            // TODO can not check if block return/break occured.
            RubyValue v = block.invoke(self, (RubyValue) arg0, (RubyValue) arg1);
            return ((RubyFixnum) v).toInt();
          }
        });
  }
  public static void main(String[] args) {
    Class student = Student.class;
    Method[] methods = student.getDeclaredMethods();

    ArrayList<String> methodList = new ArrayList<>();
    for (Method method : methods) {
      methodList.add(method.getName());
    }
    Collections.sort(methodList);
    for (String name : methodList) {
      System.out.println(name);
    }
  }
示例#10
0
 public List<Interval> merge(List<Interval> intervals) {
   List<Interval> res = new ArrayList<>();
   Collections.sort(intervals, (Interval i1, Interval i2) -> i1.start - i2.start);
   Interval pre = null;
   for (Interval i : intervals) {
     if (res.isEmpty() || i.start > pre.end) {
       res.add(i);
       pre = i;
     } else {
       pre.end = Math.max(pre.end, i.end);
     }
   }
   return res;
 }
示例#11
0
  public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
    if (intervals == null || intervals.size() <= 1) return intervals;

    Collections.sort(intervals, new IntervalComparator());
    ArrayList<Interval> res = new ArrayList<Interval>();
    Interval last = intervals.get(0);
    for (int i = 1; i < intervals.size(); i++) {
      Interval curt = intervals.get(i);
      if (last.end < curt.start) {
        res.add(last);
        last = curt;
      } else {
        last.end = Math.max(last.end, curt.end);
      }
    }
    res.add(last);
    return res;
  }
示例#12
0
  // As an owner, I want the entry of an item to have minimal required
  // navigation in the user interface. It must be easy to describe an item.
  // http://stackoverflow.com/questions/18441846/how-to-sort-an-arraylist-in-java
  public void testSortAlpha() {
    ArrayList<GCard> gcards = new ArrayList<GCard>();
    GCard gcard1 =
        new GCard("Starbucks", 20, 1, 4, "Food & Drink", True, "Will get you half a coffee");
    gcards.add(gcard1);
    GCard gcard2 = new GCard("Cineplex", 50, 75, 5, "Entertainment", False, "I might keep these");
    gcards.add(gcard2);
    // <gcard1, gcard2>

    Collections.sort(
        gcards,
        new Comparator<GCard>() {
          @Override
          public int compare(GCard card1, GCard card2) {
            return card1.getName().compareTo(card2.getName());
          }
        });
    assertTrue((gcards.get(0) == gcard2) && (gcards.get(1) == gcard1));
    // <gcard2, gcard1>
  }
 public int minTotalDistance(int[][] grid) {
   List<Integer> ipos = new ArrayList<Integer>();
   List<Integer> jpos = new ArrayList<Integer>();
   for (int i = 0; i < grid.length; i++) {
     for (int j = 0; j < grid[0].length; j++) {
       if (grid[i][j] == 1) {
         ipos.add(i);
         jpos.add(j);
       }
     }
   }
   int sum = 0;
   for (Integer pos : ipos) {
     sum += Math.abs(pos - ipos.get(ipos.size() / 2));
   }
   Collections.sort(jpos);
   for (Integer pos : jpos) {
     sum += Math.abs(pos - jpos.get(jpos.size() / 2));
   }
   return sum;
 }
 public List<List<String>> groupStrings(String[] strings) {
   List<List<String>> res = new ArrayList<List<String>>();
   HashMap<String, List<String>> map = new HashMap<String, List<String>>();
   for (String s : strings) {
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < s.length(); i++) {
       int l = (s.charAt(i) - s.charAt(0) + 26) % 26;
       sb.append(l + " ");
     }
     String str = sb.toString();
     if (!map.containsKey(str)) {
       List<String> tem = new ArrayList<String>();
       map.put(str, tem);
     }
     map.get(str).add(s);
   }
   for (String m : map.keySet()) {
     Collections.sort(map.get(m));
     res.add(map.get(m));
   }
   return res;
 }
  // make sure that A.length <= B.length: m < n
  public double search(int A[], int B[]) {
    int m = A.length;
    int n = B.length;
    if (m == 0) return (B[n / 2] + B[(n - 1) / 2]) / 2.0;
    if (n == 0) return (A[m / 2] + A[(m - 1) / 2]) / 2.0;

    int leftA = 0;
    int rightA = m - 1;
    int leftB = 0;
    int rightB = n - 1;

    leftB = Math.max((n - m - 1) / 2, 0);
    rightB = n - 1 - leftB;

    while (true) {
      if (leftA >= rightA - 1 || leftB >= rightB - 1) break;

      int midA = (leftA + rightA) / 2;
      int midB = (leftB + rightB + 1) / 2;
      if (A[midA] < B[midB]) {
        leftA = midA;
        rightB = midB;
      } else {
        rightA = midA;
        leftB = midB;
      }
    }

    ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i = leftA; i <= rightA; i++) list.add(A[i]);
    for (int i = leftB; i <= rightB; i++) list.add(B[i]);
    Collections.sort(list);

    int count = leftA + leftB;
    int remain = (m + n - 1) / 2 - count;
    if ((m + n) % 2 != 0) return list.get(remain);
    else return (list.get(remain) + list.get(remain + 1)) / 2.0;
  }
示例#16
0
 public static void sortStringKeyValueList(List<StringKeyValue> listNovaFirma) {
   Collections.sort(listNovaFirma, STRINGKEYVALUECOMPARATOR);
 }
示例#17
0
 @Override
 public List<Customer> listAllCustomers() {
   List<Customer> customers = new ArrayList<Customer>(CustomerServiceImpl.customers.values());
   Collections.sort(customers);
   return customers;
 }