算法练习LeetCode初级算法之排序和搜索
合并两个有序数组
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
System.arraycopy(nums2, 0, nums1, m, n);
Arrays.sort(nums1);
}
}
第一个错误的版本
递归解法:自己突然来了灵感写的,哈哈哈,递归真的很神奇!
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
class Solution extends VersionControl {
public int firstBadVersion(int n) {
return helper(0, n);
}
private int helper(int l,int r) {
if (l<r) {
int m=l+(r-l)/2;
if (isBadVersion(m)) {
return helper(l, m);
}else {
return helper(m+1, r);
}
}
return l;
}
}
二分法解法:
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l=1;
int r=n;
while(l<r){
int m=l+((r-l)/2);
if(isBadVersion(m)){
r=m;
}else{
l=m+1;
}
}
return l;
}
}
很慢的解法:
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int i=1;
while(i<=n){
if(isBadVersion(i)){
break;
}
i++;
}
return i;
}
}
算法练习LeetCode初级算法之排序和搜索的更多相关文章
- 【LeetCode算法】LeetCode初级算法——字符串
在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...
- 算法练习LeetCode初级算法之链表
删除链表中的节点 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode ne ...
- 算法练习LeetCode初级算法之字符串
反转字符串 我的解法比较low,利用集合的工具类Collections.reverse反转,用时过长 class Solution { public void reverseString(char[] ...
- 算法练习LeetCode初级算法之数组
删除数组中的重复项 官方解答: 旋转数组 存在重复元素 只出现一次的数 官方解答: 同一个字符进行两次异或运算就会回到原来的值 两个数组的交集 II import java.util.Arr ...
- 算法练习LeetCode初级算法之其他
位1的个数 解法一: class Solution { // you need to treat n as an unsigned value public int hammingWeight(int ...
- 算法练习LeetCode初级算法之数学
Fizz Buzz class Solution { public List<String> fizzBuzz(int n) { List<String> list=new L ...
- 算法练习LeetCode初级算法之设计问题
打乱数组 不断的让第一个与后面随机选择的数交换 class Solution { private int[] nums; private int[] initnums; public Solution ...
- 算法练习LeetCode初级算法之动态规划
爬楼梯:斐波那契数列 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 非递归解法 class S ...
- 算法练习LeetCode初级算法之树
二叉树的前序遍历 我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list class Solution { public List<Integer> preorderT ...
随机推荐
- 细谈getRequestDispatcher()与sendRedirect()的区别
问题?细谈getRequestDispatcher()与sendRedirect()的区别 首先我们要知道: (1)request.getRequestDispatcher()是请求转发,前后页面共享 ...
- 【mysql】工具使用
mysql之workbench如何只导出(insert语句)数据 MySQL 编码:utf8 与 utf8mb4,utf8mb4_unicode_ci 与 utf8mb4_general_ci htt ...
- PythonStudy——可变与不可变 Variable and immutable
ls = [10, 20, 30] print(id(ls), ls) ls[0] = 100 print(id(ls), ls) print(id(10)) print(id(20)) print( ...
- alert大法看执行流程(一次采坑)
页面的dom元素加载完了,给元素一次性添加事件. 收获:事件都是一次性给添加好的,不是点击一次,................................................... ...
- 全面理解Java内存模型(JMM)及volatile关键字
[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/72772461 出自[zejian ...
- EF LIKE 查询
<!-- CSDL content --> <edmx:ConceptualModels> <Schema Namespace="Model" Ali ...
- 如何增加黑客通过ssh入侵的难度--保护ssh的三把锁
源文档:https://blog.csdn.net/cnbird2008/article/details/6130696 简介 如果需要远程访问计算机并启用了 Secure Shell (SSH) 连 ...
- Python3 练习2 列表和字典练习
找出列表list中大于100的值,给字典dic的k1键,小于等于100的值,给字典dic的k2键 ''' 提示:创建字典的两种方式 ex: ''' v1 = [2,3,4,5,] v2 = 88 di ...
- public class PageRender implements ResponseRender
package cn.ubibi.jettyboot.demotest.controller.render; import cn.ubibi.jettyboot.framework.commons.S ...
- Pycharm 设置上下左右快捷键
Pycharm的版本 Note:英文版的Pycharm,使用中文版的对照即可. 1. 打开Pycharm软件→File→Settings 2.Keymap→Editor Actions→搜索(up)→ ...