019/leetcode
/47. Permutations II/Permutations II.java
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by qwen on 2019/6/10.
*/
public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
boolean[] visited = new boolean[nums.length];
permuteUnique(res, new ArrayList<>(), nums, visited);
return res;
}
private void permuteUnique(List<List<Integer>> res, List<Integer> cur, int[] nums, boolean[] visited) {
if(nums.length == visited.length) {
res.add(new ArrayList<>(cur));
return;
}
for(int i = 0; i < nums.length; i++) {
if(visited[i] || (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1])) {
continue;
}
visited[i] = true;
cur.add(nums[i]);
permuteUnique(res, cur, nums, visited);
cur.remove(cur.size() - 1);
visited[i] = false;
}
}
}
/30. Substring with Concatenation of All Words/Substring with Concatenation of All Words.java
package com.company;
/**
* Created by qwen on 2019/5/13.
*/
public class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for(int i = 1; i <= s.length(); i++) {
for(String word : wordDict) {
if(i >= word.length() && dp[i - word.length()] && s.substring(i - word.length(), i).equals(word)) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
}
/137. Single Number/Single Number.java
package com.company;
/**
* Created by qwen on 2019/5/28.
*/
public class Solution {
public boolean isPerfectSquare(int num) {
long a = 1;
long b = num;
while(a < b) {
long mid = (a + b) / 2;
long midSquare = mid * mid;
if(midSquare == num) {
return true;
} else if(midSquare < num) {
a = mid + 1;
} else {
b = mid;
}
}
return false;
}
}
/108. Convert Sorted Array to Binary Search Tree/Convert Sorted Array to Binary Search Tree.java
package com.company;
/**
* Created by qwen on 2019/5/14.
*/
public class Solution {
public int[] productExceptSelf(int[] nums) {
int[] res = new int[nums.length];
res[0] = 1;
for(int i = 1; i < nums.length; i++) {
res[i] = res[i - 1] * nums[i - 1];
}
int right = 1;
for(int i = nums.length - 1; i >= 0; i--) {
res[i] = res[i] * right;
right *= nums[i];
}
return res;
}
}
/283. Move Zeroes/Move Zeroes.java
package com.company;
/**
* Created by qwen on 2019/5/31.
*/
public class Solution {
public int romanToInt(String s) {
String[] arr = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
int res = 0;
for(int i = 0; i < arr.length; i++) {
int count = s.length() / arr[i].length();
while(count > 0) {
res += nums[i] * Math.min(count, s.length() / (i + 1));
count -= s.length() / (i + 1);
}
s = s.substring(s.length() % (i + 1));
}
return res;
}
}
/28. Implement strStr/Implement strStr.java
package com.company;
/**
* Created by qwen on 2019/6/5.
*/
public class Solution {
public boolean isMatch(String s, String p) {
int i = 0;
int j = 0;
int star = -1;
while(i < s.length()) {
if(j < p.length() && (p.charAt(j) == '?' || p.charAt(j) == s.charAt(i))) {
i++;
j++;
} else if(j < p.length() && p.charAt(j) == '*') {
star = j;
i++;
} else if(star == -1) {
return false;
} else {
j = star + 1;
star = -1;
i++;
}
}