力扣---2020.9.3
51. N 皇后
class Solution {
List<List<String>> res = new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
if (n<=0) return res;
char[][] board = new char[n][n];
for (char[] chars : board) Arrays.fill(chars,'.');
backtrack(board,0);
return res;
}
public void backtrack(char[][] board,int row){
if(board.length==row){
res.add(charToStr(board));
return;
}
int n = board[row].length;
for(int col = 0;col < n;col++){
if(!isValid(board,row,col)) continue;
board[row][col] = 'Q';
backtrack(board,row+1);
board[row][col] = '.';
}
}
public List<String> charToStr(char[][] board){
List<String> result = new ArrayList<>();
for (char[] c : board){
result.add(String.valueOf(c));
}
return result;
}
public boolean isValid(char[][] board,int row,int col){
int rows= board.length;
for (char[] c : board){
if (c[col]=='Q'){
return false;
}
}
// 左上角
for (int i = row-1,j = col-1;i >=0 && j >= 0;i--,j--){
if (board[i][j]=='Q') return false;
}
// 右上角
for (int i = row-1,j = col+1;i >=0 && j < rows;i--,j++){
if (board[i][j]=='Q') return false;
}
return true;
}
}
劍指 Offer 20. 表示數值的字串
class Solution {
public boolean isNumber(String s) {
if (s.length()==0 || s == null){
return false;
}
s = s.trim();
boolean numFlag = false;
boolean dotFlag = false;
boolean eFlag = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// 判斷為數字 numFlag 標記為true
if (c >= '0' && c <= '9'){
numFlag = true;
// 判斷為 . 時,之前需要沒有出現過 . 和 e
}else if (c=='.' && !dotFlag && !eFlag){
dotFlag = true;
// 判斷為e時,之前需要出現過數字,但是沒有出現過e
}else if ((c=='e' || c=='E') && numFlag && !eFlag){
eFlag = true;
numFlag = false;
}else if ((c=='+' || c=='-') && (i==0 || s.charAt(i-1)=='e' || s.charAt(i-1)=='E')){
}else {
return false;
}
}
return numFlag;
}
}
劍指 Offer 41. 資料流中的中位數
class MedianFinder {
PriorityQueue<Integer> left;
PriorityQueue<Integer> right;
/** initialize your data structure here. */
public MedianFinder() {
left = new PriorityQueue<>((n1,n2)->n2-n1);
right = new PriorityQueue<>();
}
public void addNum(int num) {
left.add(num);
right.add(left.poll());
if(left.size()+1 < right.size()){
left.add(right.poll());
}
}
public double findMedian() {
if(right.size()>left.size())return right.peek();
return (left.peek()+right.peek())/2.0;
}
}
class MedianFinder {
Queue<Integer> A, B;
public MedianFinder() {
A = new PriorityQueue<>(); // 小頂堆,儲存較大的一半
B = new PriorityQueue<>((x, y) -> (y - x)); // 大頂堆,儲存較小的一半
}
public void addNum(int num) {
if(A.size() != B.size()) {
A.add(num);
B.add(A.poll());
} else {
B.add(num);
A.add(B.poll());
}
}
public double findMedian() {
return A.size() != B.size() ? A.peek() : (A.peek() + B.peek()) / 2.0;
}
}
你知道的越多,你不知道的越多。
相關文章
- 力扣---2020.7.30力扣
- 力扣---2020.9.27力扣
- 力扣---2020.9.29力扣
- 力扣---2020.9.28力扣
- 力扣---2020.9.4力扣
- 力扣2713 2024.6.19力扣
- 力扣2589 5.16力扣
- 力扣1542 2024.5.22力扣
- 力扣題解力扣
- 力扣(LeetCode)543力扣LeetCode
- 力扣(LeetCode)934力扣LeetCode
- 力扣(LeetCode)103力扣LeetCode
- 力扣(LeetCode)513力扣LeetCode
- 力扣(LeetCode)389力扣LeetCode
- 力扣(LeetCode)796力扣LeetCode
- 力扣(LeetCode)863力扣LeetCode
- 力扣(LeetCode)310力扣LeetCode
- 力扣(LeetCode)130力扣LeetCode
- 力扣(LeetCode)965力扣LeetCode
- 力扣社群開通力扣
- 力扣-9.23-680力扣
- 力扣之按身高排序力扣排序
- 力扣之移動零力扣
- 力扣之兩數之和力扣
- 教你如何玩轉力扣力扣
- 力扣oj-字串相乘力扣字串
- 力扣最長公共字首力扣
- 力扣-48 旋轉影像力扣
- 力扣27. 移除元素力扣
- 力扣-376. 擺動序列力扣
- 力扣 22. 括號生成力扣
- 力扣-231. 2 的冪力扣
- 力扣-283. 移動零力扣
- 力扣-54. 螺旋矩陣力扣矩陣
- leetcode力扣 213. 打家劫舍 IILeetCode力扣
- 力扣之有效的迴文力扣
- 力扣之存在重複元素力扣
- 力扣#43 字串相乘(C++)力扣字串C++