python leetcode 419. Battleships in a Board
無難度 按照題意解題即可
按照遍歷順序只需判斷左邊和上邊是否相連
class Solution: def countBattleships(self, board): """ :type board: List[List[str]] :rtype: int """ m=len(board) if m==0: return 0 n=len(board[0]) count=0 for i in range(m): for j in range(n): if board[i][j]=='X': if (i-1<0 or board[i-1][j]=='.') and (j-1<0 or board[i][j-1]=='.'): count+=1 return count
相關推薦
python leetcode 419. Battleships in a Board
無難度 按照題意解題即可 按照遍歷順序只需判斷左邊和上邊是否相連 class Solution: def countBattleships(self, board): """ :type board: List[List[str]]
419. Battleships in a Board(python+cpp)
題目: Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '
419. Battleships in a Board
rds with turn || words ret hat win return Given an 2D board, count how many battleships are in it. The battleships are represented with ‘
[LeetCode] Battleships in a Board 平板上的戰船
Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You
[LeetCode&Python] Problem 700. Search in a Binary Search Tree
ould ted ant elif div serial turn therefore roo Given the root node of a binary search tree (BST) and a value. You need to find the node
LeetCode | Reverse Words in a String(C#)
++ style str blog cnblogs count item leetcode string 題目: Given an input string, reverse the string word by word. For example,Given s = "
[LeetCode] Single Element in a Sorted Array
should d+ rst where 利用 題目 cond 順序 二分 Given a sorted array consisting of only integers where every element appears twice except for one el
LeetCode-Single Element in a Sorted Array
一、Description 題目描述:在一個有序陣列中找出只出現一次的數字,要求時間複雜度為O(log n) 。 Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11
Leetcode|Reverse Words in a String
Given s = “the sky is blue”, * return “blue is sky the”. 這種問題一般有三種解法: 1,One simple approach is a two-pass solution: First pass to split the s
leetcode Delete Node in a Linked List
leetcode Delete Node in a Linked List 水題~~~ 刪除結點,很簡單的一題可以直接檢索p.next.value == node.value時 執行p.next = p.next.next就好了 可是題目並沒有給head。。然後這時單鏈表。。 所以這方法
python leetcode 33. Search in Rotated Sorted Array
二分查詢,注意if判斷和退出迴圈的條件 class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target:
LeetCode——Peak Index in a Mountain Array(852)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists some 0 < i < A.length - 1 such th
LeetCode-Peak Index in a Mountain Array
Description: Let’s call an array A a mountain if the following properties hold: A.length >= 3 There exists some 0 < i < A.
python -- leetcode ---- 81.Search in Rotated Sorted Array II
-81.Search in Rotated Sorted Array II 給定一個list,是由一個有序陣列在某一樞紐處旋轉得到的,並且其中可能含有重複元素,要求判斷target是否在這個list中. 雖然這個list經過旋轉,但是還是可以用二分查詢的思想,因為mid的
LeetCode-Delete Node in a Linked List
Description: Write a function to delete a node (except the tail) in a singly linked list, given only
JavaScript刷LeetCode -- 700. Search in a Binary Search Tree
一、題目 Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the give
[LeetCode] Reverse Words in a String 翻轉字串中的單詞
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12):
[LeetCode] Delete Node in a BST 刪除二叉搜尋樹中的節點
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the B
[LeetCode] Reverse Words in a String III 翻轉字串中的單詞之三
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Exa
[LeetCode] Single Element in a Sorted Array 有序陣列中的單獨元素
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element th