public void addTaskToHead(OMPTask task) { taskLock.lock(); try { tasks.offerFirst(task); } finally { taskLock.unlock(); } }
public int[] larger(int input[]) { Deque<Integer> stack = new LinkedList<Integer>(); int result[] = new int[input.length]; for (int i = 0; i < result.length; i++) { result[i] = -1; } stack.offerFirst(0); for (int i = 1; i < input.length; i++) { while (stack.size() > 0) { int t = stack.peekFirst(); if (input[t] < input[i]) { result[t] = i; stack.pollFirst(); } else { break; } } stack.offerFirst(i); } return result; }
public int inc(int bom, int dis) { bom %= size; if (bom == 0) { bom = size; } Deque<Integer> tempStack = new LinkedList<Integer>(); for (int i = 0; i < bom; i++) { tempStack.offerLast(stack.pollFirst() + dis); } for (int i = 0; i < bom; i++) { stack.offerFirst(tempStack.pollLast()); } return stack.peekLast(); }
public String removeDuplicateLetters(String s) { Deque<Character> stack = new LinkedList<>(); Map<Character, Integer> count = new HashMap<>(); for (int i = 0; i < s.length(); i++) { count.compute( s.charAt(i), (key, val) -> { if (val == null) { return 1; } else { return val + 1; } }); } Set<Character> visited = new HashSet<>(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); count.put(ch, count.get(ch) - 1); if (visited.contains(ch)) { continue; } while (!stack.isEmpty() && stack.peekFirst() > ch && count.get(stack.peekFirst()) > 0) { visited.remove(stack.peekFirst()); stack.pollFirst(); } stack.offerFirst(ch); visited.add(ch); } StringBuffer buff = new StringBuffer(); while (!stack.isEmpty()) { buff.append(stack.pollLast()); } return buff.toString(); }
public static void main(String[] args) { String s1 = "ABCDEBF"; System.out.println(s1.length()); System.out.println(s1.charAt(4)); System.out.println(s1.indexOf("B")); System.out.println(s1.substring(0, 2)); System.out.println(s1.lastIndexOf("B")); System.out.println(s1.toUpperCase()); System.out.println(s1.toLowerCase()); System.out.println(s1.contains("B")); System.out.println(s1.concat("GP")); System.out.println(s1.startsWith("A")); System.out.println(s1.endsWith("P")); System.out.println(s1.isEmpty()); char[] s2 = s1.toCharArray(); Deque<Character> qc = new LinkedList<>(); for (int i = 0; i < s2.length; i++) { qc.offerFirst(s2[i]); } while (!qc.isEmpty()) { System.out.println(qc.pollFirst()); } }
/** * Return a worker, returning it to the worker pool. * * @param worker the worker to be released */ void returnWorker(RingoWorker worker) { if (!workers.offerFirst(worker)) { worker.shutdown(); } }
@Override public void pendingValue(Message message) { pendingValues.offerFirst(message); }
/** * Finds an A* path to the target from the start. If no path is possible, returns null. * * @param startx the x coordinate of the start location * @param starty the y coordinate of the start location * @param targetx the x coordinate of the target location * @param targety the y coordinate of the target location * @return the shortest path, or null */ public Queue<Coord> path(int startx, int starty, int targetx, int targety) { start = Coord.get(startx, starty); target = Coord.get(targetx, targety); open.clear(); finished = new boolean[width][height]; parent = new Coord[width][height]; Direction[] dirs; switch (type) { case MANHATTAN: dirs = Direction.CARDINALS; break; case CHEBYSHEV: case EUCLIDEAN: case DIJKSTRA: default: dirs = Direction.OUTWARDS; break; } Coord p = start; open.add(p); while (!p.equals(target)) { finished[p.x][p.y] = true; open.remove(p); for (Direction dir : dirs) { int x = p.x + dir.deltaX; if (x < 0 || x >= width) { continue; // out of bounds so skip ahead } int y = p.y + dir.deltaY; if (y < 0 || y >= height) { continue; // out of bounds so skip ahead } if (!finished[x][y]) { Coord test = Coord.get(x, y); if (open.contains(test)) { double parentG = g(parent[x][y].x, parent[x][y].y); if (parentG < 0) { continue; // not a valid point so skip ahead } double g = g(p.x, p.y); if (g < 0) { continue; // not a valid point so skip ahead } if (parent[x][y] == null || parentG > g) { parent[x][y] = p; } } else { open.add(test); parent[x][y] = p; } } } p = smallestF(); if (p == null) { return null; // no path possible } } Deque<Coord> deq = new ArrayDeque<>(); while (!p.equals(start)) { deq.offerFirst(p); p = parent[p.x][p.y]; } return deq; }