leetcode mock interview-two sum II
package com.company; import java.util.LinkedList;
import java.util.List; public class Main { public int[] twoSum(int[] numbers, int target) {
int i = 0, j = numbers.length - 1;
int []ret = new int[2];
while (i < j) {
if (numbers[i] + numbers[j] < target) {
i++;
}
else if (numbers[i] + numbers[j] > target) {
j--;
}
else {
ret[0] = i+1;
ret[1] = j+1;
break;
}
}
return ret;
} public static void main(String[] args) {
// write your code here
System.out.println("Hello"); int []numbers = {2, 7, 11, 15};
int target = 9;
Main mn = new Main();
int[] ret = mn.twoSum(numbers, target);
System.out.printf("ret: %d, %d\n", ret[0], ret[1]); }
}
leetcode mock interview-two sum II的更多相关文章
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- leetcode & Mock Interview
leetcode & Mock Interview https://leetcode.com/interview/ xgqfrms 2012-2020 www.cnblogs.com 发布文章 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【LeetCode】113. Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [LeetCode] Nested List Weight Sum II 嵌套链表权重和之二
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- vuejs学习——vue+vuex+vue-router项目搭建(三)
前言 vuejs学习——vue+vuex+vue-router项目搭建(一) vuejs学习——vue+vuex+vue-router项目搭建(二) 为什么用vuex:组件之间的作用域独立,而组件之间 ...
- 在树莓派3B上安装node.js
本文主讲如何在树莓派3B上安装node.js 环境描述1. 树莓派安装了`2016-11-25-raspbian-jessie-lite`(PS:在此版本的镜像中,默认禁用了ssh,在烧录好镜像之后, ...
- ubuntu16.04安装python3
今天用了下阿里云的云服务器,装个python3真是各种踩坑.记录下吧: ubuntu自带了2.7.想要装3.5并设置为默认python版本. 安装python3.5 sudo add-apt-repo ...
- noip2007树网的核
想一下可以发现随便枚举一条直径做就可以了. 核越长越好.于是枚举核的过程可以做到O(n) 然后就是统计答案. 对于每个核最大偏心距肯定是核上面每个点不走核内的点所能走到的最远点的最值. 而且对于核的两 ...
- java中代码块的认知
在以往面试题中,出现过很多次关于代码块执行顺序的题目,目前可算是搞清楚执行顺序了.看以下代码: package com.itcast.code; public class BlockTest { st ...
- Mybatis 删除多条数据XML SQL语句删除
Mybatis 删除多条数据XML SQL语句删除 1.删除多条数据SQL写法 <delete id="deleteParamsByIds"> delete from ...
- php回溯
$sl = debug_backtrace(); 返回的$sl 是一个二维数组 包含如下元素: function string 当前的函数名,参见: __FUNCTION__. line intege ...
- bzoj 2565: 最长双回文串 manacher算法
2565: 最长双回文串 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem. ...
- poj 3624 Charm Bracelet 背包DP
Charm Bracelet Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 Descripti ...
- hihocoder1320 160周 压缩字符串
hihocoder1320 题目链接 思路: dp解法.用map[i][j]表示从第i个开始到第j个的字串的best压缩长度.(包括i,j,两端闭合). 用k表示i,j中的一点. 用zip()表示压缩 ...