Jerry's Blog

Recording what I learned everyday

View on GitHub


25 July 2019

Angular (40) -- NgRx Devtools

by Jerry Zhang

LeetCode Day 20: P157. Read N Characters Given Read4 (Easy)

题目:

用一个写好的方法read4读一个文件,读n个字符,每次可以读四个字符。读完后存到buf里,并返回实际读取的字符数。

我的思路:

这个题表述很不清楚,有可能是故意需要面试者去充分沟通需求的。另外其实考察的是调用别人写过的函数的能力。实际的 业务逻辑非常简单,就是检查是否读到结尾了。

答案:

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Number of characters to read
     * @return    The number of actual characters read
     */
    public int read(char[] buf, int n) {
        char[] buffer = new char[4];//temp buffer for each call to read4()
        int read;//actual num of char read by the current call to read4()   	
        int hasRead = 0;//running total of char read
        while (true) {
            read = read4(buffer);//read4
            int toRead = Math.min(read, n-hasRead);//determine how many char to copy over to buf from buffer
            for (int i = 0; i < toRead; i++){//do the copy
                buf[hasRead++]=buffer[i];
            }
            if (read < 4) return hasRead;
            if (hasRead == n) return n;
        }
    }
}

Angular: Store Devtools

Install Redux DevTools

npm install --save-dev @ngrx/store-devtools
import {StoreDevtoolsModule} from '@ngrx/store-devtools';
import {environment} from '../environments/environment';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    StoreModule.forRoot(fromApp.appReducer),
    EffectsModule.forRoot([AuthEffects]),
    StoreDevtoolsModule.instrument({logOnly: environment.production}),
    SharedModule,
    CoreModule
  ],
  bootstrap: [AppComponent],
  providers: [LoggingService]
})
export class AppModule {
}

Then using this devtool, we can see what is happening when our application is running.

Install Router Store

npm install --save @ngrx/router-store

Then, Import the module:

import {StoreRouterConnectingModule} from '@ngrx/router-store';

StoreRouterConnectingModule.forRoot(),
tags: Angular