21 July 2019
Angular (36) -- NgRx Effects
by Jerry Zhang
LeetCode Day 16: P125. Valid Palindrome (Easy)
题目:
给一个string, 判断是否是一个palindrome(回文),只考虑字母,忽略大小写
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
我的思路:
首先需要处理一下数据,去除非字母的字符并统一成小写字母。然后按照一般的回文算法即可。
我的代码:
public class E_125_ValidPalindrome {
public static boolean isPalindrome(String s) {
if (s == null || s.equals("")) {
return true;
}
char[] string = s.toCharArray();
int j = 0;
for (int i = 0; i < string.length; i++) {
if (string[i] >= 65 && string[i] <= 90) {
string[j] = (char) (string[i] + 32);
j++;
} else if (string[i] >= 97 && string[i] <= 122 || string[i] >= 48 && string[i] <= 57) {
string[j] = string[i];
j++;
}
}
int k = j - 1;
for (int i = 0; i < j / 2; i++) {
if (string[i] != string[k]) {
return false;
}
k--;
}
return true;
}
public static void main(String[] args) {
boolean palindrome = isPalindrome("A man, a plan, a canal: Panama");
System.out.println("palindrome = " + palindrome);
boolean race_a_car = isPalindrome("race a car");
System.out.println("race_a_car = " + race_a_car);
boolean op = isPalindrome("0P");
System.out.println("0P = " + op);
}
}
faster than 99.41%
坑:
一开始没仔细看题,题目要求是字母或数字,我只考虑了字母的情况。
最优解:
class Solution {
private static final char[]charMap = new char[256];
static{
for(int i=0;i<10;i++){
charMap[i+'0'] = (char)(1+i); // numeric
}
for(int i=0;i<26;i++){
charMap[i+'a'] = charMap[i+'A'] = (char)(11+i); //alphabetic, ignore cases
}
}
public boolean isPalindrome(String s) {
char[]pChars = s.toCharArray();
int start = 0,end=pChars.length-1;
char cS,cE;
while(start<end){
cS = charMap[pChars[start]];
cE = charMap[pChars[end]];
if(cS!=0 && cE!=0){
if(cS!=cE)return false;
start++;
end--;
}else{
if(cS==0)start++;
if(cE==0)end--;
}
}
return true;
}
}
Angular: NgRx Effects
Error Handling
In the auth.action.ts
file, export a new constant, AUTHENTICATE_FAIL. Also create a new class AuthenticateFail.
export const AUTHENTICATE_FAIL = '[Auth] Login Fail';
export class AuthenticateFail implements Action {
readonly type = AUTHENTICATE_FAIL;
constructor(public payload: string) {}
}
In the auth.reducer.ts
file, we need to add a new state, authError
.
export interface State {
user: User;
authError: string;
loading: boolean;
}
const initialState: State = {
user: null,
authError: null,
loading: false
};
In the authReducer function, when we login successfully, we need to set authError
back to null in case it was
something different. We need to do the same when we start login. So we need a new case LOGIN_START
, which we have
created in my last blog.
case AuthActions.LOGIN_START:
return {
...state,
authError: null,
loading: true
};
Do not forget add the new actions in the union type:
export type AuthActions =
| AuthenticateSuccess
| Logout
| LoginStart
| AuthenticateFail
| SignupStart
| ClearError
| AutoLogin;
In the fail case, we should set the user to null, and set authError to the payload, and set loading to false.
case AuthActions.AUTHENTICATE_FAIL:
return {
...state,
user: null,
authError: action.payload,
loading: false
};
When we are done logging in, we should also set loading to false.
case AuthActions.AUTHENTICATE_SUCCESS:
const user = new User(
action.payload.email,
action.payload.userId,
action.payload.token,
action.payload.expirationDate);
return {
...state,
authError: null,
user: user,
loading: false
};
In the auth.component.ts
, subscribe the authState.
ngOnInit(): void {
this.storeSub = this.store.select('auth').subscribe(authState => {
this.isLoading = authState.loading;
this.error = authState.authError;
if (this.error) {
this.showErrorAlert(this.error);
}
});
}