zoukankan      html  css  js  c++  java
  • leetcode 较难题II

    1.solve数独

    dfs

    class Solution
    {
    public:
    	bool isValid(vector<vector<char> > &board, int x, int y)
    	{
    		int i, j;
    		for (i = 0; i < 9; i++)
    			if (i != x && board[i][y] == board[x][y])
    				return false;
    		for (j = 0; j < 9; j++)
    			if (j != y && board[x][j] == board[x][y])
    				return false;
    		for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
    			for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
    				if (i != x && j != y && board[i][j] == board[x][y])
    					return false;
    		return true;
    	}
    	bool solveSudoku(vector<vector<char> > &board)
    	{
    		for (int i = 0; i < 9; ++i)
    			for (int j = 0; j < 9; ++j)
    			{
    				if ('.' == board[i][j])
    				{
    					for (int k = 1; k <= 9; ++k)
    					{
    						board[i][j] = '0' + k;
    						if (isValid(board, i, j) && solveSudoku(board))
    							return true;
    						board[i][j] = '.';
    					}
    					return false;
    				}
    			}
    		return true;
    	}
    };
    

     2.Longest Valid Parentheses

    class Solution {
    public:
        int longestValidParentheses(string s) 
        { 
            stack<int> left;//position of '(' 
            for(int ii = 0; ii < s.size(); ++ii){ 
                if (s[ii] == '(') left.push(ii); 
                else if (!left.empty()){//')' 
                    s[ii] = 'k'; 
                    s[left.top()] = 'k'; 
                    left.pop(); 
                } 
            } 
            int maxLength = 0; 
            int length = 0; 
            for(int ii = 0; ii < s.size(); ++ii){ 
                if (s[ii]=='k'){ 
                    ++length;                 
                    if (maxLength < length) maxLength = length; 
                } 
                else length = 0; 
            } 
            return maxLength; 
        }
    };
    
     
  • 相关阅读:
    《修改代码的艺术》读书笔记
    《软件架构师的12项修炼》阅读笔记2
    《软件架构师的12项修炼》阅读笔记1
    python 包
    《编程珠玑》阅读笔记1
    Darknet和YOLO的区别和关系
    darknet
    yolov3
    软件质量属性
    酷客 机器学习十讲(一)机器学习介绍
  • 原文地址:https://www.cnblogs.com/tgkx1054/p/3136721.html
Copyright © 2011-2022 走看看