题目描述

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

public class Solution {public int searchInsert(int[] A, int target) {int a = 0;int b = A.length - 1;while(a <= b) {int n = (a + b) / 2;if(A[n] == target) {return n;} else if(A[n] < target) {a = n + 1;} else {b = n - 1;}}return a;}
}

题目描述

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return[-1, -1].

For example,
Given[5, 7, 7, 8, 8, 10]and target value 8,
return[3, 4].

public class Solution {public int[] searchRange(int[] A, int target) {int a = 0;int b = A.length - 1;while(a <= b) {int n = (a + b) / 2;if(A[n] == target) {int m = n;while (n > 0) {if(A[n - 1] == target) {n--;} else {break;}}while (m < A.length - 1) {if(A[m + 1] == target) {m++;} else {break;}}int[] res = {n, m};return res;} else if(A[n] < target) {a = n + 1;} else {b = n - 1;}}int[] res = {-1, -1};return res;}}

题目描述

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

public class Solution {public int search(int[] A, int target) {int a = 0;int b = A.length - 1;while(a <= b) {int n = (a + b) / 2;if(A[n] == target) {return n;} else if((A[n] < target && A[n] < A[b] && target <= A[b]) || (A[n] > A[b] && !(A[a] <= target && target < A[n])) ) {a = n + 1;} else {b = n - 1;}}return -1;}}

题目描述

Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.

For"(()", the longest valid parentheses substring is"()", which has length = 2.

Another example is")()())", where the longest valid parentheses substring is"()()", which has length = 4.

import java.util.Stack;
public class Solution {public int longestValidParentheses(String s) {char[] c = s.toCharArray();int result = 0;int pre = -1;Stack<Integer> stack = new Stack<>();for(int i = 0; i < c.length; i++) {if(c[i] == '(') {stack.push(i);} else {if(stack.size() == 0) {pre = i;} else {stack.pop();if(!stack.isEmpty()) {result = result > (i - stack.peek()) ? result : (i - stack.peek());} else {result = result > (i - pre) ? result : (i - pre);}}}}return result;}
}

题目描述

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    For example, given array S = {-1 2 1 -4}, and target = 1.The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
import java.util.Arrays;
public class Solution {public int threeSumClosest(int[] num, int target) {int result = Integer.MAX_VALUE;if (num == null || num.length < 0) {return result;}Arrays.sort(num);int temp = 0;int low = 0;int high = 0;int close = Integer.MAX_VALUE;for (int i = 0; i < num.length - 2; i++) {low = i + 1;high = num.length - 1;while (low < high) {temp = num[low] + num[high] + num[i];if(temp == target) {return target;}if(temp > target) {high--;} else {low++;}if(close > Math.abs(temp - target)) {result = temp;close = Math.abs(temp - target);}}}return result;}}

题目描述

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, abc)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},A solution set is:(-1, 0, 1)(-1, -1, 2)
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {public ArrayList<ArrayList<Integer>> threeSum(int[] num) {ArrayList<ArrayList<Integer>> result = new ArrayList<>();if (num == null || num.length < 0) {return result;}Arrays.sort(num);int temp = 0;int low = 0;int high = 0;for (int i = 0; i < num.length - 2; i++) {if (i != 0 && num[i] == num[i - 1]) {continue;}low = i + 1;high = num.length - 1;while (low < high) {temp = num[low] + num[high];if (temp + num[i] == 0) {ArrayList<Integer> solution = new ArrayList<>();solution.add(num[i]);solution.add(num[low]);solution.add(num[high]);result.add(solution);if(num[low] == num[high]) {break;} low++;high--;while (low < high && num[low] == num[low - 1]) {low++;}while (low < high && num[high] == num[high + 1]) {high--;}} else if (temp + num[i] < 0) {low++;} else {high--;}}}return result;}
}

20190520算法题存档相关推荐

  1. 算法题存档20200505

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  2. 算法题存档20191223

    题目描述 删除给出链表中的重复元素,使链表中的所有元素都只出现一次 例如: 给出的链表为1->1->2,返回1->2. 给出的链表为1->1->2->3->3 ...

  3. 算法题存档20190207

    题目描述 如果一个整数只能被1和自己整除,就称这个数是素数. 如果一个数正着反着都是一样,就称为这个数是回文数.例如:6, 66, 606, 6666 如果一个数字既是素数也是回文数,就称这个数是回文 ...

  4. 算法题存档20190127

    题目描述 假设一个探险家被困在了地底的迷宫之中,要从当前位置开始找到一条通往迷宫出口的路径.迷宫可以用一个二维矩阵组成,有的部分是墙,有的部分是路.迷宫之中有的路上还有门,每扇门都在迷宫的某个地方有与 ...

  5. 算法题存档20200627(树)

    给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表. 如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,那么请你返回 Tru ...

  6. 算法题存档2020425

    给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [   [1,2,3],   [1,3,2],   [2,1,3],   [2,3,1],   [3, ...

  7. 20191219算法题存档

    题目描述 给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组 注意: 可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和n public class ...

  8. 20190730算法题存档

    题目描述 Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→ ...

  9. 20190724算法题存档

    题目描述 Sort a linked list in O(n log n) time using constant space complexity. public class Solution {p ...

最新文章

  1. Neo4j 图创建2 以小麦及其相关信息为例(未完成)
  2. python 快速删除程序_如何快速一次性卸载所有python包(第三方库)呢
  3. android supportv4最新版本19.1,Android Studio:无法找到:’com.android.support:support-v4:19.1.0′...
  4. 六石管理学:新增一类产品问题,面子类问题
  5. JAVA 实验报告总结
  6. 斐讯k2p openwrt固件改双WAN口
  7. 已知圆上三个点坐标,求圆半径 r 和 圆心坐标
  8. (15年)单循环赛制,所有参赛队伍在竞赛中均能两两相遇一次。由键盘获得N队伍数,采用递归算法实现对N支队伍总比赛场次的计算
  9. element-ui 固定弹窗底部的按钮
  10. 音频信号转为开关控制信号_基于CPCI总线控制卡的信号完整性设计
  11. PHP开发小技巧①②—一些常用的PHP正则表达式
  12. 网口压线顺序_水晶头压线顺序
  13. pip install 报错:ERROR: Exception: Traceback (most recent call last):..raise ValueError(“check_hostnam
  14. 跨癌症模型模拟免疫细胞行为
  15. VSTO,PPT插件,C#,文本框TextFrame对象和TextFrame2对象在文本居中上的区别
  16. 计算机专业要学英语口语,学习英语口语必须掌握两大法宝
  17. Java写的第一个小游戏(续)
  18. 工信部信息技术发展司谢少锋司长高度评价华云数据“自主创新”的钻研精神
  19. MYSQL 那点破事,索引、SQL调优、事务、B+树、分表 ....
  20. 浅谈 yso的 Commons-Collections1 (cc1)反序列化链 如何手写这条链子

热门文章

  1. wake on lan
  2. mysql dede arctiny_如何用织梦SQL命令行工具操作数据库及常用sql语句整理
  3. SpringBoot+Vue 完整的外卖系统,手机端和后台管理
  4. (四)java版spring cloud+spring boot 社交电子商务平台-断路器(Hystrix)
  5. java八大基本类型介绍
  6. 高可用之KeepAlived(2):keepalived+lvs
  7. 14-磁盘管理-df,du命令,磁盘分区
  8. mysql触发器的简单入门(二)
  9. poi读取合并单元格
  10. server2008密码不满足密码策略的要求,检查最小密码长度、密码复杂性和密码历史的要求”的解决办法...