leetCode練習(139)
題目:Single Number
難度:medium
問題描述:
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"] Output: true Explanation: Return true because"leetcode"
can be segmented as"leet code"
.
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"] Output: true Explanation:Return true because"
applepenapple"
can be segmented as"
apple pen apple"
. Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output: false
解題思路:
* 使用動態規劃
* 使用temp[i]代表s的前i個字元是否能被切割
* temp[i]=
* true :任意 j<i, (temp[j]=true)&&(s[j,i]屬於wordDict)
* false:其他情況
程式碼如下:
public static boolean wordBreak(String s, List<String> wordDict) {
if(wordDict.size()==0) return false;
boolean[] dp = new boolean[s.length()+1];//在該位置之前是否是可行的,也就是該處可以加分隔符
dp[0] = true;//空字串是true
for(int i=1;i<dp.length;i++){//求出dp[i] 也就是前i個字元 s[0]-s[i-1]
for(int j=i-1;j>=0;j--){
if(dp[j]){//前j個可以分割 s[0]-s[j-1]
String str = s.substring(j,i);
if(wordDict.contains(str)){
dp[i] = true;
break;
}
}
}
}
return dp[s.length()];
}
相關推薦
leetCode練習(139)
題目:Single Number難度:medium問題描述:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s ca
leetCode練習(98)
題目:Validate Binary Search Tree 難度:medium 問題描述: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is
leetCode練習(140)
題目:Single Number難度:hard問題描述:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to
leetCode練習(153)、(154)
題目:Find Minimum in Rotated Sorted Array難度:medium問題描述:Suppose an array sorted in ascending order is rotated at some pivot unknown to you be
leetCode練習(61)
題目:Rotate List 難度:medium 問題描述: Given a list, rotate the list to the right by k places, where k is n
leetCode練習(121)
題目:Best Time to Buy and Sell Stock 難度:easy 問題描述: Say you have an array for which the ith element is the price of a given stock on day i.
leetCode練習(147)
題目:Insertion Sort List難度:medium問題描述:使用插入排序對List進行排序求解思路:從左到右,依次將node插入到左邊已經排好的list中程式碼如下:public stati
leetCode練習(1)
題目:gas station難度:MEDIUM問題描述:There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car
leetcode練習(2)
Container With Most Water 題意:在二維座標系中,(i, ai) 表示 從 (i, 0) 到 (i, ai) 的一條線段,任意兩條這樣的線段和 x 軸組成一個木桶,找出能夠盛水最多的木桶,返回其容積。 程式碼: int l = 0 ; int
leetCode練習(47)
題目:Permutations II 難度:medium 問題描述: Given a collection of numbers that might contain duplicates, return all possible unique permutations.
leetCode練習(88)
題目:Merge Sorted Array 難度:easy 問題描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y
leetCode練習(172)
題目:Factorial Trailing Zeroes難度:EASY問題描述:Given an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3 Output: 0 Explan
leetCode練習(128)
題目:Longest Consecutive Sequence 難度:hard 問題描述: Given an unsorted array of integers, find the length of the longest consecutive elements se
leetCode練習(43)
題目:Multiply Strings 難度:dedium 問題描述: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th
Leetcode代碼練習(二)
sts compare bre 那種 character span 一次 給定 spa 首先,沒有第二題,沒有第二題的原因是,JavaScript中根本就沒有那種數據結構,盡管我在playground裏面調試出了正確的結果,但是也許是因為數據結構問題,最終沒能讓我通過。 所
【LeetCode演算法練習(C++)】Count and Say
題目: The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 2
二、Linear Regression 練習(轉載)
hold off int 必須 html 移動 根據 which win from 轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2961660.html 前言 本文是多元線性回歸的練習,這裏練習的
六、regularized logisitic regssion練習(轉載)
method open lose hold on update import 等高線 algo 2個 轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964858.html 在上一講Deep learn
日常編程練習(三)
style tar code 依次 span 二叉 div 前序遍歷 cin 一、樹的遍歷算法 樹的創建 struct BinaryTreeNode { int val; BinaryTreeNode* left; BinaryTreeNode*
課堂練習(團隊)
公開 壓力 自己的 般的 責任 開發人員 做出 勞動力 了解 1.團隊如何做決定? a)獨裁:領導說了算。 優點:領導比較冷靜、客觀,以整體利益為主;避免了團隊間的明面上的糾紛,團隊能有效地服從、執行。 缺點:領導可能不知道團隊的能力,不了解團隊的做事方法,不能做出正確的決