1.把数组排成最小的数(offer45)
class Solution {
public static String minNumber(int[] nums) {
String[] arr=new String[nums.length];
for(int i=0;i<nums.length;i++){
arr[i]=nums[i]+"";
}
Arrays.sort(arr,(o1,o2)->(o1+o2).compareTo(o2+o1));
StringBuilder stringBuilder=new StringBuilder();
for(String s:arr){
stringBuilder.append(s);
}
return stringBuilder.toString();
}
}
2.把数字翻译成字符串
动态规划解决此问题
class Solution {
public static int translateNum(int num) {
String s=num+"";
char[] arr=s.toCharArray();
int[] dp=new int[arr.length];
dp[0]=1;
int i=1;
for(;i<arr.length;i++){
dp[i]=dp[i-1];
int ret=10*(arr[i-1]-'0')+(arr[i]-'0');
if(ret>9 && ret<=25){
if(i==1){
dp[i]=dp[i]+1;
}else{
dp[i]=dp[i]+dp[i-2];
}
}
}
return dp[i-1];
}
}
3.礼物的最大价值
动态规划解决此问题
class Solution {
public static int maxValue(int[][] grid) {
int[][] dp=new int[grid.length][grid[0].length];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(i==0 && j==0){
dp[i][j]=grid[0][0];
}else if(i==0 && j!=0){
dp[i][j]=grid[i][j]+dp[i][j-1];
}else if(j==0 && i!=0){
dp[i][j]=grid[i][j]+dp[i-1][j];
}else{
dp[i][j]=grid[i][j]+Math.max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[grid.length-1][grid[0].length-1];
}
}
4.把字符串转换成整数
class Solution {
public static int strToInt(String str) {
List<Character> list=new ArrayList<>();
if(str.length()==0){
return 0;
}
str=str.trim();
if(str==""){
return 0;
}
char[] temp=str.toCharArray();
if(temp.length==1 && (temp[0]=='+'||temp[0]=='-')){
return 0;
}
StringBuilder stringBuilder=new StringBuilder();
if(temp[0]=='+' ||temp[0]=='-' || (temp[0]>='0'&&temp[0]<='9')){
list.add(temp[0]);
stringBuilder.append(temp[0]);
}else{
return 0;
}
for(int i=1;i<temp.length;i++){
if(temp[i]>='0'&&temp[i]<='9'){
list.add(temp[i]);
stringBuilder.append(temp[i]);
}else {
break;
}
Long result=Long.parseLong(stringBuilder.toString());
if(result<=Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else if(result>=Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
}
if(list.size()==1 && (list.get(0)=='+'|| list.get(0)=='-')){
return 0;
}
return Integer.parseInt(stringBuilder.toString());
}
}
5.回文子字符串的个数
class Solution {
public static int countSubstrings(String s) {
if(s.length()==1){
return 1;
}
int result=s.length();
for(int i=0;i<s.length();i++){
for (int j=i+1;j<s.length();j++){
String temp=s.substring(i,j+1);
StringBuilder stringBuilder=new StringBuilder(temp);
if(temp.equals(stringBuilder.reverse().toString())){
result++;
}
}
}
return result;
}
}
6.和大于等于target的最短子数组
使用双层for循环来解决此题
class Solution {
public static int minSubArrayLen(int target, int[] nums) {
int ret=Integer.MAX_VALUE;
for(int i=0;i<nums.length;i++){
int temp=target;
List<Integer> list=new ArrayList<>();
for(int j=i;j<nums.length;j++){
if(temp>=0){
temp-=nums[j];
list.add(nums[j]);
}else{
break;
}
if(temp<=0){
ret=Math.min(ret,list.size());
}
}
}
if(ret==Integer.MAX_VALUE){
return 0;
}
return ret;
}
}
7.字符串中的变位词
class Solution {
public static boolean checkInclusion(String s1, String s2) {
if(s1.length()>s2.length()){
return false;
}
char[] ss1=s1.toCharArray();
Arrays.sort(ss1);
String a=new String(ss1);
for(int i=0;i<(s2.length()-s1.length()+1);i++){
String s=s2.substring(i,i+s1.length());
char[] temp=s.toCharArray();
Arrays.sort(temp);
String ret=new String(temp);
if(a.equals(ret)){
return true;
}
}
return false;
}
}
8.字符串中的所有变位词
class Solution {
public static List<Integer> findAnagrams(String s, String p) {
List<Integer> list=new ArrayList<>();
char[] p1=p.toCharArray();
Arrays.sort(p1);
String pp=new String(p1);
for(int i=0;i<(s.length()-p.length()+1);i++){
String temp=s.substring(i,i+p.length());
char[] t1=temp.toCharArray();
Arrays.sort(t1);
String tt=new String(t1);
if(pp.equals(tt)){
list.add(i);
}
}
return list;
}
}
9.乘积小于k的子数组
class Solution {
public static int numSubarrayProductLessThanK(int[] nums, int k) {
int ret=1;
int left=0;
int count=0;
for(int right=0;right<nums.length;right++){
ret=ret*nums[right];
while(left<=right && ret>=k){
ret/=nums[left++];
}
count+=right>=left?(right-left+1):0;
}
return count;
}
}
10.和为k的子数组
解题思路: 链接: bilibili LeetCode力扣
class Solution {
public static int subarraySum(int[] nums, int k) {
HashMap<Integer,Integer> map=new HashMap<>();
map.put(0,1);
int pre=0;
int count=0;
for(int i=0;i<nums.length;i++){
pre+=nums[i];
if(map.containsKey(pre-k)){
count+=map.get(pre-k);
}
map.put(pre,map.getOrDefault(pre,0)+1);
}
return count;
}
}
|