*******************************
一、字符串类
20 有效的括号
import java.util.Stack;
class Solution{
public static boolean isValid(String s) {
if(s.isEmpty()) return false;
char[] ss = s.toCharArray();
Stack<Character> stk = new Stack<>();
for(int i = 0; i < ss.length; i++) {
if(ss[i] == '(') stk.push(')');
else if(ss[i] == '[') stk.push(']');
else if(ss[i] == '{') stk.push('}');
else if(stk.isEmpty() || ss[i] != stk.pop()) return false;
}
return stk.isEmpty();
}
public static void main(String[] args) {
System.out.println(isValid("([)]"));
}
}
344 反转字符串
class Solution {
public static char[] reverseString(char[] s) {
for(int i = 0; i <= (s.length)/2 - 1; i++){
char temp = s[i];
s[i] = s[s.length - 1 - i];
s[s.length - 1 - i] = temp;
}
return s;
}
public static void main(String[] args) {
System.out.println(reverseString(new char[]{'a', 'b', 'c'}));
}
}
*******************************
二、数字类
1 两数之和
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class Solution {
public static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
public static void main(String[] args) {
int[] a = {2,7,11,15};
System.out.println(Arrays.toString(twoSum(a, 9)));
}
}
167 两数之和2
import java.util.Arrays;
class Solution {
public static int[] twoSum(int[] numbers, int target) {
int left = 0;
int right = numbers.length -1;
while(left < right) {
if(numbers[left] + numbers[right] < target) {
left++;
}else if((numbers[left] + numbers[right] > target)){
right--;
}else return new int[]{left + 1, right + 1};
//题目描述:返回值从1开始计数,意思是第几个数,所以返回值要 + 1
}
return new int[]{-1, -1};
}
public static void main(String[] args) {
int[] a = {2,7,14,15};
System.out.println(Arrays.toString(twoSum(a, 9)));
}
}
7 整数反转
class Solution {
public static int reverse(int x) {
int rev = 0;
while(x != 0) {
int pop = x % 10;
x /= 10;
//判断接下来rev*10不会溢出
if(rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE/10 && pop > 7) ) return 0;
if(rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE/10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
return (int)rev;
}
public static void main(String[] args) {
System.out.println(reverse(-10230));
}
}
9 回文数
class Solution {
public static boolean isPalindrome(int x) {
if(x < 0 || x != 0 && x % 10 == 0) return false;
//反转一半位数【偶数位】,或者一半+1位数【奇数位】
int reverse = 0;
while(reverse < x) {//不满足该条件看,说明已经反转一半位数【偶数位】,或者一半+1位数【奇数位】
reverse = reverse * 10 + x % 10;
x /= 10;
}
//偶数位判相等,奇数位判除去多反转的那位 是否和处理后的x值相等
return reverse == x || reverse/10 == x;
}
public static void main(String[] args) {
System.out.println(isPalindrome(1001));
}
}
53 最大子序和
import java.util.ArrayList;
import java.util.List;
class Solution {
public static List<Integer> maxSubArray(int[] nums) {
int pre = 0, res = nums[0];
int left = 0; int right = 0;
for (int i = 0; i < nums.length; i++) {
// pre = pre < 0 ? nums[i] : (pre + nums[i]);
if(pre < 0) {
left = i;//抛弃pre时,即是起始点
pre = nums[i];
} else pre += nums[i];
//pre: pre < 0时,从nums[i]开始(更新left),否则就保留pre,给后面留机会
//res:前面(不包含处理nums[i])所有轮pre的最大值
// res = Math.max(res, pre);
if(pre > res ) {
res = pre;
right = i;//
}
//如果不求左右,此处直接return res;
}
//用于输出最大和的子序列
List<Integer> list = new ArrayList<Integer>();
for (int i = left; i <= right; i++) {
list.add(nums[i]);
}
return list;
}
public static void main(String[] args) {
int[] a = {-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubArray(a));
}
}
70 爬楼梯
class Solution{
public int climbStairs(int n) {
if(1 == n) return 1;
int a = 1, b = 1, sum = 0;
for(int i = 2; i <= n; i++) {
sum = a + b;
a = b;
b = sum;
}
return sum;
}
}
class Solution {
public static int climbStairs(int n) {
//日常判空
if(n == 0) return 1;
int[] dp = new int[n+1];
dp[0] = 1; dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
public static void main(String[] args) {
System.out.println(climbStairs(3));
}
}
*******************************
三、链表类
//周边配件
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val){ this.val = val;}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
class Solution{
//算法主方法
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
}
//测试用 创建链表的方法
public static ListNode createLinkedList(int[] arr) {// 将输入的数组输入到链表中
if (arr.length == 0) {
return null;
}
ListNode head = new ListNode(arr[0]);
ListNode current = head;
for (int i = 1; i < arr.length; i++) {// 过程
current.next = new ListNode(arr[i]);
current = current.next;
}
return head;
}
//测试用 打印链表的方法
public static void printLinkedList(ListNode head) {// 将链表结果打印
ListNode current = head;
while (current != null) {
System.out.printf("%d -> ", current.val);
current = current.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
int[] x = {1, 3, 5};
int[] y = {2, 4, 6};
ListNode list1 = createLinkedList(x);
ListNode list2 = createLinkedList(y);
ListNode list0 = mergeTwoLists(list1, list2);
printLinkedList(list0);
}
}
21 合并两个有序链表
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
while( l1 != null && l2 != null) {
if(l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}else {
l2.next = mergeTwoLists(l2.next, l1);
return l2;
}
}
return l1 == null ? l2 : l1;
}
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(-1);
ListNode pre = dummyHead;
while(l1 != null && l2 != null) {
if(l1.val <= l2.val) {
pre.next = l1;
l1 = l1.next;
}else {
pre.next = l2;
l2 = l2.next;
}
pre = pre.next;
}
pre.next = l1 == null ? l2 : l1;
return dummyHead.next;
}
206 反转单链表
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}
class Solution {
public ListNode reverseList(ListNode head) {
if(null == head || null == head.next) return head;
ListNode pre = null, cur = head;
while(cur != null) {
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}