- 博客(16)
- 收藏
- 关注
原创 Math.abs()的使用方法
abs() 方法可返回数的绝对值。例如document.write(Math.abs(7.25) + "<br />")document.write(Math.abs(-7.25) + "<br />")document.write(Math.abs(7.25-10))输出:7.257.252.75
2021-04-06 20:13:23 8124
原创 遍历时记录下遇到的最大值
加一个res=Math.maxclass Solution { public int lengthOfLongestSubstring(String s) { Map<Character, Integer> dic = new HashMap<>(); int res = 0, tmp = 0; for(int j = 0; j < s.length(); j++) { res = Math.ma
2021-04-06 11:00:21 48
原创 动态规划
假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?示例 1:输入: [7,1,5,3,6,4]输出: 5解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。class Solution { public int maxProfit(int[] prices) { int cost = Inte
2021-04-04 16:00:24 61
原创 基本类型之间转换
字符串转换为intString str="123456";int i=Integer.parseInt(str);字符串转换为String char ch[]=new char[]{'5','a','B'}; String str=String.valueOf(ch);
2021-04-03 20:19:09 56
原创 栈
创建:Stack stack = new Stack<>();三个常用方法:(1)stack.push(item) 把项压入栈顶,返回item参数(2)stack.pop() 移除栈顶对象,返回对象(3)stack.isEmpty() 检测堆栈是否为空,空返回true,否则false...
2021-04-02 20:15:43 38
原创 next()和nextLine()区别
Java中Scanner类中的方法next()和nextLine()都是吸取输入台输入的字符,区别:next()不吸取字符前/后的空格。nextLine()会吸取字符前/后的空格。 public static void main(String[] args) { String s1,s2; Scanner sc=new Scanner(System.in); System.out.print("请输入第一个字符串:"); s1=sc.nextLine(); Syste
2021-04-01 14:16:12 158
原创 链表
ListNodeclass Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { // 使用带头结点的链表解决问题 // 待输出链表的头部 ListNode head = new ListNode(); // 待输出链表的 last 结点 ListNode last = head; while(l1 != null &a
2021-04-01 13:38:30 53
原创 双指针问题
双指针又分为:左右指针,快慢指针左右指针:即一个指针left=0,另一个指针right=nums.length-1。right指针向左移动,减小两数之和,left指针向右移动,增加两数和。输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。输入:nums = [2,7,11,15], target = 9输出:[2,7] 或者 [7,2]class Solution { public int[] twoSum(int[
2021-03-31 10:39:05 63
原创 滑动窗口问题
输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。例如:输入:target = 9输出:[[2,3,4],[4,5]]public int[][] findContinuousSequence(int target) { int i = 1; // 滑动窗口的左边界 int j = 1; // 滑动窗口的右边界 int sum = 0; // 滑动窗口中数字的和
2021-03-30 22:37:06 99
原创 if elseif的区别
if (x == 1)x is 1if(x==1){ x is 1}else{ x is not 1}if(x==1){x is 1}else if(x==2){x is 2}else{x is not 1 and x is not 2}x =3if(x>1){x > 1}if(x>2){x > 2}System.out.printl(x)
2021-03-30 22:24:49 90
原创 约瑟夫环问题
0,1,···,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字(删除后从下一个数字开始计数)。求出这个圆圈里剩下的最后一个数字。例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3。class Solution { public int lastRemaining(int n, int m) { int n=5; int m=3;
2021-03-30 20:11:00 37
原创 一维数组
int[] array1 = new int[3];//声明创建一个包含3个元素的数组array1(初始值为0)int[] array2 = {1, 2, 3};//声明、创建并初始化一个包含3个元素的数组int[] array3 = new int[] {1, 2, 3};//声明、创建并初始化一个包含3个元素的整型数组int[] array4;array[4] = {1, 2, 3}//先声明一个数组array,再进行创建及初始化int[] array5 = new int[3];array
2021-03-30 16:48:37 52
原创 知value查key
int k; for(k=0;k<=nums.length-1;k++){ if(map.get(k)==“已知的值”){ break; } } return k;
2021-03-28 21:52:53 39
原创 找出数组中的最大值
最大值int max = Integer.MIN_VALUE; for(int i = 0;i < nums.length;i++){ if(map.get(nums[i]) >= max){ max = map.get(nums[i]); } }最小值int min = Integer.MAX_VALUE; for(int i = 0;i < nums.
2021-03-28 21:26:23 339
原创 统计一个数字出现过的次数
Map<Character, Integer> frequency = new HashMap<Character, Integer>();for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); frequency.put(ch, frequency.getOrDefault(ch, 0) + 1); }getOrDefault() 方法获取
2021-03-28 20:50:58 201
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人