【JAVA、C++】LeetCode 015 3Sum】的更多相关文章

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, a ≤ b ≤ c) The solut…
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…
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 解题思路一: 暴力枚举 共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3) 解题思路二: 动态…
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->…
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 解题思路一:…
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be…
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe…
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字翻转并不难,可以转成String类型翻转,也可以逐位翻转,本题涉及到的主要是边界和溢出问题,使用Long或者BigInteger即可解决. 题目不难: JAVA实现如下: public class Solution { static public int reverse(int x) { if(x=…
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSII…
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路: 由于要求时间复杂度O(log (m+n))所以几乎可以肯定是递归和分治的思想. <算法导论>里有找两个数组第K小数的算法,时间复杂度为O(lo…
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst…
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that…
  Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 解题思路: 新建一个ListNode进行存储即可,JAVA实现如下: static public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNo…
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"…
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given…
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. 解题思路一: 四路夹逼,C++: class Solution { public: vector<vector<int> > fou…
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. 解题思路: 思路一: 使用DFS算法,JAVA实现如下: static String[] alpha = new String[] { " &q…
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可,注意边界条件: JAVA实现: static public String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; for (int i = 0; i < strs[0].length(…
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 解题思路: 类似上题,方法多多,本题直接给出上题中字典匹配的代码: JAVA实现: static public int romanToInt(String s) { int num=0; String Roman[][] = { {"", "I", &q…
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 本题思路比较简单,难度主要集中在罗马数字本身,直接贴代码. 思路一,从高位开始: JAVA: static public String intToRoman(int number) { int[] values = { 1000, 900, 500, 400, 100, 90, 50…
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe…
Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下: static public boolean isPalindrome(int x) { if (x < 0) return false; int temp = x,beginIndex = 0, endIndex = 0; while (temp >= 10) { temp /= 10; en…
1.数据库创建 参考接上文cassandra入门 http://www.cnblogs.com/piaolingzxh/p/4197833.html 2.下载jdbc驱动源码,构建jar包 源码下载地址:https://bitbucket.org/openscg/cassandra2-jdbc/src,最新包可能会构建失败,请下载2.1.1版的 当然,你也可以使用我构建好的,地址:http://download.csdn.net/detail/piaolingzxh/8320131 注:使用ma…
原文:AES加密CBC模式兼容互通四种编程语言平台[PHP.Javascript.Java.C#] 由于本人小菜,开始对AES加密并不了解,在网络上花了比较多时间查阅资料整理: 先简单从百度找来介绍: 1     密码学中的高级加密标准(Advanced Encryption Standard,AES),又称高级加密标准Rijndael加密法, 2 是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分析且广为全世界 3 所使用.经过五年的甄选流程,高级加密标准由美国国…
前面几篇Java学习笔记都是半夜写的,比较伤身体,今天开始想调整生物钟,早上起来学2小时,看看能坚持多久 本周目标: 1.JavaJDBC使用 2.JavaWeb编程 3.Java框架基础(反射+注解) 从Java1.5开始有注解,被很多框架广泛使用 概念: Java提供了一种原程序中的元素,关联任何信息和任何元数据的途径和方法. 本篇学习概要: Java注解[一.概述] Java注解[二.Java中的常见注解] Java注解[三.注解的分类] Java注解[四.自定义注解] Java注解[五.…
.net也使用过反射,不过用的比较浅显,用来记日志等.. Java基础课程学习已经过了一段时间了,接下来继续学习 本次学习包含以下内容 Java反射[一.概述] Java反射[二.Class类的使用] Java反射[三.方法的反射] Java反射[四.成员变量的反射和构造的反射]…
本文地址 分享提纲: 1. Java程序特点 1.1 基本语法 1.2 字符串 1.3 变量 1.4 Java数组 1.5 Java枚举 1.6 Java修饰符 1.7 Java编译制定在制定目录 2. Java面向对象 2.1 Java类和对象 2.2 类的一些注意点 2.3 Java Number类 2.4 Java Character 类 2.5 Java String 类 2.6 Java StringBuffer 和 StringBuilder 类 2.7 Java 数组 2.8 Ja…
http://blog.csdn.net/ns_code/article/details/17539599这个博主的关于java并发编程系列很不错,值得收藏. 为了方便各位网友学习以及方便自己复习之用,将Java并发编程系列内容系列内容按照由浅入深的学习顺序总结如下,点击相应的标题即可跳转到对应的文章    [Java并发编程]实现多线程的两种方法    [Java并发编程]线程的中断    [Java并发编程]正确挂起.恢复.终止线程    [Java并发编程]守护线程和线程阻塞    [Ja…
背景 项目中我们会遇到这种情况:在几个方法中增加同样的代码,这些代码是与业务无关的,而且以后有可能因为考虑不周或需求变动再或者是其它原因,我们须要对他们进行逐一进行修改.举个详细的样例,比方程序中的日志控制.事务控制等,这些功能是与业务无关的,但却须要将它们与我们的逻辑混在一起.达到一些特殊的需求. 这种情况往往代码都是同样的,能够抽离出来,为了复用,我们能够将这些同样的代码单独封装成的方法,以供其它须要的地方调用,这样对于以后的改动就做到了仅仅改动一处的效果,达到了程序的复用.但另外一个问题便…
在[java虚拟机系列]java虚拟机系列之JVM总述中我们已经详细讲解过java中的内存模型,了解了关于JVM中内存管理的基本知识,接下来本博客将带领大家了解java中的垃圾回收与内存分配策略. 垃圾回收(Garbage Collection,GC)是java语言的一大特色,在Java中,程序员不需要去关心内存动态分配和垃圾回收的问题,这一切都交给了JVM来处理.而在C/C++中是需要程序员主动释放的,而在java中则交给JVM自动完成,既然是交给程序自动执行,那么这里就必须完成以下几件事:…