由买买提看人间百态

topics

全部话题 - 话题: nums
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
w****x
发帖数: 2483
1
int mul(const char*& p);
int num(const char*& p);
int Add(const char*& p)
{
int res = mul(p);
while (*p == '+' || *p == '-')
{
if (*p == '+')
res += mul(++p);
else
res -= mul(++p);
}
return res;
}
int mul(const char*& p)
{
int res = num(p);
while (*p == '*' || *p == '/')
{
if (*p == '*')
res *= num(++p);
else
res /= num(++p);
}
return res;
}
int num(const char*& p)
{
bool b... 阅读全帖
z***8
发帖数: 17
2
来自主题: JobHunting版 - Leetcode 最新题, 搞不懂

下面的code需要美化,不过应该是O(n):
void FakeRadixSort(vector &num, vector &temp, int N)
{
vector count;
count.resize(2*N);
int i=0;
for (i=0; i<2*N; i++)
{
count[i] = 0;
}
int size = num.size();

for (i=0; i {
int index = num[i] % N;
index += N;
++ count[index];
}
for (i=1; i<2*N; i++)
{
count[i] += count[i-1];
}
for (i=size-1; i>=0; i--)
{
int index = num[i] % N;
... 阅读全帖
r***e
发帖数: 29
3
来自主题: JobHunting版 - BBC 编程风格面试题,求指教
我的答案(C++):
#pragma once
#ifndef _ROMON_HEADER_
#define _ROMON_HEADER_
#include
#include
using namespace std;
const string ROMON_DIGIT[] = {"","I","II","III","IV","V","VI","VII","VIII","
IV"};
//ROMON 0-9
const int ROMON_SCALE[] = {10, 50, 100, 500, 1000};
const char ROMON_SCALE_REP[] = {'X', 'L', 'C', 'D', 'M'};
//ROMON scale
const int ROMON_MAX = 3999;
const int ROMON_MIN = 1;
// rewrite the interface
class RomanNumeralGenerator
{
public:
virtual string generator(int n... 阅读全帖
l******s
发帖数: 3045
4
来自主题: JobHunting版 - f家店面题
private int minHops(byte[] nums){
//dp[i,j] is hop times at pace amount j of each stone i
int[,] dp = new int[nums.Length, nums.Length];
if (nums.Length < 1 || nums[0] == 0) return -1;
for (int i = 0; i < nums.Length; i++){
if (nums[i] == 0) continue;
int maxHop = 2, minTimes = 1;
for (int j = 1; i - j >= 0; j++)
if (i < 3 && i - j == 0 || dp[i - j, j - 1] > 0){
dp[i, j - 1] = dp[i, j] = dp[i - j, j - 1] + 1;
max... 阅读全帖
x****1
发帖数: 118
5
来自主题: JobHunting版 - FLAG干货:
Linkedin
phone1:烙印
lowest common ancestor w/ and w/o parent pointer
phone2:国人
search in rotated sorted array
onsite:
1.两个国人
implement addInterval(start, end) and getcoverage(),
2.两个国人
talk projects and some behavior question
3.烙印
lunch, talk about technologies interest
4.亚裔,不确定是否国人
Manager, talked a lot of behavior questions, interest and projects
5.烙印
Design: tinyurl
6.烙印+小白
1.exclusive array, give an arr1, return a new arr2, arr2[i] is the
multiplication of all elements in arr1 except arr1[i]
... 阅读全帖
m******e
发帖数: 82
6
来自主题: JobHunting版 - 问一道fb的面试题目
#include
using namespace std;
int GetSingleOperand(char* &expr) {
int num = 0;
while (*expr >= '0' && *expr <= '9') {
num = 10 * num + *expr - '0';
expr++;
}
return num;
}
int GetOperand(char* &expr) {
int num = GetSingleOperand(expr);
while (*expr != '\0') {
if (*expr == '*') {
expr++;
num *= GetSingleOperand(expr);
} else if (*expr == '/') {
expr++;
num /= GetSingleOperand(expr);
... 阅读全帖
a***e
发帖数: 413
7
来自主题: JobHunting版 - 请问大牛们leetcode上的Permutations II
再问一下 , 对于无重复的permutation, 这个答案好理解。但怎么改来处理有重复数
的问题呢?
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
vector > permute(vector& num) {
sort(num.begin(), num.end());
vector> result;
vector path;
dfs(num, path, result);
return result;
}
private:
void dfs(const vector& num, vector &path,vector > &
result) {
if (path.siz... 阅读全帖
T*******e
发帖数: 4928
8
算了,给你贴一个: http://www.itint5.com/oj/#26
int getNumber(const string &expr, int &pos){
int num=0;
while(expr[pos]&&expr[pos]<='9'&&expr[pos]>='0'){
num=num*10+expr[pos++]-'0';
}
return num;
}
//返回表达式expr的值
int evaluate(const string& expr) {
int res=0;
int size=expr.size();
if(size==0) return res;
vector v;
int pos=0;
v.push_back(getNumber(expr, pos));
while(pos char op=expr[pos++];
int num=getNumber(expr, pos);
... 阅读全帖
f******s
发帖数: 659
9
//no recursion, no string manipulation
//space O(1), time O(n)
boolean isPalindrome(int num){
if(num<0) return false;
int rev = 0; //reverse num
int num0 = num; //original value
while (num>0){
rev = rev * 10 + num % 10; //get the rightmost digit of num
num /= 10;
}
return rev==num0;
}
x******u
发帖数: 17
10
public class Solution {
public int candy(int[] ratings) {
int num[] = new int[ratings.length];

for (int i = 0; i < ratings.length - 1; i++) {
if (ratings[i+1] > ratings[i]) num[i+1] = num[i] + 1;
}

for (int i = ratings.length - 1; i > 0; i--) {
if (ratings[i-1] > ratings[i]) num[i-1] = Math.max(num[i]+1, num
[i-1]);
}

int sum = 0;
for (int i = 0; i < num.length; i++)
sum += 1 +... 阅读全帖
a***e
发帖数: 413
11
我不知道这种情况怎么继续努力啦,只能看看别人的答案?我觉得自己的思路是对的啊,
。。。有人碰到类似情况吗?
Last executed input:
["My","momma","always","said,",""Life","was","like","a","box
","of","chocolates.","You","never","know","what","you're","gonna","get."],
12
Given an array of words and a length L, format the text such that each line
has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words
as you can in each line. Pad extra spaces ' ' when ... 阅读全帖
S*******C
发帖数: 822
12
import java.util.Stack;
/*
* Given Tree and Node n and int k, print all node which are at physical
distance <=k from n
* @Amazon intern
*/
public class Solution {
public static void main(String args[]){
Solution solution = new Solution();
TreeNode a = new TreeNode(8);
TreeNode b = new TreeNode(6);
TreeNode c = new TreeNode(10);
TreeNode d = new TreeNode(9);
TreeNode e = new TreeNode(12);
TreeNode f = new TreeNode(4);
TreeNode ... 阅读全帖
A*******e
发帖数: 2419
13
来自主题: JobHunting版 - LC的3sum谁有简洁代码?
不可能吧。这个python版都要20行了。
2/3/4-sum实际每个都有三个版本:
判断有没有解,只需找一组。
找所有唯一解
找所有解。
看了一下,我的版本比较长,是因为找所有解,LC只需要所有唯一解,这样可简化一些。
class Solution:
# @return a list of lists of length 3, [[val1,val2,val3]]
def threeSum(self, num):
num.sort()
result = []
for i in range(len(num)):
twoSumTuples = self.twoSum(num[i+1:], 0 - num[i])
if twoSumTuples:
for pair in twoSumTuples:
threeSum = [num[i]] + pair
if threeS... 阅读全帖
l******s
发帖数: 3045
14
来自主题: JobHunting版 - 请教一道经典的面试真题
try this. The idea is from that for each N, the minimum set of nums are from
1 to x, where x is to make sure Sigma(1,x) == N or just greater than N.
public int minAddition(int N, int[] nums)
{
int i = 1, sum = 0, result = 0;
for(i = 0; sum < N; i++) sum += (result = i + 1);
Array.Sort(nums);
for(int j = 0; j < nums.Length; j++)
if((j == 0 || (j > 0 && nums[j] != nums[j-1])) && nums[j] <= i &&
nums[j] > 0)
result--;
return result;
}
l******s
发帖数: 3045
15
private static int divideBy7(int num){
if (num == int.MinValue) return -divideBy7(int.MaxValue);
if (num < 0) return -divideBy7(-num);
int result = 0;
while (num >= 7){
int subResult = 1, div = 7;
while (((div << 3) | 7) > 0 && num >= ((div << 3) | 7)){
div = (div << 3) | 7;
subResult = subResult << 3 | 1;
}
while ((div << 1) > 0 && num >= (div << 1)){
div <<= 1; subResult <<= 1;
}
num -= div; res... 阅读全帖
k*****u
发帖数: 136
16
来自主题: JobHunting版 - 请问一道题:leetcode 416题的扩展
写416的解法,
bool canPartition(vector& nums) {
int sum = 0;
for(auto num:nums){
sum+= num;
}

if(sum == 0 || sum%2 ==1) return false;

int target = sum/2;
unordered_map pre;
pre[0] = true;

for(auto num:nums){
unordered_map now;
for(auto it: pre){
now[it.first+num] = true;
now[it.first] = true;
if(it.fi... 阅读全帖
a***e
发帖数: 413
17
【 以下文字转载自 JobHunting 讨论区 】
发信人: abcee (abcee), 信区: JobHunting
标 题: Re: 请问大牛们leetcode上的Permutations II
发信站: BBS 未名空间站 (Fri Jun 13 18:00:00 2014, 美东)
再问一下 , 对于无重复的permutation, 这个答案好理解。但怎么改来处理有重复数
的问题呢?
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
vector > permute(vector& num) {
sort(num.begin(), num.end());
vector> result;
vector pat... 阅读全帖
a*******y
发帖数: 105
18
我也不明白为啥不对, 难道还要排序?
select p1.num, p1.company, p1.name, p2.num, p2.company from
(SELECT distinct stopb.name, a.company, a.num
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart') as p1
join
(SELECT distinct stopa2.name, a2.company, a2.num
FROM route a2 JOIN route b2 ON
(a2.company=b2.company AND a2.num=b2.num)
JOIN stops stopa2 ON (a2.stop=stopa2.id)
JOIN stops... 阅读全帖
W********n
发帖数: 254
19
来自主题: DotNet版 - C#版ruby太慢了
原帖在这
http://my.oschina.net/iveryang/blog/125302
还有这
http://www.oschina.net/code/snippet_186034_20776
用C# TPL算了一下只要0.28秒
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
TPL(10000000);
Console.WriteLine("TPL elapsed: " + sw.Elapsed);
sw.Restart();
NonTPL(10000000);
Console.WriteLine("Non TPL elapsed: " + sw.Elapsed);
Console.Read();
}
static void Non... 阅读全帖
c******f
发帖数: 243
20
来自主题: Programming版 - 问个java8问题
在学java 8,用Java做不出来...哪里错了
同样的题 majority number..
scala
val num = List(1,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,2,3,4,5,6,7)
num.groupBy(x=>x).filter(_._2.size > num.length / 2).map(_._1).foreach(
println)
output:
1
java
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(... 阅读全帖
k*****y
发帖数: 744
21
来自主题: Quant版 - 问一个面试题,关于概率的
int main()
{
const int N = 63;
const int MAX = N + 6;
double prob[ MAX+1 ];
double ans[ MAX+1 ];
double visit[ MAX+1 ];
for( int ith=0; ith<=MAX; ++ith ){
prob[ ith ] = ( ith>=1 && ith<=6 ) ? 1./6 : 0;
ans[ ith ] = 0; visit[ ith ] = 0;
}

for( int runtime=2; runtime<=MAX; ++runtime ){
for( int ith=MAX; ith>=0; --ith ){
if( ith <= N) prob[ ith ] = 0; // reset if the state is
transient

for( int num=1;... 阅读全帖
t******g
发帖数: 372
22
来自主题: Statistics版 - 关于在R中对字符数组进行比较
1> data(iris)
1> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1
...
1> identical(as.character(iris), as.vector(iris, mod='character'))
[1] TRUE
1> identical(as.charac... 阅读全帖
w*******y
发帖数: 60932
23
Radio Shack has an on-line and in-store clearance on GE Z-Wave products.
YMMV on in-store availability. Use the "Find It In Store" button to look up
local availability.
$10 off of $40 coupon:
http://view.ed4.net/v/WUDODZ/GDQ54/U1069AB/MPS33V/FORMATOVERRID
. SD Radio Shack coupon page.
GE 45606 Z-Wave Dimmer Switch Reg. $54.99, on sale $19.97. Lower than
these:
http://www.google.com/search?hl=en&tbs=shop:1,p_ord:pd&as_q=&as 45606&as_oq=&as_eq=&num=100&scoring=pd&as_occt=any&price1=&price2=&sh... 阅读全帖
b*****n
发帖数: 2324
24
来自主题: Military版 - est