`
文章列表
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order.   Example If k1 = 10 and k2 = 22, then your fu ...
算法游戏,给一个只有+-两种字符的array,两个玩家,轮到某个玩家他可以任选 两个连续的++将他们变成--,如果某个玩家发现对方无法行动则赢得游戏,要求写 isWin(String s)判断先行动的玩家能否赢。   Followup 如何优化,时间上和空间上。 public boolean canWin(char[] s) { int start = -1; for (int i = 0; i < s.length; i++) { if (s[i] == '+') { if (start != -1 && ...
Implement Stack in Java. public class MyStack<E> { private static class Node<E> { E value; Node<E> next; public Node(E v, Node<E> n){ value = v; next = n; } } private Node<E> head; private int size = 0; public void push(E e) { head = ...
Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted.   Examples:1) If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should be able to find that the subarray lies betwe ...
[问题描述]:  设有n堆石子排成一排,其编号为1、2、3、…、n(n<=100)。每堆石子的数量用:a[1]、a[2]、…、a[n] 表示,现将这n堆石子归并成一堆,归并的规则: ◆每次只能将相邻两堆归并成一堆,即:第 1 堆石子 a[1] 只能与第 2 堆石子 a[2] 归并,最后一堆石子 a[n] 只能与 a[n-1] 归并,中间的石子 a[i] 只能与 a[i-1] 或 a[i+1] 归并; ◆每次归并的代价是两堆石子的重量之和。  我们假如5堆的石子,其中石子数分别为7,6,5,7,100       •按照贪心法,合并的过程如下:        每次合并得分      ...
Given a family tree for a few generations for the entire population and two people, write a routine that will find out if they are blood related. Siblings are blood related since they have the same parents. Cousins are blood related since one of their parents have the same parents etc.  You have a ...
Given a string and a positive integer d. Some characters may be repeated in the given string. Rearrange characters of the given string such that the same characters become d distance away from each other. Note that there can be many possible rearrangements, the output should be one of the possible r ...
given grid of colors, coordinate of a point and its color, find the perimeter of the region that has the same color of that point. BFS或DFS,构成perimeter的条件是只要上下左右有一个不是同颜色或者是out of bound 用一个set记录visit的信息   public static class Point{ int r, c; public Point(int x, int y) {r = x; c = y;} public i ...
given an order string "abc" check if "aabdccd" maintain the order "aabdccd" -> true; "abbca" -> false; note:order does not contain all chars in s   public boolean checkOrder(String pat, String s) { int[] pos = new int[256]; Arrays.fill(pos, -1); ...
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. public int computeArea(int A, int B, int C, ...
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You must use only standard operations of a s ...
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +,- and *. Example 1 Input: "2-1-1". ((2-1)-1) = 0 (2-(1-1)) = 2 Output: [0, 2]
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given expression is always valid. Some examples: " ...
  /* This class will be given a list of words (such as might be tokenized * from a paragraph of text)
实现一个mini parser, 输入是以下格式的string:"324" or"[123,456,[788,799,833],[[]],10,[]]"要求输出:324 or [123,456,[788,799,833],[[]],10,[]].也就是将字符串转换成对应的格式的数据. 输入一个数组的字符串, 要返回一个数组, 里面每一个元素是要么一个整数, 要么是一个数组. 但是注意数组可以多层嵌套.    Solution: public class NestedIntList { private int value; pri ...
Global site tag (gtag.js) - Google Analytics