y****n 发帖数: 743 | 1 这道题相对高效一点的算法是:
public int Rand7()
{
int num = 100;
while (num >= 21)
num = Rand5() * 5 + Rand5();
return num / 3; //或者 return num % 7;
} |
|
d*********g 发帖数: 154 | 2
,
刚写了一个练练~~大家看看有问题没
void printNumCount(int arr[], int n)
{
for(int i = 0; i < n; ++i)
{
int num = arr[i];
while(arr[num-1] != num)
{
swap(arr, num-1, i);
num = arr[i];
}
}
for(int i = 0; i < n; ++i)
{
if(arr[i] == i+1)
arr[i] = 1;
else
{
arr[arr[i]-1]++;
arr[i] = 0;
}
}
} |
|
d*********g 发帖数: 154 | 3
,
刚写了一个练练~~大家看看有问题没
void printNumCount(int arr[], int n)
{
for(int i = 0; i < n; ++i)
{
int num = arr[i];
while(arr[num-1] != num)
{
swap(arr, num-1, i);
num = arr[i];
}
}
for(int i = 0; i < n; ++i)
{
if(arr[i] == i+1)
arr[i] = 1;
else
{
arr[arr[i]-1]++;
arr[i] = 0;
}
}
} |
|
j******2 发帖数: 362 | 4 可以不全转,但流程是一样:
1.用stack记op,push的不是算符而是优先级。
2.用int记num,来一个数num++,处理一个算符num--(-2+1)。
每次来op时检查num>=2,最后检查num==1。 |
|
j******2 发帖数: 362 | 5 可以不全转,但流程是一样:
1.用stack记op,push的不是算符而是优先级。
2.用int记num,来一个数num++,处理一个算符num--(-2+1)。
每次来op时检查num>=2,最后检查num==1。 |
|
y*******o 发帖数: 6632 | 6 int k = num.size(); ---
int target = -num[i];
int duble = 0;
while (j < k) {
duble = num[j] + num[k]; ---
num is out of array bound, right? |
|
t*********h 发帖数: 941 | 7 from collections import defaultdict
num1=2345678
num2=3429
def convert2int(lst):
return int(''.join(map(str,lst)))
def compare(num, result,l,digits):
n1=convert2int(str(num)[:l+1])
n2=convert2int(result[:l+1])
if l==(digits-1):
return n2>n1
return n2>=n1
def find_next(x1,num,result,digits,l):
if l==digits:
print 'found:'+''.join(map(str,result))
return
for i in xrange(10):
x=x1.copy()
if x[i]>0:
result[l]=i
x[i]-=1
if compare(num,result,l,digit... 阅读全帖 |
|
b******7 发帖数: 92 | 8 分两步:先压缩连续出现次数大于1的字符,再压缩连续出现次数为1的字符
int print_number(int num, char * buf)
{
assert(num >0);
char * p = buf;
while(num > 0)
{
*p++ = num%10 + '0';
num /= 10;
}
//[buf,p) reverse
int len = p - buf;
for(p--;buf < p; buf++,p--)
{
char tmp = *buf;
*buf = *p;
*p = tmp;
}
return len;
}
void compress(char * src)
{
if(src == NULL || *src == '\0') return;
char * p0 = src;
int singlenum = 0;
char * start = src;
... 阅读全帖 |
|
S*******w 发帖数: 24236 | 9 def findMissingTwo(numList, N):
summ = 0
for num in numList:
summ += num
summOfMissingTwo = (N+1)*N/2 - summ
summ = 0
M = summOfMissingTwo/2
for num in numList:
if num <= M:
summ += num
a = (M+1)*M/2 - summ
b = summOfMissingTwo - a
print "The missing two are:%d and %d)"%(a, b)
numList = [1, 2, 3, 4, 0, 6, 0, 8, 9, 10]
findMissingTwo(numList, len(numList)) |
|
t*******2 发帖数: 182 | 10 也写了一个,思路和ls的大同小异吧
public class Solution {
public String countAndSay(int n) {
if(n < 1) {
return "";
}
String result = "1";
for(int i=1; i
result = translate(result);
}
return result;
}
public String translate (String n) {
StringBuilder sb = new StringBuilder("");
String temp = n;
int[] digits = new int[temp.length()];
... 阅读全帖 |
|
s*******n 发帖数: 305 | 11 用heap, nlogk
// T(nlgk), S(k+lgk)
public static void heapWay1(int[] num, int k) {
PriorityQueue heap = new PriorityQueue(k);
int size = k;
for (int i=0; i
if (heap.size()
heap.offer(num[i]);
else { // reach the size==k
if (num[i]<=heap.peek()) // smaller than root
continue;
else { // remove root, insert new one
... 阅读全帖 |
|
s********u 发帖数: 1109 | 12 为了保险起见,我自己试了一下:
stringstream stream;
stream.str("3745+38");
int num;
char op;
stream>>num;
cout << num << endl;
stream>>op;
cout<< op <
stream>>num;
cout<
确认是可以分开3745,+, 38。就算没有空格也是如此。
何况应该后缀表达式数字之间至少是有空格隔开的,否则怎么表示23+3.
在这种情况下,stream>>str,然后再用string创建stream转换成数字或者直接取str[0
]操作符就行了。 |
|
x*******d 发帖数: 196 | 13 哥们儿,代码写的不错。经典的筛法。
面你的人水平差,可能没见过这个方法,这个有可能是黑你;还有可能是真傻,然后又
自以为是,觉得你方法不行。
有个小优化可以考虑下,内循环的乘法可以避免,每次用加法。
for(int j=i*i; j<=num;j+=i)
flags[j] = false;
用的是collabedit,面试结束,我还截屏了下备份,在eclipse中也测试成功。 code在
下面。 也造福其他童靴。
public void printPrimeNum(int num){
boolean[] flags=new boolean[num+1];
for(int i=2; i
for(int i=2; i*i<=num; i++){
if(flags[i]){
for(int j=i; j*i<=num;j++)
flags[i*j]=false;
}
... 阅读全帖 |
|
l***p 发帖数: 358 | 14 中间有0的情况,比如
10042319
#include
int main(int argc, char * argv[])
{
int num = atoi(argv[1]);
char s[] = "45002319";
printf("source: %s and num:%d", s, num);
size_t size;
while (num > 0 && (s && (size = strlen(s)) > 0))
{
if (size > 1)
{
if (s[0] > s[1])
{// 4[<4, 0]
size_t cz = 1;
char * n = s + 1;
... 阅读全帖 |
|
p*****2 发帖数: 21240 | 15 就是个小智商题吧?
(defn last-digit [num]
(cond
(neg? num) (last-digit (- num))
(zero? num) 1
:default (let [t (mod num 4)
m (if (zero? t) 4 t)]
(int (mod (Math/pow 2 m) 10))))) |
|
y***i 发帖数: 414 | 16 public static List> getPerms(int[] num) {
if (num == null || num.length == 0) {
return null;
}
List> res = new ArrayList>();
List first = new ArrayList();
res.add(first);
for (int i = 0; i < num.length; i++) {
List> newRes = new ArrayList>();
for (int j = 0; j < res.size(); j++) {
List current = res.ge... 阅读全帖 |
|
g**********y 发帖数: 14569 | 17 1. 拉格朗日定理:任何一个整数都可以分解成4个整数的平方和。
2. a_i <= sqrt(T)
3. BFS, search one square sum (0 ~ sqrt(T)), then two square sum, ... it
will end at most level 4.
程序可能可以更高效 --
public List split(int N) {
int n = (int) Math.sqrt(N);
int[] square = new int[n];
for (int i=0; i
List[] res = new List[N];
res[0] = new ArrayList();
Queue queue = new ArrayDeque();
q... 阅读全帖 |
|
i******s 发帖数: 301 | 18 AC代码:
public class Solution {
public int findMin(int[] num) {
int min = num[0];
for (int i=1; i < num.length; ++i) {
if (min > num[i]) {
min = num[i];
}
}
return min;
}
} |
|
s****i 发帖数: 65 | 19 perm(num,k+1,n,res);
后面3句,
tmp = num[k];
num[k]=num[i];
num[i]=tmp;
原先i,k上的值换了,现在又换回来了,下一个for循环跟下一个数换 |
|
l***p 发帖数: 160 | 20 time series 的题和leetcode 的 Largest Rectangle in Histogram 很像
我的解法(把int 改成double) 是一样的。
class Solution {
public:
vector maxDays(vector nums) {
int size = nums.size();
vector result(size, 0);
nums.push_back(INT_MAX);
stack s;
int idx = 0;
while (idx <= size) {
if (s.empty() || nums[idx] < nums[s.top()]) {
s.push(idx++);
}
else {
int cur = s.top();
... 阅读全帖 |
|
p****6 发帖数: 3 | 21 来自主题: JobHunting版 - 求问一个题 my answer of C++ version. This question actually is a partial of the
quicksort method.
void reorder(vector& nums)
{
int len = nums.size();
int start = -1;
for(int i = 0; i < len; i++)
{
if(nums[i] < 0)
swap(nums[i], nums[++start]);
}
} |
|
R*********d 发帖数: 34 | 22 来自主题: JobHunting版 - 刷了半天题 方便起见,就用了ArrayList,不过意思是一样的。
public class PositiveIntegerIterator
{
private int prev = -1;
private Iterator it;
public PositiveIntegerIterator(Iterator it){
this.it = it;
}
public boolean hasNext(){
if(prev > 0){
return true;
}
while(it.hasNext()){
prev = it.next();
if(prev > 0){
return true;
}
}
return false;
}
public Integer next(){
... 阅读全帖 |
|
R*********d 发帖数: 34 | 23 来自主题: JobHunting版 - 刷了半天题 方便起见,就用了ArrayList,不过意思是一样的。
public class PositiveIntegerIterator
{
private int prev = -1;
private Iterator it;
public PositiveIntegerIterator(Iterator it){
this.it = it;
}
public boolean hasNext(){
if(prev > 0){
return true;
}
while(it.hasNext()){
prev = it.next();
if(prev > 0){
return true;
}
}
return false;
}
public Integer next(){
... 阅读全帖 |
|
b*****n 发帖数: 618 | 24
这是个sliding window吧
sum[i]表示nums[i]结尾的长度为n的subarray sum
sum[i] = sum[i - 1] - nums[i - n] + nums[i]
dp[i]表示nums[i]结尾的长度>=n的subarray sum最大值
dp[i] = max(sum[i], dp[i - 1] + nums[i]) |
|
t**r 发帖数: 3428 | 25 public class Solution {
public static String intToRoman(int num) {
String M[] = {"", "M", "MM", "MMM"};
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC",
"CM"};
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX",
"XC"};
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII",
"IX"};
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}
} |
|
I******c 发帖数: 163 | 26 我的java实现,参考了Blaze的思路,不过不知道真正理解了他的思路没有,因为我不
太看得懂javascript.下面是几个点:
1. 这个题目用的是backtracking而不是dp,因为dp不可能给你产生排列组合。dp通常
就是给你一个最值。
2. 时间复杂度是指数级。
3. 题目的本质是产生排列。
4. 一般的backtracking来形成字符串的话,从现有字母集中每次任意挑一个字母,然后
从剩下的字母里继续挑。把所有字母都加到字符串里面去了,这轮程序也就完了。这个
题目因为有个顺序的限制条件,决定程序怎样进行下去并不能用剩下多少字符来决定,
而应该是用已经加入字符串的字母的位置来决定。
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
public class Example5 {
public static void main(String [] args){
... 阅读全帖 |
|
d********i 发帖数: 582 | 27 看到一个答案,可以用一个for loop来检测sudoku 是否valid。
但是dan实在看不懂这句话: board[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3] ==
num
不知道为什么可以check 3 * 3 blocks, 一会除一会乘一会求余,把我看晕。有能帮
忙解释下么?
public static boolean isValidSudoku(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] != '.') {
if (!isValid(board, board[i][j], i, j)) {
return false;
}
... 阅读全帖 |
|
m******0 发帖数: 222 | 28 基本典型的3sum
num = [-5, 5, 10]
sort(num)
for (i=0; i
{
int a=i, b=len-1
while (a <= b)
if (num[a]+num[b] > 0-num[i]) b--;
else if ( < ) a++;
else return true;
}
return false;
O(n^2) |
|
m******0 发帖数: 222 | 29 这个follow up我猜是这个意思:
假设有m个人需要给钱,数目为give[0 ... m-1],然后有n个人需要收钱,数目为
receive[0 ... n-1],(sum(give)=sum(receive))
如果give[i]==receive[j], 那么i给j的钱正好,就是一次交易。如果give[5]=10,
receive[3]=4, receive[8]=6, 那么5要分别给3、8各一次钱,就是2次交易。
现在给定give[]和receive[], 需要求最少交易次数。
想了半天没找到dp的做法,能想到的只有硬搜了,用递归是比较好实现的。有人想到更
好的解法请post。
// 硬搜
int dfs() {
int ans=INT_MAX;
for (int& g : give)
for (int& r : receive)
if (g>0 && r>0) {
int num = min(g,r);
g-=num, r-=num;
ans = min(ans, dfs());
g... 阅读全帖 |
|
S*******C 发帖数: 822 | 30 public boolean hasDuplicate(int[] nums) {
if(nums == null || nums.length == 0)
return false;
int check = 0;
int len = nums.length;
for (int i = 0; i < len; i++)
check ^= (nums[i] - 1) ^ i;
return check != 0;
} |
|
a********r 发帖数: 218 | 31 找subsets的题,看到一个大牛的code:
vector> subsets(vector& nums) {
sort (nums.begin(), nums.end());
int elem_num = nums.size();
int subset_num = pow (2, elem_num);
vector> subset_set (subset_num, vector());
for (int i = 0; i < elem_num; i++)
for (int j = 0; j < subset_num; j++)
if ((j >> i) & 1)
subset_set[j].push_back (nums[i]);
return subset_set;
}
在这里,if ((j >> i) & 1) 怎么理解?
大牛能不能举个例子解释一下这个条件?跪谢了。 |
|
发帖数: 1 | 32 叔这个题目是动态编程。
其实不是很复杂。
1. 就是你先算一下以k为长度,滑动窗口的和,放到一个数组里。
2. 从左到右计算 以i为起点,到i为止,最大值的窗口的起始index
比如说[1,3,2,0]
所对应[0,1,1] 因为 2 + 0 < 3 + 2
3.从右向左计算以z为为起点,从z向右考虑,最大窗口和的起始index
4.因为窗口不能重复,所以i + k <= j + k <= z
在j的范围内遍历并且更新结果就可以了。
代码如下:
class Solution {
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
int sum = 0, len = nums.length - k + 1;
int[] sums = new int[len];
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (i >= k) sum -= nums[i... 阅读全帖 |
|
发帖数: 1 | 33 这题是84题的一个小拓展,不过不是很容易想到。
84题就是说如果直方图的高度是递增的,我们就入栈他的index以计算宽度,如果下降
了,就弹栈清算无法拓展面积的bar。
代码如下:
public int largestRectangleArea(int[] heights) {
Stack st = new Stack<>();
int[] nums = Arrays.copyOf(heights, heights.length + 1);
int res = 0;
for (int i = 0; i < nums.length; i++) {
while (!st.empty() && nums[st.peek()] >= nums[i]) {
int h = nums[st.pop()];
int w = st.empty() ? i : i - 1 - st.peek();
res = Math.max(res, w * h);
... 阅读全帖 |
|
|
|
|
G****A 发帖数: 4160 | 37 // main.cpp
// 10005
#include
#include
using namespace std;
class Monitor {
int num;
public:
Monitor() { num = 1; }
// ~Monitor() {cout<<"des11"<
void incident() { num++; }
void decrement() { num--;}
void print() { cout<<"The times of incident :"<
}
};
class Monitor2 {
Monitor *m;
public:
Monitor2(Monitor * mm): m |
|
a****m 发帖数: 693 | 38 总是说不能display, 如果把所有的class里面的改成public, 就可以直接用num and
denum
输出到 display, 但是如果private,怎么都不行,谢谢。
#include
using namespace std;
class CFraction
{
private: //private method to get GCD
int num;
int denum;
public:
CFraction(int num, int denum);
int getnum();
int getdenum();
void display();
};
//constructor...
int CFraction::getnum(){
return num;
}
int CFraction::getdenum(){
return denum;
}
CFraction::CFraction( int a, int b )
{
num = a;
denum = b;
}
void CFraction::display() {
cout << ... 阅读全帖 |
|
g*********s 发帖数: 1782 | 39 你这个看着像面试题啊。
string s;
bool first_line(true);
int line_count (0);
while (getline(cin, s)) {
stringstream ss(s);
int num;
vector v;
if (first_line) {
ss >> num;
line_count = num;
}
else {
while (ss >> num)
v.push_back(num);
// process v;
}
} |
|
M**T 发帖数: 108 | 40 /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone{
num na, nb, nc, nd, nf;
private class num{
int x, y, z;
public num(int a, int b, int c){
x =a; y = b; z = c;
}
}
public void add_na(int a, int b, int c){
na = new num(a, b, c);
}
public void add_nb(int a, int b, int c){
nb = new num(a, b, c)... 阅读全帖 |
|
b**l 发帖数: 25 | 41 This will work:
List nums = new ArrayList<>();
for (int i = 1; i <= 15; i++) {
nums.add(1);
}
for (int i = 1; i <= 3; i++) {
nums.add(i);
}
Collection> collect = nums.stream().collect(Collectors
.groupingBy(n -> n)).values();
System.out.println(collect);
collect.stream().filter(l -> l.size() > nums.size() / 2)
.flatMap(Collection::stream).distinct()
.forEach(System.out::... 阅读全帖 |
|
j*******2 发帖数: 18 | 42 void getArray(int num)
{
int* a = new int[num + 1];
a[0] = 1;
a[1] = 1;
for(int i = 2; i < num + 1; i ++)
a[i] = a[i - 1] * (i - 1);
for(int j = num - 1; j > 0; j --)
{
a[0] = a[0] * (j + 1);
a[j] = a[j] * a[0];
}
for(int k = 1; k < num + 1; k ++)
printf(" %d",a[k]);
delete [] a;
} |
|
R*********i 发帖数: 7643 | 43 Assuming LZ wants the values in A and B matched:
proc sql;
create table new as
select a.num as A, b.num as B
from have(where=(id='A')) a, have(where=(id='B')) b
where a.num=b.num
order by a.num;
quit; |
|
y****n 发帖数: 46 | 44 data have;
input id $ num;
cards;
A 1
A 2
A 3
A 4
B 1
B 2
B 3
B 4
;
run;
proc sort data=have;
by num id;
run;
proc transpose data=have out=want(drop=_name_ num);
by num notsorted;
var num;
id id;
run; |
|
b*****r 发帖数: 359 | 45 %let add = 0;
data a;
set a;
if num > 1 then
do;
%let add = %eval(&add+1);
put num;
%put &add;
end;
else
put num;
%put &add;
run;
数据表格a,如图所示。只有一行数据。
我要做的是: 如果num>1, 则全局变量add加1。否则,不做任何操作。
可是现在表格a里的num<1,为什么我的代码,还是执行了 %let add = (&add+1);?
谢谢指点!
怎样才能实现我要的操作? |
|
z*********i 发帖数: 146 | 46 data sample:
RCPT_ULI final_pcn our_doc1 GENDER_USE AGE_10 zone level agegroup
111111110 . . . .
111112090 . M 50.41 1 6
111112151 . . . .
111112231 13 F685640 M 52.34 Ed 1 6
111112330 24 F035580 F 39.62 ... 阅读全帖 |
|
t***n 发帖数: 546 | 47 鉴于有人要求详细配置,现在把几个配置文件贴出来,欢迎大牛们指正
gtalk.conf***********************
[general]
context=google-in ; Context to dump call into
allowguest=yes
[guest] ; special account for options on guest account
disallow=all
allow=ulaw
context=google-in
[dummy-gtalk]
username=d***[email protected]
disallow=all
allow=ulaw
context=google-in
connection=dummy
[user3-gtalk]
username=u***[email protected]
disallow=all
allow=ulaw
context=google-in
connection=user3
[user1-gtalk]
username=u***[email protected]
disallow=all
... 阅读全帖 |
|
|
r**u 发帖数: 1567 | 49 这个刚有人总结过,我copy&paste。
要求从N个元素中随机的抽取1个元素, 其中N无法确定(数据流)。
int Num;
int i = 0;
int choose = 0;
while(scanf("%d", &Num)&& Num != 0){
if (((double)rand()/(double)RANDOM_MAX) < (1.0 / (double)++i))
choose = Num;
}
cout << choose <
总是选择第一行, 二分之一几率选择第二行, 以此类推, K分之一机率选择第K行,
当while 执行完毕,假设N个数字,则每个数字被选择的机率均为1/N
第一行被选择的机率为:
1 * 1/2 * 2/3 * 3/4 *.... (N-1)/N = 1/N
第K行被选择的机率为:
1/K * K/(K+1) * .... (N-1)/N = 1/N
probability |
|
c*********n 发帖数: 1057 | 50 不用单独处理if(num/10=0)吧,不然你1004怎么弄呢?
我觉得可以这样:
得到
num
if(head == NULL){
else
return head;
newnode = new Node();
newnode->data = num%10;
newnode->next=head;//insert into the head of the list
ConstructLinkedList(num/10,newnode); |
|