- //给你一组字符,让你输出里面出现次数最多且数值最大的一个,出现几次
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Map<Integer,
Integer> map = new HashMap<Integer, Integer>();
Scanner
sc = new Scanner(System.in);
System.out.println(“请输入数组长度:”);
int n = sc.nextInt();
int[] a = new int[n];
System.out.println(“输入数组:”);
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
for (int i : a) {
Integer
x = map.get(a[i]);
if (x == null) {
map.put(a[i], 1);
}
else {
x++;
map.put(a[i], i);
}
}
Integer
valuemax = Integer.MIN_VALUE;
Integer
keymax = Integer.MIN_VALUE;
for (Entry<Integer, Integer> entry : map.entrySet()) {
Integer
key = entry.getKey();
Integer
value = entry.getValue();
if (value >= valuemax) {
valuemax = value;
if (key >= keymax) {
keymax = key;
}
}
}
for (Entry<Integer, Integer> entry : map.entrySet()) {
Integer
key = entry.getKey();
Integer
value = entry.getValue();
if (key == keymax) {
System.out.println(“频率最大数” + key + " 频率:" + valuemax);
}
}
}
}
- 两数之和.描述给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。返回这两个数的下标
public class Demo {
public static void main(String[] args){
Scanner
sc=new Scanner(System.in);
System.out.println(“输入数组的长度:”);
int n=sc.nextInt();
System.out.println(“输入数组:”);
int[] a=new int[n];
for(int i=0;i<a.length;i++) {
a[i]=sc.nextInt();
}
System.out.println(“输入目标数字”);
int x=sc.nextInt();
for(int i=0;i<a.length;i++) {
for(int j=i+1;j<a.length;j++) {
if(a[i]+a[j]==x) {
System.out.println(i+","+j);
}
}
}
}
}
140.链表划分。描述:给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。
public ListNode partition(ListNode head, int x) {
if (head ==
null) {
return
null;
}
ListNode
leftDummy = new ListNode(0);
ListNode
rightDummy = new ListNode(0);
ListNode
left = leftDummy;
ListNode
right = rightDummy;
while (head
!= null) {
if
(head.val < x) {
left.next = head;
left = head;
} else
{
right.next = head;
right = head;
}
head =
head.next;
}
right.next
= null;
left.next =
rightDummy.next;
return
leftDummy.next;
}
- /* 输入包括正整数sum(1 ≤ sum ≤ 10^18)
* 输出描述:输出一个正整数,即满足条件的X,如果没有这样的X,输出-1。
* 例如X = 509, 在黑板上出现过的数字依次是509, 50, 5, 他们的和就是564.
*/
import java.util.Scanner;
public class text {
public static void main(String[] args) {
Scanner
sc = new Scanner(System.in);
long num = sc.nextLong();
long res = binarySearch(num);
System.out.println(res);
}
private static long binarySearch(long num) {
long l = 0, r = num;
while (l <= r) {
long mid = l + ((r - l) >>>
1);
long midRes = getNiuSum(mid);
if (midRes == num)
return mid;
else if (midRes < num)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
private static long getNiuSum(long num) {
long ans = 0;
while (num != 0) {
ans += num;
num /= 10;
}
return ans;
}
}