您的位置:首页 > 其它

LeetCode (V)

2014-01-02 19:14 218 查看


Valid Palindrome

 Total Accepted: 4292 Total
Submissions: 20224My Submissions

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
 is a palindrome.
"race a car"
 is not a palindrome.
Note:

Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.

class Solution {
public:
bool isPalindrome(string s) {
string t;
for (int i = 0; s[i]; ++i) {
if (isalpha(s[i]))
t += tolower(s[i]);
else if (isdigit(s[i]))
t += s[i];
}
for (int i = 0, j = t.length() - 1; i < j; ++i, --j) {
if (t[i] != t[j])
return false;
}
return true;
}
};



Valid Parentheses

 Total Accepted: 4145 Total
Submissions: 15036My Submissions

Given a string containing just the characters 
'('
')'
'{'
'}'
'['
 and 
']'
,
determine if the input string is valid.
The brackets must close in the correct order, 
"()"
 and 
"()[]{}"
 are
all valid but 
"(]"
 and 
"([)]"
 are
not.

class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for (int i = 0; s[i]; ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
stk.push(s[i]);
else {
if (stk.size() > 0 && (
stk.top() == '(' && s[i] == ')' ||
stk.top() == '{' && s[i] == '}' ||
stk.top() == '[' && s[i] == ']'))
stk.pop();
else
return false;
}
}
if (stk.size() == 0)
return true;
else
return false;
}
};



Valid Sudoku

 Total Accepted: 2635 Total
Submissions: 9931My Submissions

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character 
'.'
.



A partially filled sudoku which is valid.

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
vector<bool> f;
for (int i = 0; i < 9; ++i) {
f.clear();
f.resize(9, false);
for (int j = 0; j < 9; ++j) {
if (board[i][j] == '.') continue;
if (f[board[i][j] - '1']) return false;
f[board[i][j] - '1'] = true;
}
}
for (int i = 0; i < 9; ++i) {
f.clear();
f.resize(9, false);
for (int j = 0; j < 9; ++j) {
if (board[j][i] == '.') continue;
if (f[board[j][i] - '1']) return false;
f[board[j][i] - '1'] = true;
}
}
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
f.clear();
f.resize(9, false);
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
if (board[i + x][j + y] == '.') continue;
if (f[board[i + x][j + y] - '1']) return false;
f[board[i + x][j + y] - '1'] = true;
}
}
}
}
return true;
}
};



Valid Number

 Total Accepted: 2175 Total
Submissions: 21723My Submissions

Validate if a given string is numeric.
Some examples:
"0"
 => 
true

" 0.1 "
 => 
true

"abc"
 => 
false

"1 a"
 => 
false

"2e10"
 => 
true


Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

class Solution {
public:
bool isNumber(const char *s) {
char *a = (char *)s;
bool flag = false;
while (isspace(*a))
++a;
if (*a == '+' || *a == '-')
++a;
while (isdigit(*a))
++a, flag = true;
if (*a == '.') {
++a;
while (isdigit(*a))
++a, flag = true;
}
if (*a == 'e') {
if (!flag) return false;
++a;
if (*a == '+' || *a == '-')
++a;
char *b = a;
while (isdigit(*a))
++a, flag = true;
if (a == b) return false;
}
while (isspace(*a))
++a;
if (flag && *a == '\0')
return true;
else
return false;
}
};



Validate Binary Search Tree

 Total Accepted: 4455 Total
Submissions: 17621My Submissions

Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.

/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
void visit(TreeNode *root, vector<int> &a) {
if (root == NULL) return;
visit(root->left, a);
a.push_back(root->val);
visit(root->right, a);
}
public:
bool isValidBST(TreeNode *root) {
vector<int> a;
visit(root, a);
const int n = a.size();
for (int i = 1; i < n; ++i)
if (a[i] <= a[i - 1])
return false;
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: