LeetCode (N)

Next Permutation
 
AC Rate: 761/2986
My Submissions

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

class Solution {
public:
    void nextPermutation(vector<int> &num) {
        next_permutation(num.begin(), num.end());
    }
};


N-Queens
 
AC Rate: 649/2562
My Submissions

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

class Solution {
	vector<bool> row, left, right;
	void fill(const int col, const int n, vector<string> &board, vector<vector<string> > &ans) {
		if (col == n) {
			ans.push_back(board);
			return;
		}
		for (int i = 0; i < n; ++i) {
			if (row[i] || left[i - col + n] || right[i + col])
				continue;
			row[i] = left[i - col + n] = right[i + col] = true;
			board[i][col] = 'Q';
			fill(col + 1, n, board, ans);
			row[i] = left[i - col + n] = right[i + col] = false;
			board[i][col] = '.';
		}
	}
public:
	vector<vector<string> > solveNQueens(int n) {
		vector<vector<string> > ans;
		string raw(n, '.');
		vector<string> board(n, raw);
		row.resize(n, false);
		left.resize(2 * n, false);
		right.resize(2 * n, false);
		fill(0, n, board, ans);
		return ans;
	}
};



N-Queens II
 
AC Rate: 484/1989
My Submissions

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

class Solution {
	vector<bool> row, left, right;
	void fill(const int col, const int n, int &ans) {
		if (col == n) {
			++ans;
			return;
		}
		for (int i = 0; i < n; ++i) {
			if (row[i] || left[i - col + n] || right[i + col])
				continue;
			row[i] = left[i - col + n] = right[i + col] = true;
			fill(col + 1, n, ans);
			row[i] = left[i - col + n] = right[i + col] = false;
		}
	}
public:
	int totalNQueens(int n) {
		row.resize(n, false);
		left.resize(2 * n, false);
		right.resize(2 * n, false);
		int ans = 0;
		fill(0, n, ans);
		return ans;
	}
};





Number of 1 Bits

  Total Accepted: 18125  Total Submissions: 49460 My Submissions

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.







class Solution {
public:
    int hammingWeight(uint32_t n) {
        int c = 0;
        for (; n; ++c)
            n &= (n - 1);
        return c;
    }
};











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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值