Online Stock Span

股價跨幅

(持續更新)

Leetcode 901

Real-World Algorithms: A Beginner’s Guide CP1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class StockSpanner {
public:
StockSpanner() {

}

int next(int price) {
int span = 1;
while(!mStack.empty() && price >= mStack.top().first){
span += mStack.top().second;
mStack.pop();
}
mStack.emplace(price, span);
return span;

}
private:
stack<pair<int, int>> mStack;
};

TwoSum

(持續更新)

意外很有意思的小題目,Leetcode天字號第一題

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class TwoSum {

public static int[] runTwoSum(int[] numbers, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
int complement;
for (int i = 0; i < numbers.length; i++) {
complement = target - numbers[i];
if (map.containsKey(complement)) {
return new int[]{i, map.get(complement)};
}
map.put(numbers[i], i);
}
throw new IllegalArgumentException("No Solution");
}

}

Test by JUnit5

1
2
3
4
5
6
7
8
@Test
public void twoSumTest() throws Exception {
int target = 6;
int[] numbers = {1, 2, 3, 4};
int[] result;
result = TwoSum.runTwoSum(numbers, target);
assertTrue(numbers[result[0]] + numbers[result[1]] == target);
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector<int32_t> TwoSum::runTwoSum(vector<int32_t>& numbers, int target) {
unordered_map<int32_t, int32_t> table;
vector<int32_t> result(2);
int complement = 0;
for (int32_t i = 0; i < (int32_t) numbers.size(); i++) {
complement = target - numbers[i];
if (table.count(complement) > 0) {
result[0] = i;
result[1] = table[complement];
return result;
}
table.insert( { numbers[i], i });
}
throw invalid_argument("No Solution");
}

Test by Catch2

1
2
3
4
5
6
7
8
9
10
TEST_CASE ("TwoSum Test") {
TwoSum twosum;
vector<int32_t> numbers{1,2,3,4};
int32_t target = 6;
vector<int32_t> answer{1,3};
vector<int32_t> result;
result = twosum.runTwoSum(numbers, target);
std::sort(result.begin(), result.end());
REQUIRE(result == answer);
}

不得不說真的有點意思
尤其是Catch2 跟JUnit5的部分
未來也許會用Mockitoi? blog上面更好讀
下一個更新應該會用 gradle&cmake把code包好 and gitgub release
下下個更新應該是 Python + Javascript(NodeJs)
下下下個更新應該是 Kotlin + Dart ??? not sure

Midlife Crisis

開始還願

償還欠網路世界的技術債

大概就是包山包海
從leetcode刷題面試
到我關心的技術
目前構想用youtuber模式
從各種主題開始一系列東西
基本上不會拍片(沒必要?)

順便玩一下hexo

例如
1
cout << "Hello hexo" << endl;

把他做成jupyter notebook的形狀