Twitter OA prepare: K-complementary pair】的更多相关文章

2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时,算成不同的pair,比如数组是[3,3],K=6,这时的pair有[0, 0], [0, 1], [1, 0], [1, 1]4个. 这个case让这道本来不难的题一下子麻烦了许多.我的对应处理方法是:用HashMap记录每个元素出现次数,用一个变量res记录可行pair数. 像夹逼方法那样,一左一…
准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shortest(17); System.out.println(a); } public static int shortest(int target) { if (target < 1) { return -1; } int count = 1; while (target != 1) { if (ta…
思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2…
Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence is allowed since in a palindrome maximum number of character with odd occurrence can be '1'. All other character should occur in even number of times.…
分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A, int N) { if (A==null || A.length==0) return 0; int cur = 0; int count = 0; boolean[] visited = new boolean[N]; while (cur>=0 && cur<A.lengt…
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q , where p & q are two integers, and the denominator q is not equal to zero. Hence, all integers are rational numbers where denominator, in the most re…
You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one move on the array: choose any two integers [L, R], and flip all the elements between (and including) the L-th and R-th bits. L and R represent the lef…
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A: A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0 3 is an equil…
Give n points on 2-D plane, find the K closest points to origin Based on bucket sort: package fbPractise; import java.util.*; class Coordinate { int x; int y; public Coordinate(int x, int y) { this.x = x; this.y = y; } } public class Kclosest { publi…
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新 package twoSigma; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.lang.StringBuilder; public class Lon…