一些简单的算法题

1.将两个有序链表合并为一个新的有序链表并返回,新链表是通过拼接给定的两个链表的所有节点组成的

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode l1Current = l1;
        ListNode l2Current = l2;
        if (null == l1Current) return l2Current;
        if (null == l2Current) return l1Current;
        ListNode head = null;
        if (l1Current.val < l2Current.val) {
            head = l1Current;
            l1Current = l1Current.next;
        } else {
            head = l2Current;
            l2Current = l2Current.next;
        }
        ListNode current = head;
        while (null != l1Current && null != l2Current) {
            if (l1Current.val < l2Current.val) {
                current.next = l1Current;
                l1Current = l1Current.next;
            } else {
                current.next = l2Current;
                l2Current = l2Current.next;
            }
            current = current.next;
        }
        if (null == l1Current) current.next = l2Current;
        if (null == l2Current) current.next = l1Current;
        return head;
    }
}
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
}

2.给定一个包括”(“,”)”,”{“,”}”,”[“,”]”的字符串,来判断字符串是否有效,即括号匹配

(利用栈实现)

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class Solution {
    private static final Map<String, String> MAP = new HashMap<String, String>();
    static {
        Solution.MAP.put(")", "(");
        Solution.MAP.put("}", "{");
        Solution.MAP.put("]", "[");
    }
    public boolean isValid(String s) {
        if (null == s)
            throw new NullPointerException("s is null");
        if ("".equals(s)) return true;
        Stack<String> stack = new Stack<String>();
        for (int i = 0; i < s.length(); i++) {
            String now = s.substring(i, i + 1);
            if (!Solution.MAP.containsKey(now))
                stack.push(now);
            else {
                if (stack.size() == 0) return false;
                String last = stack.pop();
                if (!Solution.MAP.get(now).equals(last)) return false;
            }
        }
        return stack.size() == 0;
    }
}

3,字符串在文件中出现的次数

import java.io.BufferedReader;
import java.io.FileReader;
public class Test {
    public static int countWordInFile(String filename, String word) {
        int count = 0;
        try (FileReader fr = new FileReader(filename)) {
            try (BufferedReader br = new BufferedReader(fr)) {
                String line = null;
                while ((line = br.readLine()) != null) {
                    int index = -1;
                    while (line.length() >= word.length() && (index = line.indexOf(word)) >= 0) {
                        count++;
                        line = line.substring(index + word.length());
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return count;
    }
    public static void main(String[] args) {
        System.out.println(countWordInFile("e:\\blog\\test\\temp.txt", "public"));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42014895/article/details/82746952
今日推荐