s********x 发帖数: 914 | 1 切磋一下
public static ArrayList multiplyBigInterger(List num1,
List num2) {
if (num1 == null || num1.isEmpty() || num2 == null || num2.isEmpty()
) {
throw new IllegalArgumentException("Invalid input");
}
if (num1.size() < num2.size()) {
return multiplyBigInterger(num2, num1);
}
// num1.size() >= num2.size()
ArrayList result = new ArrayList(num1.size()
... 阅读全帖 |
|
f*******t 发帖数: 7549 | 2 找出了一年多前写的逆波兰处理算数表达式的代码,强烈建议有兴趣的自己实现一下:
#include
#include
#include
#include
#define BUFFSIZE 1024
using namespace std;
struct Token {
bool isNum;
int num;
char op;
Token();
Token(const Token& t);
};
Token::Token()
{
isNum = false;
num = 0;
op = 0;
}
Token::Token(const Token& t)
{
isNum = t.isNum;
num = t.num;
op = t.op;
}
Token *getToken(const char expr[], int& idx)
{
Token *res = NULL;
while(expr[idx] == ' ')
... 阅读全帖
|
|
z********c 发帖数: 72 | 3 这是Leetcode上Multiplication那道题的程序,过了数据
string multiply(string num1, string num2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector res(num1.size () + num2.size (), 0);
for (int i = 0; i < num1.size (); i++) {
int carry = 0;
for (int j = 0; j < num2.size (); j++) {
int d1 = num1[num1.size () - 1 - i] - '0';
int d2 = num2[num2.size () - 1 - j] - '0';
c... 阅读全帖 |
|
b*******y 发帖数: 2048 | 4 public int Compare(String s1, String s2)
{
String[] a1 = s1.Split('.');
String[] a2 = s2.Split('.');
int i=0;
while(i
{
ss1=a1[i];
ss2 = a2[i];
int num1 =integer.parseInt(ss1);
int num2 = integer.parseInt(ss2);
if(num1==0 || num2 ==0)
return num1-num2;
while(ss1.charAt(0)=='0')
{
ss1 = ss1.subString(1);
num2 *=10;
}
while(ss2.charAt(0)=='0')
{
ss2 = ss2.sub... 阅读全帖 |
|
r*c 发帖数: 167 | 5 I tend to believe either Greedy or DP works. Here is a DP solution, not
fully tested. Greedy is preferred, as it is cleaner and shorter.
int minPairDistSum(const vector num1, const vector num2) {
int len = num1.size();
assert(len == num2.size());
if(len <= 0) return INT_MIN;
vector >dp(len + 1, vector(len +1, 0));
for(int i=0; i<=len; ++i){
for(int j=0; j<=len; ++j){
dp[i][j] = INT_MAX;
... 阅读全帖 |
|
s*********d 发帖数: 2406 | 6 没什么好说的,还是自己不行。
第一题
abstract class absClass
{
public int AddTwoNumbers(int Num1, int Num2)
{
return Num1 + Num2;
}
public abstract int MultiplyTwoNumbers(int Num1, int Num2);
}
实例化 absclass
interviewer 说 不需要用考到任何算法知识。只考java。
answer :
public class aaClass extends absClass {
public int MultiplyTwoNumbers(int Num1, int Num2){
int multiplyresult = 0 ;
multiplyresult=Num1*Num2;
return multiplyresult;
}
这个有什么问题?
Q2:经典题
Q2:
Given a sorted 2-D array (M x N) con... 阅读全帖 |
|
x**********4 发帖数: 70 | 7 import re
class Solution:
def evaluate(self, expr):
tokens = re.split('[ ()]', expr)
tokens = [token for token in tokens if token]
var, stack, i = {}, [], 0
keywords = set(['add', 'mult', 'let'])
while i < len(tokens):
if tokens[i] == 'add' or tokens[i] == 'mult':
stack.append(tokens[i])
i += 1
elif tokens[i] == 'let':
i += 1
while i < len(tokens) and tokens[i] not ... 阅读全帖 |
|
b****e 发帖数: 45 | 8 贴一个我的版本:
string multiply(string num1, string num2) {
string res(num1.size() + num2.size(), '0');
int carry = 0;
for (int i = num2.size() - 1; i >= 0; i--) {
int digit2 = num2[i] - '0';
for (int j = num1.size() - 1; j >= 0; j--) {
int m = digit2 * (num1[j] - '0') + res[i + j + 1] - '0' + carry;
res[i + j + 1] = m % 10 + '0';
carry = m / 10;
}
if (carry > 0) {
int m = res[i] - '0' + carry;
res[i... 阅读全帖 |
|
i*********7 发帖数: 348 | 9 抛砖引玉一下
这是我做的大数乘法
void bigmultiply(string num1, string num2){
vector result(num1.size()+num2.size(), 0);
int carry = 0;
for(int i = num2.size() - 1; i >= 0; i--){
carry = 0;
for(int j = num1.size() - 1; j >= 0; j--){
int i1 = num1[j] - '0';
int i2 = num2[i] - '0';
int curdig = (i1 * i2) % 10;
int nextbit = i1 * i2 / 10;
int cursum = curdig + carry + result[i + j + 1];
carry = cursum/10 + nextb... 阅读全帖 |
|
j*****y 发帖数: 1071 | 10 写了一个,欢迎测试
#include
#include
#include
#include
#include
using namespace std;
bool isNumber(string s)
{
if(s[0] == '+' || s[0] == '-' || s[0] == '(' || s[0] == ')')
{
return false;
}
return true;
}
int helper(int a, int b, char c)
{
if(c == '+')
{
return a + b;
}
else
{
return a - b;
}
}
bool valid(vector &s)
{
stack v;
stack c;
for(int i = 0; i < s.size(); ++i)
... 阅读全帖 |
|
j*****y 发帖数: 1071 | 11 写了一个,欢迎测试
#include
#include
#include
#include
#include
using namespace std;
bool isNumber(string s)
{
if(s[0] == '+' || s[0] == '-' || s[0] == '(' || s[0] == ')')
{
return false;
}
return true;
}
int helper(int a, int b, char c)
{
if(c == '+')
{
return a + b;
}
else
{
return a - b;
}
}
bool valid(vector &s)
{
stack v;
stack c;
for(int i = 0; i < s.size(); ++i)
... 阅读全帖 |
|
w****r 发帖数: 28 | 12 试着写了个python的, n = 2000 花时间大概十几秒钟
def tolist(n):
result = []
while (n >= 10):
result.append(n % 10)
n = n / 10
result.append(n)
return result
def mult(num1, num2):
result = [0]*(len(num1) + len(num2))
for i in range(len(num1)):
for j in range(len(num2)):
temp = num1[i] * num2[j]
if temp >= 10:
result[i + j] += temp % 10
result[i + j + 1] += temp / 10
else:
result[i + j] += temp
for k in range(len(result)):
if result[k] >= 1... 阅读全帖 |
|
w***o 发帖数: 109 | 13 我也凑个热闹
public String multiply(String num1, String num2) {
int n = num1.length();
int m = num2.length();
int[] result = new int[n + m];
for(int i = m - 1; i >= 0; i--) {
int k = n + i;
int a = num2.charAt(i) - '0';
for(int j = n - 1; j>= 0; j--) {
int x = a * (num1.charAt(j) - '0');
x += result[k];
result[k--] = x % 10;
result[k] += x / 10;
}
}
... 阅读全帖 |
|
l********t 发帖数: 14 | 14 可以这样做
step1 记录数组里面有多少 0,1,2
num_0 = num_of_0;
num_1 = num_of_1;
num_2 = num_of_2;
0 to num0 为zone1 num0 to num0+num1 定义为zone2 num0+num1 to num0+num1+num2
定义为zone3
step2
zone1里面 如果是0 不动 如果是2 和zone3里面的0互换
这样zone1和zone3 里面 一个有0,1,2,一个只有0,1 或者1,2
假设是0,1的情况 也就是zone1只剩下0,1,zone3还有0,1,2
那么扫zone2, 把里面的2 全部和zone3里面的0交换
这样可以保证 zone1只有0,1, zone2有0,1,2, zone3 只有1,2
接下来zone1里面的1和zone2里面的0换
zone3 里面的1和zone2里面的2换
这样 zone1只有0 zone2只有1 zone3只有2 |
|
l********t 发帖数: 14 | 15 可以这样做
step1 记录数组里面有多少 0,1,2
num_0 = num_of_0;
num_1 = num_of_1;
num_2 = num_of_2;
0 to num0 为zone1 num0 to num0+num1 定义为zone2 num0+num1 to num0+num1+num2
定义为zone3
step2
zone1里面 如果是0 不动 如果是2 和zone3里面的0互换
这样zone1和zone3 里面 一个有0,1,2,一个只有0,1 或者1,2
假设是0,1的情况 也就是zone1只剩下0,1,zone3还有0,1,2
那么扫zone2, 把里面的2 全部和zone3里面的0交换
这样可以保证 zone1只有0,1, zone2有0,1,2, zone3 只有1,2
接下来zone1里面的1和zone2里面的0换
zone3 里面的1和zone2里面的2换
这样 zone1只有0 zone2只有1 zone3只有2 |
|
e***s 发帖数: 799 | 16 我贴个C#的吧,不过前面要自己顶一个一个lookup table
public static int RomantoInteger(string s)
{
int ret = 0;
if (s.Length == 0)
return ret;
string num1 = "";
string num10 = "";
string num100 = "";
string num1000 = "";
for (int i = 0; i < s.Length; i++)
{
if (s[i] == 'I' || s[i] == 'V')
{
num1 = s.Substring(i);
break;
... 阅读全帖 |
|
p*****2 发帖数: 21240 | 17 好久没写了。刚才写了一个练练。大牛给指点一下。
class Version implements Comparable
{
private String version;
private String[] getNumbers(String ver)
{
return ver.split("\\.");
}
public Version(String _version)
{
version=_version;
}
public String getVersion()
{
return version;
}
public int compareTo(Version v)
{
String[] arr1=getNumbers(version);
String[] arr2=getNumbers(v.getVersion());
int ... 阅读全帖 |
|
t*********h 发帖数: 941 | 18 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... 阅读全帖 |
|
C****p 发帖数: 6 | 19 我的java代码,暴力两层dfs,672ms竟然过了。。。
第一层遍历所有数字组合,第二层遍历所有运算符组合,calculate的时候用了deque来
处理乘法优先的情况
不得不说这题有点变态,好像是G家的面试题?
public class Solution {
public List addOperators(String num, int target) {
List ans = new ArrayList();
if (num == null || num.length() == 0) {
return ans;
}
char[] digits = num.toCharArray();
List numbers = new ArrayList();
dfs(ans, target, numbers, digits, 0);
return ans;
}
... 阅读全帖 |
|
L**********1 发帖数: 797 | 20 1.
int getClosestTo1K (int num1, int num2) {
if(Math.abs(1000-num1) < Math.abs(1000-num2)) {
return num1;
} else {
return num2;
}
} |
|
p**********r 发帖数: 10 | 21 I have the following dataset data1:
var num1 num2 count
a 1 11 2
a 1 11 2
b 2 21 1
c 3 45 3
c 3 45 3
c 3 45 3
count实际上就是# of ob in group(var num1 num2)
我的问题时,我想要挑出count最大的那个组,记录下num1,num2,然后用这两个 |
|
g*******y 发帖数: 380 | 22 说实在的,不懂你在说什么?
第一,你所指的保留值是什么?是指你在data1里提取的count的值为最大的组里对应的
num1,num2吗?
第二,不懂你为什么要想那么多复杂的方法?因为从你的sample来看,你的每个组只是根
据count的值来进行重复,count是多少,每个组重复的观察值就是几个.你如果只是想要
count最大组里的num1,num2来剔取data2里的观察值的话.我觉得最简单的方法:
proc sort data=data1; by descending count;
用最大count组里的值,比如x,y来剔除你不要的观察值.
data temp; set data2; if num1=x and num2=y;
如果这个不是你想要的,那么也许我没有看懂你的问题. |
|
w***y 发帖数: 114 | 23 I donot understand what you mean?
if you only want the max count in data a that means final data a only have
one obs? or you want var as group
proc sql;
create data derived_a as
select var, num1, num2, max(count) as count
from a
group by var /*if you donot want it, just delete it*/
order by var
;
quit;
then you can combine two dataset together
proc sql;
create table a_b as
select b.*, a.count
from b as b, derived_a as a
where b.num1=a.num1 and b.num2=a.num2
;
quit;
Good luck |
|
g*******y 发帖数: 380 | 24 Then trying this:
proc sql;
create table new as
select a.*
from data2 as a
right join (select distinct num1, num2
from (select * , max(count) as max_count
from data1
having count=calculated max_count)) as b
on a.num1=b.num1 and a.num2=b.num2
;
quit; |
|
p********a 发帖数: 5352 | 25 data b(keep=id num1 rename=(num1=num));
set a;
do i=1 to num;
num1=1; output;
end;
run; |
|
m********1 发帖数: 31 | 26 请问你code里的
int d1 = num1[num1.size () - 1 - i] - '0';
int d2 = num2[num2.size () - 1 - j] - '0';
那个减去 '0' 是干什么用的呢? |
|
s*********d 发帖数: 2406 | 27 估计是了。这是个问题
public class aaClass extends absClass {
public long MultiplyTwoNumbers(int Num1, int Num2){
long multiplyresult = 0 ;
multiplyresult=Num1*Num2;
return multiplyresult;
} |
|
X*K 发帖数: 87 | 28 第一题如果不考虑溢出的话似乎也就是加一个可有可无的@Override了:
public class aaClass extends absClass {
@Override
public int MultiplyTwoNumbers(int Num1, int Num2) {
int multiplyresult = 0 ;
multiplyresult=Num1*Num2;
return multiplyresult;
} |
|
y***x 发帖数: 22 | 29 写了个JavaScript的,想法跟二爷一开始的一样,扫两遍,一遍处理+-, 一遍处理*/
function getnum(str){
var n = str.split(/[+\-]+/);
var c = str.split(/[0-9\*\/]+/);
for(var i=0; i
n[i] = getmdnum(n[i]);
}
for(var j=0; j
n[j+1] = cal(n[j], n[j+1], c[j+1]);
}
return n[j];
}
function getmdnum(str){
var n = str.split(/[\*\/]/);
var c = str.split(/[0-9]+/);
if(n.length==1) return n[0]-0;
for(var i=0; i
... 阅读全帖 |
|
y***x 发帖数: 22 | 30 写了个JavaScript的,想法跟二爷一开始的一样,扫两遍,一遍处理+-, 一遍处理*/
function getnum(str){
var n = str.split(/[+\-]+/);
var c = str.split(/[0-9\*\/]+/);
for(var i=0; i
n[i] = getmdnum(n[i]);
}
for(var j=0; j
n[j+1] = cal(n[j], n[j+1], c[j+1]);
}
return n[j];
}
function getmdnum(str){
var n = str.split(/[\*\/]/);
var c = str.split(/[0-9]+/);
if(n.length==1) return n[0]-0;
for(var i=0; i
... 阅读全帖 |
|
x****m 发帖数: 1084 | 31 multiply string seems........
class Solution:
def multiply(self, num1, num2):
return str(int(num1) * int(num2)) |
|
发帖数: 1 | 32 开始刷题,提交之后告诉Runtime: 116 ms,如何知道这个运行时间好坏?能否看到别
人的提交?看到有Java的Runtime 4ms,差很多,自己的算法好坏如何判断?
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both
arrays.
The result can be in any order.
难度Easy。
我的代码:
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersect = function(nums1, nums2) {
if ( nums1.length ... 阅读全帖 |
|
c********m 发帖数: 571 | 33 Thanks for this great information.
My refinance loan is more than the loan balance. But they also paid the
second half of my property tax in the closing. I just found another piece of
paper with summary amount.
This is the amount I should get:
$num1 = $newloan - $payoff - $propertytax + $appraisal
And I got two checks:
$num2 = $check1 (from closing) + $check2(from agent)
Just found $num2 is $5 less than $num1. Looks like it's the fee for property
tax duplicate bill.
So it's reasonable for me to ... 阅读全帖 |
|
c*******u 发帖数: 1269 | 34 struct A
{
A(int num1, int num2, int num3 ...)
{
para.line1[0]=num1;
...
};
struct
{
int line1[2];
int line2[3];
}para;
} ; |
|
|
p**********r 发帖数: 10 | 36 多谢啊,可能使我表述得太差了,但需要的就是这个结果。
我也想过用这个方法,但是这中间有一个手动纪录num1 num2的过程,如果对1000+不同
的样本做会很麻烦,而且实际上我需要比较的值除了num1 num2之外还有num3 num4。。。
不知有没有什么办法? |
|
g*******y 发帖数: 380 | 37 I still don't understand why do you care the number of observations?
What you need is just the observation which has the maximum count,right? If
you really don't like type the value of each variable, try to get it by
subset data or using proc means, then you can use proc sql to join dataset
by using:
"where a.num1=b.num1 and a.num2=b.num2 and..." |
|
o**m 发帖数: 828 | 38 假如我的data是这样子的:
prod num1 num2
A 10 30
B 1 3
A 20 20
A 30 10
B 3 1
我想得到的是 第二列和第三列数据的 关于 prod上的平均,有什么R的函数可以操作?
比如我想要的结果是:
prod num1 num2
A 20 20
B 2 2
谢谢
|
|
h********o 发帖数: 103 | 39 It's not good to save DATE values as character in SAS. Actually it is a
numeric variable. you need to use INFORMAT/FORMAT to read/write those data.
For your case, you can use the following code:
=================
data one;
input char1 $ num1 date : mmddyy10. char2 $ num2;
format date mmddyy10.;
cards;
Adriana 21 3/21/2000 MP 7
Nathan 14 3/21/2000 CD 19
;
proc print;
run;
Obs char1 num1 date char2 num2
1 Adriana 21 03/21/2000... 阅读全帖 |
|
Y*****y 发帖数: 361 | 40 整体的idea很巧妙,但是实现的最后一步没完全明白。
假设那两个出现过一次的数是N1, N2。如果那些出现过两次的数在 indexOf1 这一位上
是1(或者0), 并且在array中出现在N1和N2之后,那num1和num2指的就不是N1和N2的
位置了吧。 |
|
l*****a 发帖数: 14598 | 41 第一题到底考什么?
为什么不是
return Num1*Num2;
如果说溢出的话,那个Add就不溢出了?
请牛人指点!!! |
|
l*****a 发帖数: 14598 | 42 why not
return Num1*Num2? |
|
f********a 发帖数: 165 | 43 string multiply(string num1, string num2) {
需要考虑负数,非正常输入,比如--+@2就不行把,处理00012 X 00012这种类型的。面
t家的时候被这些边界条件整死了。 |
|
T****U 发帖数: 3344 | 44 应该是num1<=num2<=num3<=num4<=num5 |
|
f********y 发帖数: 156 | 45 没错,还要就是每个数最多出现5次,因为同一个数最多被5个不同threads 打印
应该是num1 |
|
b******b 发帖数: 713 | 46 面了google/facebook/linkedin/two sigma/aqr/uber, 被uber/aqr据了。基本所有面
过的题:
hedge fund 1:
1. Write a function that takes as input integers P and Q and returns P to
the power of Q. Note any assumptions you make and the complexity of the
algorithm. We expect you to do better than O(Q).
2. Write a function that takes as input an array of 1 million integers,
such that 1 ≤ x ≤ 10 for every element x in the array, and returns the
sorted array. The sort does not need to occur in-place. Obviously you ... 阅读全帖 |
|
a********5 发帖数: 1631 | 47 因为除法。
这道题要考虑除法就非常蛋疼了。
op = /, num1 = 10,
max_num2 = 100
min_num2 = -100
但是num2也可以取1
而且还要考虑正负号 |
|
|
|