c**********e 发帖数: 2007 | 1 这个Strategy design pattern的例子为什么人为得弄得这么复杂?
#include
#include
#include
using namespace std;
class Strategy;
class TestBed
{
public:
enum StrategyType
{
Dummy, Left, Right, Center
};
TestBed()
{
strategy_ = NULL;
}
void setStrategy(int type, int width);
void doIt();
private:
Strategy *strategy_;
};
class Strategy
{
public:
Strategy(int width): width_(width){}
void format()
{
char line[80], wo... 阅读全帖 |
|
z****e 发帖数: 54598 | 2 老外评出的singleton
最优是
enum //改动两个单词,解决所有问题,包括serializable,这个在后面三种方式中都
需要额外处理
次优的是
inner class //仅仅为了lazy load,很多人觉得毫无必要
和
static final = new //这个是最常用的,inner class那个介入了虚拟机加载类的机制
最糟糕的是
double check+volatile //代码又长又tricky,要写一篇长文分析汇编代码来解释为什
么用double check,还需要留意volatile关键字,毫无必要,而且最后效率还被
volatile拖下来了,弄巧成拙
singleton模式就是一堆keywords的乐章 |
|
h****e 发帖数: 928 | 3 是的,可以在你的class里面加上这个:
enum TYPE {BUY, SELL};
static long call_count[SELL+1]; |
|
r*****d 发帖数: 1924 | 4 【 以下文字转载自 WashingtonDC 讨论区 】
发信人: Westridge (西岭), 信区: WashingtonDC
标 题: Java开发人员知识点(更新)
发信站: BBS 未名空间站 (Wed Apr 18 00:03:19 2012, 美东)
Java开发人员知识点
1.听说过James Gosling,SUN和Oracle公司。知道网上下载Java的地址,在哪讨论Java
。练习过Java在Windows下的安装和配置。知道Java应用系统中常见的几种license和JCP。了
解bytecode和Java在不同系统下可以轻松移植的原理。
2.懂得基本的Java编程和行命令格式。了解面向对象的编程思路。
几个基本点:Java基本语法和控制结构,命名和代码风格,结构化,对象封装,继承,
抽象,多态,接口,异常处理,堆空间,栈空间,垃圾回收器,static,this,
synchronized,annotations,JUnit,JDBC,JSP/servlet
Java Core APIs: java.lang,java.util,java.io,java.a... 阅读全帖 |
|
p*****2 发帖数: 21240 | 5
给个简单的例子。我Java不熟。Enum从来没用过呀。 |
|
y**********u 发帖数: 6366 | 6 Enum也得自己定义啊
如果你不介意比较难看的话,随便把你要改的参数放入一个container里头就可以了,没
必要重新写类
传递 |
|
a********d 发帖数: 195 | 7 1. String的hash func,用那个(a[0]*31+a[1])*31...,然后自然unit overflow,用
bitmap来记位的那个题。
请问和bloomfilter(用几个bitmap和几个hash func来标位)哪个好?怎么比较(或者
说怎么忽悠面试官)?
2. OO parking lot
现在收费了!设计一个收费的parking lot,parking spot按大小收费,有enum的大中
小。哪个达人帮忙在150的基础上给指点一下收费这个应该怎么搞?
class tollMachine
class InTollMachine:tollMachine
class OutTollMachine:tollMachine
class Tollticket{in/out machine...}
class ParkingMngment{}
主要是整个的流程是怎样的?这些东西都compose到parkingMngment类里?哪些方法放
在哪些类里?
3.老问题,nextIterator of in order tree
好像给的都是保留当前的状态在类里面。
如果给... 阅读全帖 |
|
g***j 发帖数: 1275 | 8 Implement the “paint fill” function that one might see on many image
editing programs. That is, given a screen (represented by a 2-dimensional
array of Colors), a point, and a new color, fill in the surrounding area
until you hit a border of that color.
我看到最近版上不好题目提到了 150上提到的答案如下,我有几个疑问
1 什么叫“until you hit a border of that color”
2 为什么只考虑四个点?不是8个点?8个点有重复的吧?
3 递归的时候这四个点的顺序不一样,会有问题么?
We can implement this algorithm recursively:
1 enum Color {
2 Black, White, Red, Yellow, Green
3 }
4 boolean Paint... 阅读全帖 |
|
w****x 发帖数: 2483 | 9 design一个vending machine.
class VendingMachine
{
public:
....
bool getBeverage(int nType);
private:
vector m_beverages;
};
class Beverage;
class Coca : public Beverage;
class Water : public Beverage;
VendingMachine里有一个bool getBeverage函数。要制定饮料类型获取饮料。
因为m_beverages里都是基类, 所以需要知道类型, 所以在Beverage里加了一个type
的enum 成员. 然后在getBeverage里遍历容器看基类的type flag是否和nType一致。
这样做似乎违背了OOD原则, 但是好像也没什么更好的方法, 到底因该怎么处理? |
|
|
t******k 发帖数: 64 | 11 Why they have this enum in the first place? They already made a design
decision for you. It is therefore a reasonable question to ask why they did
so. Likely they will say, this is in response to, for example, a customer
press a button. Then, in that case, let each subclass of Beverage respond to
a representation of that button. |
|
s***0 发帖数: 117 | 12 class VendingItem
{
public:
typedef boost::shared_ptr Ptr;
enum ItemName
{
CocaCola;
Chips;
Cookies;
Condoms;
//etc
};
const unsigned int mPriceInCents;
const unsigned int getPrice() const {return mPriceInCents;};
}
class Cookies : public VendingItem
{
public:
Cookie(ItemName itemname) :
mPriceInCents(priceInCents){} // get priceInCents from some other
rule.
};
// A Rule is a class that takes 2 inputs
//... 阅读全帖 |
|
e****e 发帖数: 418 | 13 Show Ugly
class VendingMachine {
public boolean getItem(ItemType it) {
return Arrays.asList( ItemType.values() ).contains( it );
}
public float getPrice(ItemType it) {
return it.getPrice();
}
}
enum ItemType {
Coke (1.75f),
Water (1.5f),
Chip (1.2f);
private float price;
ItemType (float price) {
this.price = price;
}
public float getPrice() {
return this.price;
}
}
implement
are
of |
|
w****x 发帖数: 2483 | 14 design一个vending machine.
class VendingMachine
{
public:
....
bool getBeverage(int nType);
private:
vector m_beverages;
};
class Beverage;
class Coca : public Beverage;
class Water : public Beverage;
VendingMachine里有一个bool getBeverage函数。要制定饮料类型获取饮料。
因为m_beverages里都是基类, 所以需要知道类型, 所以在Beverage里加了一个type
的enum 成员. 然后在getBeverage里遍历容器看基类的type flag是否和nType一致。
这样做似乎违背了OOD原则, 但是好像也没什么更好的方法, 到底因该怎么处理? |
|
|
t******k 发帖数: 64 | 16 Why they have this enum in the first place? They already made a design
decision for you. It is therefore a reasonable question to ask why they did
so. Likely they will say, this is in response to, for example, a customer
press a button. Then, in that case, let each subclass of Beverage respond to
a representation of that button. |
|
s***0 发帖数: 117 | 17 class VendingItem
{
public:
typedef boost::shared_ptr Ptr;
enum ItemName
{
CocaCola;
Chips;
Cookies;
Condoms;
//etc
};
const unsigned int mPriceInCents;
const unsigned int getPrice() const {return mPriceInCents;};
}
class Cookies : public VendingItem
{
public:
Cookie(ItemName itemname) :
mPriceInCents(priceInCents){} // get priceInCents from some other
rule.
};
// A Rule is a class that takes 2 inputs
//... 阅读全帖 |
|
e****e 发帖数: 418 | 18 Show Ugly
class VendingMachine {
public boolean getItem(ItemType it) {
return Arrays.asList( ItemType.values() ).contains( it );
}
public float getPrice(ItemType it) {
return it.getPrice();
}
}
enum ItemType {
Coke (1.75f),
Water (1.5f),
Chip (1.2f);
private float price;
ItemType (float price) {
this.price = price;
}
public float getPrice() {
return this.price;
}
}
implement
are
of |
|
h****n 发帖数: 1093 | 19 写了一个,不过废了挺长时间,不知道要写详细到什么程度
enum STATE{MAINTENANCE,STAND,UP,DOWN};
public class Elevator
{
private:
STATE state;
int currentFloor;
priority_queue, greater> UpFloorReqList;
priority_queue DownFloorReqList;
public:
Elevator(STATE st, int floor)
{
state = st;
floor = currentFloor;
}
STATE GetState()
{
return state;
}
void SetState(STATE newst)
{
state = newst;
}
int GetCurrentFloor()
{
return currentFloor;
}
void SetCurrentFloor(int floor)
{
currentFloor = floor;
}
void Ad... 阅读全帖 |
|
|
d**e 发帖数: 6098 | 21 近日用Office Word的track change用得比较多,突然想知道它是怎么实现得。
我的想法是每个page是一个grid,每个点都有一个数据,里面包含原正文内容,修改的
内容,然后再beautiful display.
enum Change {
DELETE,
ADD,
}
class Point {
int x;
int y;
}
class Data {
Point position;
String content;
Map edit;
}
class Page {
Vector contents;
}
class Word {
Vector pages;
}
boolean dispaly(class Word) {
// render file here....
return true;
}
这个方法行不行?其它的方法?
谢谢。 |
|
U***5 发帖数: 2796 | 22 一边看球一边草草写了写,corner case没时间搞了,只写了头2个search的情况:
小于key的最大值 - op_max_less
大于key的最小值 - op_min_great
其他几种情况自己加Functor就是了。感觉myfind return那里比较繁琐,应该有更好办
法整理一下。
写得不好,轻拍。
//Compare.h
enum Op_code
{
op_max_less = 0,
op_min_great
};
class Less
{
public:
Less(int v): val_(v), op_(op_max_less){}
bool operator()(int v1)
{
return v1 < val_;
}
const Op_code op_;
private:
int val_;
};
class Greater
{
public:
Greater(int v): val_(v), op_(op_min_great){}
bool opera... 阅读全帖 |
|
J**9 发帖数: 835 | 23 How about this direction?
=====================================
/// array is sorted in Ascending order
int binarySearchAscending(int array[], int key, int low, int high)
{
int mid;
if(!array|| (low>high))
return -1;
while(high >= low)
{
mid = low+(high-low)/2;
if(key < array[mid])
high = mid-1;
else if (key > array[mid])
low = mid+1;
else
return mid;
}
return -1;
}
/// array is sorted in D... 阅读全帖 |
|
t****t 发帖数: 6806 | 24 #include
#include
enum search_type { Exact, MaxLess, MinGreater, MaxLessEqual, MinGreaterEqual
};
/* returns "next" element if key was to be inserted. */
template
typename Container::const_iterator
search_place(const Container& array, typename Container::value_type key,
bool ascending)
{
if (ascending) {
return lower_bound(array.begin(), array.end(), key, std::less<
typename Container::value_type>());
} else {
return lower_bou... 阅读全帖 |
|
s**********1 发帖数: 58 | 25 我也贴一个,没有上面lucky的简单,不过可以改成binary search,lucky的那个应该
不好改
主要就是写了个iterator
enum FindType {
Lesser,
LesserEqual,
Equal,
GreaterEqual,
Greater
};
class Iterator {
public:
Iterator(const vector& array)
:array(array)
{
assert(array.size());
ascending = array.front() <= array.back();
offset = ascending ? 0 : array.size() - 1;
}
bool can_step(bool forward) {
int new_offset = offset + ascending == forward ? 1 : -1;
retu... 阅读全帖 |
|
e****e 发帖数: 418 | 26 I'm not sure about "节约空间,节约时间是个什么做法?".
Here are my classes.
enum Role{
BOSS,
VP,
DIR,
MANAGER,
...
}
class Employee{
String id;
Role role;
List managed;
}
Maybe another class keep track of all references to the vips, dirs, managers
... so we can get them from this class by a single get method instead of
traversing from Boss employee. Like
class QuickRetrival {
List vips;
List dirs;
List managers;
...
} |
|
c********r 发帖数: 286 | 27 用enum实现singleton好像更简单方便些,不知道gate大侠在实际应用中是否用过,感
觉如何 |
|
c********r 发帖数: 286 | 28 enum还是相对来讲比较新,
不知道实际应用中有没有什么弊端
hack |
|
m*****n 发帖数: 204 | 29
public class ElementsImpl implements IElements {
private final int batchSize;
private final AtomicInteger counter = new AtomicInteger();
ElementsImpl(int i) {
batchSize = i;
}
public int getBatchSize() {
return batchSize;
}
@Override
public void inc() {
counter.incrementAndGet();
}
@Override
public boolean reserve() {
int val = co... 阅读全帖 |
|
m*****n 发帖数: 204 | 30
public class ElementsImpl implements IElements {
private final int batchSize;
private final AtomicInteger counter = new AtomicInteger();
ElementsImpl(int i) {
batchSize = i;
}
public int getBatchSize() {
return batchSize;
}
@Override
public void inc() {
counter.incrementAndGet();
}
@Override
public boolean reserve() {
int val = co... 阅读全帖 |
|
p*****2 发帖数: 21240 | 31 先说CC150上的Singleton吧。
三个bug
1. _instance应该是static的
2. 应该定义private constructor
3. multi threading
再提两点:
1. 这个例子还包含了lazy initialization pattern
2. 这是老的Java Singleton的实现方式,新的方式是通过Enum来实现的 |
|
p*****2 发帖数: 21240 | 32
书里边把优点都说了吧,说了一大堆。话说enum经常要继承吗?我怎么觉得一般没必要
继承?
singleton有link吗?你以前说过的理由。 |
|
p*****2 发帖数: 21240 | 33 感觉没什么意思,随便写了一下。觉得难点是如何匹配taxi。比如要考虑taxi的当前位
置,状态,如果载人,什么时候能idle。而且还要考虑traffic情况,customer也可能
会改变当前request的destination什么的。还有要检查是否有灵异事件,比如进去两个
,出来一个什么的。
class Customer{
ID, name, address, etc.
type {maybe enum regular, premium etc}
credit card
book()
}
class Request{
customer
from
to
number of people
number of taxi
}
class Driver{
}
class Taxi{
status {on order, idle, etc.}
driver
location
request
takerequest()
donerequest()
}
BookSyste... 阅读全帖 |
|
p*****p 发帖数: 379 | 34 刚写了个,上个部分代码
private enum Direction {LEFT, RIGHT, UP, DOWN};
private static float getCurrentPathMax(int currentRow, int currentCol, int[]
[] maze, int[][] visited, Direction lastStep) {
int rows = maze.length, cols = maze[0].length;
if (currentRow < 0 || currentRow >= rows || currentCol < 0 || currentCol
>= cols) {
return -1;
}
if (maze[currentRow][currentCol] == 0) {
// blank
if (visited[currentRow][currentCol] == 3) {
return 0;
... 阅读全帖 |
|
d****n 发帖数: 1241 | 35 可以使用编译器在lexing和parsing的时候用的某种技术:recursive descent parsing
http://en.wikipedia.org/wiki/Recursive_descent_parser
下边的代码假设单个的数字也是合法的,比如:
1 合法
(1)合法
(1) + (2) 合法, 等等
#include
#include
#include
#include
using namespace std;
enum TokenKind {
TOK_Unknown = -1,
TOK_End,
TOK_Op,
TOK_LParen,
TOK_RParen,
TOK_Num,
};
static TokenKind getToken(const string &Input, int &Pos)
{
assert(Pos <= Input.length());
char TheChar = Input[Pos];
while (isspace(TheC... 阅读全帖 |
|
d****n 发帖数: 1241 | 36 可以使用编译器在lexing和parsing的时候用的某种技术:recursive descent parsing
http://en.wikipedia.org/wiki/Recursive_descent_parser
下边的代码假设单个的数字也是合法的,比如:
1 合法
(1)合法
(1) + (2) 合法, 等等
#include
#include
#include
#include
using namespace std;
enum TokenKind {
TOK_Unknown = -1,
TOK_End,
TOK_Op,
TOK_LParen,
TOK_RParen,
TOK_Num,
};
static TokenKind getToken(const string &Input, int &Pos)
{
assert(Pos <= Input.length());
char TheChar = Input[Pos];
while (isspace(TheC... 阅读全帖 |
|
r*c 发帖数: 167 | 37 //矩阵求连接图
#include
#include
#include
#include
#include
using namespace std;
enum ColorEnum{White, Black};
struct Cell
{
int row;
int col;
Cell(int r, int c): row(r), col(c){}
};
struct Cluster{
bool visited;
vector vec;
Cluster(int i, int j) : visited(false) {
vec.push_back(Cell( i, j));
}
bool isWithinRange(const Cell& a, const Cell& b){
return abs(a.row - b.row) <= 1 ... 阅读全帖 | |
|
r*c 发帖数: 167 | 38 //矩阵求连接图
#include
#include
#include
#include
#include
using namespace std;
enum ColorEnum{White, Black};
struct Cell
{
int row;
int col;
Cell(int r, int c): row(r), col(c){}
};
struct Cluster{
bool visited;
vector vec;
Cluster(int i, int j) : visited(false) {
vec.push_back(Cell( i, j));
}
bool isWithinRange(const Cell& a, const Cell& b){
return abs(a.row - b.row) <= 1 ... 阅读全帖 | |
|
|
z****e 发帖数: 54598 | 40 enum在你要继承某个class时候就不管用了 |
|
d****n 发帖数: 233 | 41 这道题直接打印总是容易出错, 用状态机就简单多了。
class Solution {
public:
enum Direct{
Right,
Down,
Left,
Up
};
void print(const vector > &matrix, int top, int bottom, int
left, int right, Direct dir, vector & output){
if (top > bottom || left > right) return;
switch(dir) {
case Right:
for(int i = left; i <= right; i++)
output.push_back(matrix[top][i]);
print(matrix, top+1, bottom, left, right, ... 阅读全帖 |
|
|
|
x*********n 发帖数: 100 | 44 做BFS,每个点都设个enum,表示 none, leftOnly, rightOnly, both
这样下次碰到就能剪肢了。
divider |
|
l*n 发帖数: 529 | 45 你这些问题写个Coin的类就搞定了
enum Currency {US, CN, JP, ...}
class Coin {
int value;
Currency cur;
double vol;
}
堆叠应该不用处理,毕竟罐子本身是方形还是圆形都没定义。 |
|
f********a 发帖数: 165 | 46 大概意思是有3个门,1个后面是prize,2个后面是goat, contestant选择一个门,然后
host在剩下的两个门选择一个goat门打开,然后contestant可以选择switch门,问题是
实现一个function:
bool play_game(bool contestant_will_switch_doors)
输入是true就是换门,false就是不换门,return是true就是最后选择到了prize那个,
false就是选择到了goat那个。很纠结 这个换门不换门其实最后都是50%的几率拿到
prize。这是我的实现,没能做完。哪位能否实现这个function以及整个游戏。
enum Door
{
goat,
prize
};
class Game
{
private:
vector vec;
int m_size;
public:
Game(int size){
m_size = size;
srand(time(NULL));
int ran = rand() % m... 阅读全帖 |
|
f********a 发帖数: 165 | 47 大概意思是有3个门,1个后面是prize,2个后面是goat, contestant选择一个门,然后
host在剩下的两个门选择一个goat门打开,然后contestant可以选择switch门,问题是
实现一个function:
bool play_game(bool contestant_will_switch_doors)
输入是true就是换门,false就是不换门,return是true就是最后选择到了prize那个,
false就是选择到了goat那个。很纠结 这个换门不换门其实最后都是50%的几率拿到
prize。这是我的实现,没能做完。哪位能否实现这个function以及整个游戏。
enum Door
{
goat,
prize
};
class Game
{
private:
vector vec;
int m_size;
public:
Game(int size){
m_size = size;
srand(time(NULL));
int ran = rand() % m... 阅读全帖 |
|
w***0 发帖数: 30 | 48 没有用内部类,没有用enum,没有调用任何fancy的函数。也没有static变量。
在eclipse中,手动测试结果是正确的。因此,不是编译错误。
输入是“a" [“a","a"]-------->因此也不可能是复杂度太高。
FYI,除了给定的函数,仅有自己建的一个小函数。(应该不是这个的问题)
boolean isAllTrue(boolean[] blnA){
// return true if and only if all elements in blnA are true
for(int i =0;i
if( !blnA[i] )return false;
}
return true;
} |
|
|
a**********s 发帖数: 588 | 50 // Say you have the following
typedef enum {
red,
yellow,
blue
} Color;
const Color expectedColors[] = {Red, Yellow, Blue};
// The main loop should be something like:
for (int i = 0; i < 3N; i++) {
Color expectedColor = expectedColors[i%3];
if (getColor(i) != expectedColor) {
exchange(i, findNext(i+1, expectedColor));
}
}
int findNext(int start, Color expectedColor) {
while(getColor[start++] != expectedColor);
return start;
} |
|