This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimeInterval { | |
public: | |
TimeInterval(const std::string &msg):msg(msg) { | |
init(); | |
} | |
TimeInterval() { | |
init(); | |
} | |
~TimeInterval() { | |
end = clock(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: |