Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
给一个数组nums找出和为target的元素索引
方法一 循环遍历
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
for(int i=0;i<nums.length;i++)
{
for(int j=i+1;j<nums.length;j++)
{
if((nums[i]+nums[j])==target)
{
res[0]=i;
res[1]=j;
return res;
}
}
}
return res;
}
复杂度n^2
方法二
用map记录元素对应索引
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
Map<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++)
{
if(map.containsKey(target-nums[i]))
{
res[0]=map.get(target-nums[i]);
res[1]=i;
return res;
}
else
{
map.put(nums[i], i);
}
}
return res;
}
复杂度n
Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
将数子倒过来
public int reverse(int x) {
int res=0;
while(x!=0)
{
int tail=x%10;//取个位
int newres=res*10+tail;//新结果等于上次处理结果*10+个位
if((newres-tail)/10!=res)//处理溢出 新结果如果能退回老结果则证明无溢出
{
return 0;
}
res=newres;
x=x/10;
}
return res;
}
Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121 Output: true
Example 2:
Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
判断是否是回文数字负数为false
思路一
先与上一题一样操作将数字反转然后比较反转前后是否相同相同则为回文数字
public boolean isPalindrome(int x) {
int res=0;
int xx=x;
if(x<0)
{
return false;
}
while(xx!=0)
{
int tail=xx%10;
int newres=res*10+tail;
if((newres-tail)/10!=res)//处理溢出
{
res=0;
break;
}
res=newres;
xx/=10;
}
return res==x?true:false;
}
思路二
在思路一上改进只需转换原数字的一半位数
public boolean isPalindrome(int x) {
if (x<0 || (x!=0 && x%10==0)) return false;//小于10的数不是回文且无法用算法判断
int res=0;
while(x>res)
{
int tail=x%10;
res=res*10+tail;
x=x/10;
}
return res==x||res/10==x;//数字位数分奇偶
}
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
.
Example 1:
Input: ["flower","flow","flight"] Output: "fl"
Example 2:
Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.找到最长的公共前缀
思路:假定最长的公共前缀为pre遍历字符串数组每一元素判断每一个元素是否以pre为前缀如果当前字符串不以pre开头则将pre缩短一位判断直到当前字符串以pre开头在判断下一个
public String longestCommonPrefix(String[] strs) {
if(strs==null||strs.length==0)
{
return "";
}
String pre=strs[0];
for(int i=1;i<strs.length;i++)
{
while(strs[i].indexOf(pre)!=0)
{
pre=pre.substring(0,pre.length()-1);
}
}
return pre;
}
Valid Parentheses
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()" Output: true
Example 2:
Input: "()[]{}" Output: true
Example 3:
Input: "(]" Output: false
Example 4:
Input: "([)]" Output: false
Example 5:
Input: "{[]}" Output: true
思路一
建一个栈左括号压栈右括号弹栈匹配
Stack<Character> st = new Stack<>();
public boolean isValid(String s) {
char[] chars=s.toCharArray();
for(int i=0;i<chars.length;i++)
{
char ch=chars[i];
if(ch=='(')
{
st.push(ch);
}
else if(ch=='{')
{
st.push(ch);
}
else if(ch=='[')
{
st.push(ch);
}
else if(ch==')')
{
if(st.isEmpty())
{
return false;
}
else
{
if(st.pop()!='(')
{
return false;
}
}
}
else if(ch=='}')
{
if(st.isEmpty())
{
return false;
}
else
{
if(st.pop()!='{')
{
return false;
}
}
}
else if(ch==']')
{
if(st.isEmpty())
{
return false;
}
else
{
if(st.pop()!='[')
{
return false;
}
}
}
}//
if(st.isEmpty()==true)
{
return true;
}
else
{
return false;
}
}
思路二
public boolean isValid(String s) {
Stack<Character> st = new Stack<>();
char[] chars=s.toCharArray();
for(int i=0;i<chars.length;i++)
{
char ch=chars[i];
if(ch=='(')
{
st.push(')');
}
else if(ch=='{')
{
st.push('}');
}
else if(ch=='[')
{
st.push(']');
}
else if(st.isEmpty()||st.pop()!=ch)
{
return false;
}
}//
return st.isEmpty();
}