赞
踩
1.判断是不是ipv6字符
import java.util.Scanner; public class Zijie { public static void main(String[] args){ Scanner sc=new Scanner(System.in); String str=sc.next(); System.out.println(Isipv4(str)); } public static boolean Isipv4(String ipv4){ if(ipv4==null || ipv4.length()==0){ return false;//字符串为空或者空串 } String[] parts=ipv4.split("\\.");//因为java doc里已经说明, split的参数是reg, 即正则表达式, 如果用"|"分割, 则需使用"\\|" if(parts.length!=4){ return false;//分割开的数组根本就不是4个数字 } for(int i=0;i<parts.length;i++){ try{ int n=Integer.parseInt(parts[i]); if(n<0 || n>255){ return false;//数字不在正确范围内 } }catch (NumberFormatException e) { return false;//转换数字不正确 } } return true; } }
字符串中第一个不重复的字符
import java.util.*; public class Zijie { public static void main(String[] args){ Scanner input=new Scanner(System.in); String str=input.next(); int index=firstNo(str); if(index==-1){ System.out.println("没有"); return; } System.out.println(str.charAt(index)); } public static int firstNo(String str){ Map<Character,Integer> map=new HashMap<Character, Integer>(); for(int i=0;i<str.length();i++){ char c=str.charAt(i); if(map.containsKey(c)){ map.put(c,map.get(c)+1); }else{ map.put(c,1); } } for(int i=0;i<str.length();i++){ char c=str.charAt(i); if(map.get(c)==1){ return i; } } return -1; } }
手写top-K
import java.util.*; public class Zijie { public static void main(String[] args) { //int a[] = {2, 20, 3, 7, 9, 1, 17, 18, 0, 4}; //int k = 6; Scanner in=new Scanner(System.in); int num=in.nextInt(); Scanner input=new Scanner(System.in); int i=0; int[] array=new int[num]; while(i<num &&input.hasNextInt()){ array[i]=input.nextInt(); i++; } Scanner sc=new Scanner(System.in); int k=sc.nextInt(); new TopK().getTopKMinBySort(array, 0, array.length - 1, k); for (int j = 0; j< k; j++) { System.out.print(array[j] + " "); } } //利用快速排序法 public static class TopK { int partion(int a[], int first, int end) { int i = first; int main = a[end]; for (int j = first; j < end; j++) { if (a[j] < main) { int temp = a[j]; a[j] = a[i]; a[i] = temp; i++; } } a[end] = a[i]; a[i] = main; return i; } void getTopKMinBySort(int a[], int first, int end, int k) { if (first < end) { int partionIndex = partion(a, first, end); if (partionIndex == k - 1) return; else if (partionIndex > k - 1) getTopKMinBySort(a, first, partionIndex - 1, k); else getTopKMinBySort(a, partionIndex + 1, end, k); } } } }
最大连续子序列和
import java.util.*; public class Zijie { int max; public static void main(String[] args) { Scanner sc=new Scanner(System.in); int num=sc.nextInt(); sc.nextLine(); Scanner sc2=new Scanner(System.in); int i=0; int[] array=new int[num]; while(i<num &&sc2.hasNextInt()){ array[i]=sc2.nextInt(); i++; } System.out.println(maxSubArray(array)); } public static int maxSubArray(int[] nums){ if(nums==null ||nums.length<1){ return 0; } int[] dp=new int[nums.length]; dp[0]=nums[0]; int max=nums[0]; for(int i=1;i<nums.length;i++){ dp[i]=Math.max(nums[i],dp[i-1]+nums[i]); if(dp[i]>max){ max=dp[i]; } } return max; } }
最长不连续子串
import java.util.*; public class Zijie { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); System.out.println(lengthOfLongestSubstring(str)); } public static int lengthOfLongestSubstring(String s) { int i = 0; int j = 0; int res = 0; Map<Character, Integer> map = new HashMap<Character, Integer>(); while (j < s.length()) { char charJ = s.charAt(j); Integer cntJ = map.get(charJ); if (cntJ == null || cntJ == 0) {
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。