第一题:字符串、有效括号数
给定一个字符串,打印里面匹配的括号的个数和下标,比如:
(1)23(4()5)6,一共有三对括号,下标0和2是一对,5和10是一对,7和8是一对。输入数据的括号均能成对出现,以下输入是不存在的:((1)
输入描述:一个包含数字和括号的字符串,一行
输出描述:多行输出,第一行是成对括号的个数,后面每行是一个括号的下标。
输入:(1)
输出:
1
0
2
说明:共有1对成对的括号,其下标为0和2
输入:
(1)23(4()5)6
输出:
3
0
2
5
10
7
8
说明:有3对成对的括号,每次输出一对的下标,输出顺序以左括号所在下标的升序排列,故0,2一对,5,10一对,7,8一对。
AC 100%
import java.util.*;
public class Solution1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String s = scanner.nextLine();
method(s);
}
}
public static void method(String s){
char[] chars = s.toCharArray();
int count = 0;
for(char c: chars){
if(c == '(' || c == ')'){
count++;
}
}
System.out.println(count/2);
Deque<Integer> leftStack = new LinkedList<>();
Deque<Integer> rightQueue = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < chars.length; i++) {
if(chars[i] == '('){
leftStack.push(i);
}
if(chars[i] == ')'){
rightQueue.offer(i);
List<Integer> list = new ArrayList<>();
list.add(leftStack.pop());
list.add(rightQueue.poll());
res.add(list);
}
}
Collections.sort(res,(o1,o2)->{
return o1.get(0)-o2.get(0);
});
for (int i = 0; i < res.size(); i++) {
System.out.println(res.get(i).get(0));
System.out.println(res.get(i).get(1));
}
}
}
第二题:很赞的整数对
题目:给一个整数对(x,y),并且允许把其中一个数修改成之前两个数的和,x+y。比如(5,2),两数之和为7,经过一次修改可以变成(7,2)或(5,7)。再给一个整数m,问至少要做多少次上述修改操作,才能使两个数组成的数对是对m很赞的整数对。两个数中至少有一个数大于等于m,这个数对就是很赞的整数对。
输入描述:一行包含3个整数,分别是x,y,m。其中x,y是最初的两个整数,取值范围是int32;m是一个整数,取值范围是int32;
输出描述:至少要做多少次上述修改操作,才能使得数对为m的很赞整数对。
输入: 1 2 5
输出: 2
说明:经过2次操作,一种可行的但不一定是最优的操作方法是:(1,2)-->(3,2)-->(5,2)
输入:-1 4 15
输出: 4
说明:最少经过4次操作,一种可行的但不一定是最优的操作方法是:(-1,4)-->(3,4)-->(7,4)-->(11,4)-->(15,4)
输入: 0 -1 5
输出: -1
说明:经过多少次操作都无法满足要求,输出-1
AC 77%
import java.util.Scanner;
public class Solution2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String s = scanner.nextLine();
String[] s1 = s.split(" ");
int[] nums = new int[2];
nums[0] = Integer.valueOf(s1[0]);
nums[1] = Integer.valueOf(s1[1]);
int m = Integer.valueOf(s1[2]);
int res = find(nums, m);
System.out.println(res);
}
}
public static int find(int[] nums, int m){
//1.其中有1个大于m就不需要操作
int x = nums[0];
int y = nums[1];
long x1 = x;
long y1 = y;
if (x1 < m && y1 < m &&(x1 + y1 >Integer.MAX_VALUE) ){
return -1;
}
if(x >= m || y>= m){
return 0;
}
//2.1次相加后就大于等于m就返回1,只要加1次就可以了
int sum = x + y;
if(sum >= m){
return 1;
}
//3.如果有一个数为0,且另一个数小于0,那么就不可能成功
if ((nums[0] == 0 || nums[1] == 0)){
if (nums[0] < 0){
return -1;
}else if (nums[1] < 0){
return -1;
}else {
return -1;
}
}
//4.加一次不够的情况
int count = 1;
while (sum < m){
int maxV = Math.max(nums[0],nums[1]);
if (nums[0] < nums[1]){
nums[0] = sum;
}else {
nums[1] = sum;
}
sum += maxV;
count++;
}
return count;
}
}
第三题:leetcode322 零钱兑换原题
https://leetcode-cn.com/problems/coin-change-2/solution/ling-qian-dui-huan-ii-by-leetcode-soluti-f7uh/
题目:从面值为[1,5,10,20,100]5种面值的纸币中组成指定的金额,求组成指定金额需要的纸币的最少张数。
输入:金额,一个整数
输出:最少的张数,一个整数
输入:125
输出:3
说明:一张100元,一张20元,一张5元
输入:43
输出:5
说明:2张20元,3张1元
输入:1000000000
输出:10000000
说明:10000000张100元
AC 97%
import java.util.Arrays;
import java.util.Scanner;
public class Solution3 {
//背包问题:[1,5,10,20,100],背包重量为输入值,问最少用哪些物品可以装满背包
static int[] bags = new int[]{1,5,10,20,100};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
Integer in = scanner.nextInt();
int res = find(in);
System.out.println(res);
}
}
public static int find(int in){
int[] dp = new int[in+1];
Arrays.fill(dp,Integer.MAX_VALUE-1);
dp[0]=0;
for (int i = 1; i <= bags.length; i++) {
for(int j = 0;j<=in;j++){
if (j>=bags[i-1]){
dp[j] = Math.min(dp[j],dp[j-bags[i-1]] + 1);
}
}
}
return dp[in] == (Integer.MAX_VALUE-1)? -1:dp[in];
}
}