Skip to content

Instantly share code, notes, and snippets.

@yc0
yc0 / array_manipulation.sh
Created September 20, 2018 03:35
manipulation for array in Bash
#!/bin/bash
#assign variables into array
array=(Redhat Novell MicroSoft Sun IBM HP Dell)
#iterate all elements, and print them out
for i in 0 1 2 3 4 5 6
do
echo "array[$i]=${array[$i]}"
done
@yc0
yc0 / go-leetcode-675.go
Created September 10, 2018 16:04
675. Cut Off Trees for Golf Event in LeetCode
/*
675. Cut Off Trees for Golf Event
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:
0 represents the obstacle can't be reached.
1 represents the ground can be walked through.
The place with number bigger than 1 represents a tree can be walked through, and this positive number represents the tree's height.
You are asked to cut off all the trees in this forest in the order of tree's height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).
You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can't cut off all the trees, output -1 in that situation.
@yc0
yc0 / go-leetcode-146.go
Created September 8, 2018 12:22
146. LRU Cache in LeetCode
/*
146. LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
Follow up:
Could you do both operations in O(1) time complexity?
@yc0
yc0 / split.cpp
Last active September 7, 2018 08:06
split.cpp
void split(const std::string& s,
std::vector<std::string>& sv,
const char* delim = " ") {
// strtok and copy toward buffer.
sv.clear();
{
TIME_INTERVAL_SCOPE("strtok");
char* buffer = new char[s.size() + 1];
std::copy(s.begin(), s.end(), buffer);
char* p = std::strtok(buffer, delim);
@yc0
yc0 / time_interval.h
Created September 7, 2018 07:51
time_interval
class TimeInterval {
public:
TimeInterval(const std::string &msg):msg(msg) {
init();
}
TimeInterval() {
init();
}
~TimeInterval() {
end = clock();
@yc0
yc0 / benchmark.cpp
Last active September 7, 2018 07:52
String split Benchmark
void split(const std::string& s,
std::vector<std::string>& sv,
const char* delim = " ");
int main(int argc, char **argv) {
std::srand(static_cast<unsigned int>(time(NULL)));
std::vector<std::string> ve(8000000); // avoid resizing allocation
char data[4000000]={};
for (size_t i=0; i < 4000000; i=i+4) {
int rand = std::rand();
@yc0
yc0 / go-leetcode-48.go
Created September 6, 2018 07:07
48. Rotate Image in LeetCode
/*
48. Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
@yc0
yc0 / go-leetcode-239.go
Created August 29, 2018 03:13
239. Sliding Window Maximum in Leetcode
// 239. Sliding Window Maximum
// Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
// Example:
// Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
// Output: [3,3,5,5,6,7]
// Explanation:
@yc0
yc0 / go-leetcode-407.go
Last active August 27, 2018 16:12
407. Trapping Rain Water II in Leetcode
/**
public int trap(int[] height) {
int res = 0, l = 0, r = height.length - 1;
while (l < r) {
if (height[l] <= height[r]) {
if (l + 1 < r) {
res += Math.max(0, height[l] - height[l + 1]);
height[l + 1] = Math.max(height[l], height[l + 1]);
}
@yc0
yc0 / go-leetcode-139.go
Created August 1, 2018 15:05
139. Word Break in golang
// 139. Word Break
// Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
// Note:
// The same word in the dictionary may be reused multiple times in the segmentation.
// You may assume the dictionary does not contain duplicate words.
// Example 1: