1
1

2 Revīzijas 48a1586b0a ... 283d4e8e13

Autors SHA1 Ziņojums Datums
  邓心一 283d4e8e13 update 4 gadi atpakaļ
  邓心一 6c43c41f0d 749 WIP 4 gadi atpakaļ

+ 1 - 0
basis/concurrent/TestSemaphore.java

@@ -12,6 +12,7 @@ public class TestSemaphore {
                     semaphore.acquire();
                     System.out.println("Thread " + num + " start.");
                     Thread.sleep(100);
+                    System.out.println("Thread " + num + " end.");
                 } catch (Exception e) {
                     e.printStackTrace();
                 } finally {

+ 6 - 4
basis/concurrent/TestWaitAndNotify.java

@@ -1,7 +1,7 @@
 public class TestWaitAndNotify {
     public static void main(String[] args) {
-        WaitThread t1 = new WaitThread(1, 2, 50, false);
-        WaitThread t2 = new WaitThread(2, 2, 50, false);
+        WaitThread t1 = new WaitThread("t1", 1, 2, 50, false);
+        WaitThread t2 = new WaitThread("t2", 2, 2, 50, false);
         t1.start();
         t2.start();
     }
@@ -11,12 +11,14 @@ class MyLock {}
 
 class WaitThread extends Thread {
     private static MyLock lock = new MyLock();
+    private String name;
     private int init;
     private int delta;
     private int count;
     private boolean isLast;
 
-    WaitThread(int init, int delta, int count, boolean isLast) {
+    WaitThread(String name, int init, int delta, int count, boolean isLast) {
+        this.name = name;
         this.init = init;
         this.delta = delta;
         this.count = count;
@@ -26,7 +28,7 @@ class WaitThread extends Thread {
     @Override
     public void run() {
         for (int i = 0, num = init; i < count; i++) {
-            System.out.println(num);
+            System.out.println(name + ": " + num);
             num += delta;
             if (isLast && i == count - 1) return;
             synchronized (lock) {

+ 1 - 0
leetcode/hard/736.cc

@@ -86,6 +86,7 @@ int main() {
 
     cout << sol->evaluate("mult (add (add 1 3) 4) 5") << endl;
     cout << sol->evaluate("let x 3 y 9 x 1 x 4 (add x (let x 2 (add (add 1 x) 4)))") << endl;
+    cout << sol->evaluate("(let x 1 y 2 x (add x y) (add x y))") << endl;
 
     return 0;
 }

+ 64 - 0
leetcode/hard/749.cc

@@ -0,0 +1,64 @@
+#include <iostream>
+#include <vector>
+
+using std::vector;
+
+using std::cout;
+using std::endl;
+
+class Solution {
+public:
+    int containVirus(vector<vector<int>> &grid) {
+        int walls = 0;
+        do {
+            walls += build_wall(grid);
+        } while (spread(grid));
+        return walls;
+    }
+
+private:
+    int build_wall(vector<vector<int>> &grid) {
+        bool flag[grid.size()][grid[0].size()];
+        memset(flag, 0, sizeof(flag));
+        
+        return 0;
+    }
+
+    bool spread(vector<vector<int>> &grid) {
+        for (int i = 0; i < grid.size(); i++) {
+            vector<int> &col = grid[i];
+            for (int j = 0; j < col.size(); j++) {
+                for (int k = 0; k < 4; k++) {
+                    int x = i + dx[k], y = j + dy[k];
+                    if (x < 0 || grid.size() <= x || y < 0 || col.size() <= y) continue;
+                    if (grid[x][y] == 1 && grid[i][j] == 0) grid[i][j] = -1;
+                }
+            }
+        }
+        int cnt = 0;
+        for (vector<int> &col : grid) {
+            for (int &row : col) {
+                if (row == -1) {
+                    row = 1;
+                    cnt++;
+                }
+            }
+        }
+        return 0 < cnt;
+    }
+
+    int dx[4] = {-1, 0, 1, 0};
+    int dy[4] = {0, -1, 0, 1};
+};
+
+int main() {
+    vector<vector<int>> grid =
+        {{1, 1, 1, 0, 0, 0, 0, 0, 0},
+         {1, 0, 1, 0, 1, 1, 1, 1, 1},
+         {1, 1, 1, 0, 0, 0, 0, 0, 0}};
+    Solution* sol = new Solution();
+    
+    cout << "actual: " << sol->containVirus(grid) << ", expected: " << 13 << endl;
+
+    return 0;
+}