反转整数
class Solution {
public int reverse(int x)
{
boolean negative = x < 0;
if (negative)
x = -x;
long r = 0;
while (x > 0)
{
r = r * 10 + x % 10;
x /= 10;
}
if (negative)
r = -r;
if (r > Integer.MAX_VALUE || r < Integer.MIN_VALUE)
return 0;
return (int) r;
}
}
回文数
public boolean isPalindrome(int x)
{
if(x < 0 || (x % 10 == 0 && x != 0)) {//临界问题
return false;
}
int revertedNumber = 0;
while(x > revertedNumber) {//反转一半数字
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
return x == revertedNumber || x == revertedNumber/10;
/* int xbak = x;
int reverse = 0;
while (x > 0)
{
int t = x % 10;
reverse = reverse * 10 + t;//不好,反转之后可能越界
x/=10;
}
return (reverse == xbak); */
}
最长公共前缀
public class LongestCommonPrefix
{
/**
* @param args
*/
public static void main(String[] args)
{
String[] string =
{ "flower", "flow", "flight" };
String s = longestCommonPrefix(string);
System.out.println(s);
}
public static String longestCommonPrefix(String[] strs)
{
if (strs == null || strs.length == 0)//数组判空
{
return "";
}
String s;
for (int j = 1; j <= strs[0].length(); j++)
{
s = strs[0].substring(0, j);
for (int i = 1; i < strs.length;)
{
if (strs[i].startsWith(s))
{
i++;
} else
{
return s.substring(0, s.length() - 1);
}
}
}
return strs[0];
}
}
实现strStr()
public int strStr(String haystack, String needle)
{
if (null == haystack || haystack.length() < needle.length())
{
return -1;
}
if (null == needle || needle.length() == 0)
{
return 0;
}
int len = haystack.length() - needle.length() + 1;// 这个是关键,否则可能溢出
for (int i = 0; i < len; i++)
{
String childString = haystack.substring(i);
if (childString.startsWith(needle))
{
return i;
}
}
return -1;
}
删除排序数组中的重复项
public class Solution1
{
public static void main(String[] args)
{
int[] a = { 1,1,2};
System.out.println(removeDuplicates(a));
}
public static int removeDuplicates(int[] nums)
{
if (nums.length == 0)
{
return 0;
}
int i = 0; //快慢指针
for (int j = 1; j < nums.length; j++)
{
if (nums[i] != nums[j])
{
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
有效的括号
/*
* 基于栈后入先出的特点,我们对左括号([{不停压栈,而处理)]}时, 当栈顶元素与之对应则将其出栈,最后如果栈为空,
* 则表明所有({[都已出栈;而如果栈顶元素与)]}错误匹配如(}则肯定无法闭合,直接return false即可。
*/
public boolean isValid(String s)
{
if (s == null || s.length() == 0)
{
return true;
}
if (s.length() % 2 == 1)
{
return false;
}
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++)
{
if (stack.isEmpty())
{
stack.push(s.charAt(i));
} else if (stack.peek() == '(' && s.charAt(i) == ')')
{
stack.pop();
} else if (stack.peek() == '[' && s.charAt(i) == ']')
{
stack.pop();
} else if (stack.peek() == '{' && s.charAt(i) == '}')
{
stack.pop();
} else
{
stack.push(s.charAt(i));
}
}
return stack.isEmpty();
}
最大子序和
public static void main(String[] args)
{
int[] a ={ -22, -10, -3, -4, -11, -29, -10, -5, -6 };
System.out.println(maxSubArray(a));
}
public static int maxSubArray(int[] nums)//主要是算法思想
{
int sum = nums[0];
int current = nums[0];
int len = nums.length;
for (int i = 1; i < len; i++)
{
if (current > 0)
{
current += nums[i];
} else
{
current = nums[i];
}
if (sum < current)
{
sum = current;
}
}
return sum;
}
二进制求和
import java.util.Stack;
public class Solution7
{
public static void main(String[] args)
{
System.out.println(toTen("11"));
}
public String addBinary(String a, String b)
{
/* 溢出
int int_a = 0;
int int_b = 0;
int result = 0;
int_a = toTen(a);
int_b = toTen(b);
result = int_a + int_b;
String str_result = toBinaryString(result); return str_result;
*/
/*
* 这个方法好
*/
int i = a.length() - 1, j = b.length() - 1, carry = 0;
StringBuilder sb = new StringBuilder();
while(i >=0 || j >=0){
int m = i >= 0 ? a.charAt(i) - '0' : 0;
int n = j >= 0 ? b.charAt(j) - '0' : 0;
int sum = m + n + carry;
carry = sum / 2;
sb.insert(0, String.valueOf(sum % 2));
i--;
j--;
}
if(carry != 0) sb.insert(0, '1');
return sb.toString();
}
/*
* 2进制转10进制
*/
public static int toTen(String a)
{
int int_a = 0;
int len = a.length();
for (int i = 0; i < len; i++)
{
int c = Integer.valueOf(a.charAt(i) + "");
int_a += c * Math.pow(2, len - i - 1);
}
return (int) int_a;
}
/*
* 10进制转2进制字符
*/
public static String toBinaryString(int d)
{
Stack<Integer> stack = new Stack<Integer>();
StringBuffer sBuffer = new StringBuffer();
while (d >= 2)
{
int mod = d % 2;
d /= 2;
stack.push(mod);
}
stack.push(d);
while (!stack.isEmpty())
{
sBuffer.append(stack.pop());
}
return sBuffer.toString();
}
}