LeetCode Daily
6.18
771.宝石和石头**
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此”a”和”A”是不同类型的石头。
示例 1:
输入: J = “aA”, S = “aAAbbbb”
输出: 3
示例 2:
输入: J = “z”, S = “ZZ”
输出: 0
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jewels-and-stones
class Solution {
public int numJewelsInStones(String J, String S) {
Map<Character,Integer> jmap=new HashMap<>();
int num=0;
char[] jc=J.toCharArray();
char[] sc=S.toCharArray();
for (int i=0;i<jc.length;i++){
jmap.put(jc[i],i);
}
for (int j=0;j<sc.length;j++){
if (jmap.containsKey(sc[j])){
num++;
}
}
return num;
}
}
6.19
1021. 删除最外层的括号**
有效括号字符串为空 (“”)、”(“ + A + “)” 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接。例如,””,”()”,”(())()” 和 “(()(()))” 都是有效的括号字符串。
如果有效字符串 S 非空,且不存在将其拆分为 S = A+B 的方法,我们称其为原语(primitive),其中 A 和 B 都是非空有效括号字符串。
给出一个非空有效字符串 S,考虑将其进行原语化分解,使得:S = P_1 + P_2 + … + P_k,其中 P_i 是有效括号字符串原语。
对 S 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 S 。
示例 1:
输入:”(()())(())”
输出:”()()()”
解释:
输入字符串为 “(()())(())”,原语化分解得到 “(()())” + “(())”,
删除每个部分中的最外层括号后得到 “()()” + “()” = “()()()”。
示例 2:
输入:”(()())(())(()(()))”
输出:”()()()()(())”
解释:
输入字符串为 “(()())(())(()(()))”,原语化分解得到 “(()())” + “(())” + “(()(()))”,
删除每隔部分中的最外层括号后得到 “()()” + “()” + “()(())” = “()()()()(())”。
示例 3:
输入:”()()”
输出:””
解释:
输入字符串为 “()()”,原语化分解得到 “()” + “()”,
删除每个部分中的最外层括号后得到 “” + “” = “”。
提示:
S.length <= 10000
S[i] 为 “(” 或 “)”
S 是一个有效括号字符串
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-outermost-parentheses
class Solution {
public static String removeOuterParentheses(String S) {
if (S == null || S == ""){
return null;
}
int saveIndex = 0,mark = 0;
boolean save = false;
char[] chars = S.toCharArray();
char[] res = new char[chars.length];//保存结果的res,第一个和最后一个不需要保存。
for (int i = 0; i < chars.length ; i++) {
if (chars[i] == '('){
mark++;
}
if (chars[i] == ')'){
mark--;
}
if(mark == 0 && i !=0){
save = false; //这个不需要保存,下一个也不需要保存
continue;
}
if(save == true){
//将该保存的保存到结果数组中
res[saveIndex] = chars[i];
saveIndex++;
}
save = true; //表示可以保存,即下一个可能需要保存
}
String result = new String(res);
return result.trim();
}
}
1. Two Sum
原题连接
https://leetcode.com/problems/two-sum
内容描述
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
解题方案
HashMap原理
思路 1 - 时间复杂度: O(N)- 空间复杂度: O(N)******
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> lookup = new HashMap<>();
int[] res = new int[2];
for (int i = 0; i < nums.length; i++) {
if (lookup.containsKey(target - nums[i])) {
res = new int[] { lookup.get(target - nums[i]), i };
break;
} else {
lookup.put(nums[i], i);
}
}
return res;
}
}
第一种解法
public int[] twoSum(int[] nums,int target){
for(int i = 0; i < nums.length ; i ++){
for(int j = 0; j < nums.length ; j ++){
if(nums[j] == target.nums[i]){
return new int [] {i,j};
}
}
}
}
2nd
public int[] twoSum(int[] ,int target){
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0; i < nums.length; i ++){
map.put(nums[i],i);
}
for(int i = 0; i < nums.length; i ++){
int complement = target - num[i];
if(map.containsKey(complement) && map.get(complement) != i){
return new int[]{(i,map.get(complement))};
}
}
throw new IllegalArgumentException("No two sum solution")
}
一个HashMap解法
public int[] TwoSum(int[] nums,int target){
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < nums.length ; i ++){
int complement = target - nums[i];
if(map.containsKey(complement)){
return new int{}{map.get(complement),i};
}
map.put(nums[i],i)
}
throw new IllegalArgumentException("No two sum solution")
}
找出数组中重复的数字
题目描述
在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为 7 的数组 {2, 3, 1, 0, 2, 5, 3},那么对应的输出是重复的数字 2 或者 3。
解法
解法一
排序后,顺序扫描,判断是否有重复,时间复杂度为 O(n²)。
解法二
利用哈希表,遍历数组,如果哈希表中没有该元素,则存入哈希表中,否则返回重复的元素。时间复杂度为 O(n),空间复杂度为 O(n)。
解法三
长度为 n,元素的数值范围也为 n,如果没有重复元素,那么数组每个下标对应的值与下标相等。
从头到尾遍历数组,当扫描到下标 i 的数字 nums[i]:
如果等于 i,继续向下扫描;
如果不等于 i,拿它与第 nums[i] 个数进行比较,如果相等,说明有重复值,返回 nums[i]。如果不相等,就把第 i 个数 和第 nums[i] 个数交换。重复这个比较交换的过程。
此算法时间复杂度为 O(n),因为每个元素最多只要两次交换,就能确定位置。空间复杂度为 O(1)。
/**
* @author bingo
* @since 2018/10/27
*/
public class Solution {
/**
* 查找数组中的重复元素
* @param numbers 数组
* @param length 数组长度
* @param duplication duplication[0]存储重复元素
* @return boolean
*/
public boolean duplicate(int[] numbers, int length, int[] duplication) {
if (numbers == null || length < 1) {
return false;
}
for (int e : numbers) {
if (e >= length) {
return false;
}
}
for (int i = 0; i < length; ++i) {
while (numbers[i] != i) {
if (numbers[i] == numbers[numbers[i]]) {
duplication[0] = numbers[i];
return true;
}
swap(numbers, i, numbers[i]);
}
}
return false;
}
private void swap(int[] numbers, int i, int j) {
int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
}
测试用例
长度为 n 的数组中包含一个或多个重复的数字;
数组中不包含重复的数字;
无效测试输入用例(输入空指针;长度为 n 的数组中包含 0~n-1 之外的数字)。
还没有评论,来说两句吧...