每日java5-30

  1. //给你一组字符,让你输出里面出现次数最多且数值最大的一个,出现几次

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);

}

}

}

}

  1. 两数之和.描述给一个整数数组,找到两个数使得他们的和等于一个给定的数 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;

}

  1. /* 输入包括正整数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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值