LeetCode (V)

Valid Palindrome

  Total Accepted: 4292  Total Submissions: 20224 My 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: 15036 My 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: 9931 My 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: 21723 My 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: 17621 My 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;
    }
};




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值