回文数
- 解题思路:
**首先将数字每位数字,分割出来加入到list中,然后对list头尾分别取数进行比较。

public static boolean isPalindrome(int x) {
boolean isPalindromeNumber = true;
List<Integer> numbers = new ArrayList<>();
if (x >= 0) {
while (x != 0) {
numbers.add(x % 10);
x = x / 10;
}
Object[] nums = numbers.toArray();
for (int i = 0, j = nums.length; i < j; i++, j--) {
if (nums[i] != nums[j-1]) {
isPalindromeNumber = false;
break;
}
}
} else {
isPalindromeNumber = false;
}
return isPalindromeNumber;
}

罗马数字转整数
- 解题思路:
**首先将每位罗马数字加入到HasnMap中,然后,根据具体规则进行数字的加减,特定规则考虑进去即可。

public static int romanToInt(String s) {
HashMap<Character, Integer> mToN = new HashMap<>();
mToN.put('I', Integer.valueOf(1));
mToN.put('V', Integer.valueOf(5));
mToN.put('X', Integer.valueOf(10));
mToN.put('L', Integer.valueOf(50));
mToN.put('C', Integer.valueOf(100));
mToN.put('D', Integer.valueOf(500));
mToN.put('M', Integer.valueOf(1000));
char[] mNumbers = s.toCharArray();
int unm = 0;
for (int i = 0; i < mNumbers.length; ) {
int tem = 0;
if ((i < mNumbers.length-1)&&(((mNumbers[i] == 'I' && mNumbers[i + 1] == 'V') ||
(mNumbers[i] == 'I' && mNumbers[i + 1] == 'X') ||
(mNumbers[i] == 'X' && mNumbers[i + 1] == 'L') ||
(mNumbers[i] == 'X' && mNumbers[i + 1] == 'C') ||
(mNumbers[i] == 'C' && mNumbers[i + 1] == 'D') ||
(mNumbers[i] == 'C' && mNumbers[i + 1] == 'M')))){
tem = mToN.get(mNumbers[i + 1]) - mToN.get(mNumbers[i]);
i = i + 2;
} else {
tem = mToN.get(mNumbers[i]);
i++;
}
unm += tem;
}
return unm;
}

最长公共前缀
- 解题思路:
** 首先获取最短字符串的长度(因为最长的公共子串也不会超过字符串的长度),然后以这个字符串长度为外循环,从而便利每个字符串列表的每个串,相同的都加入到一个StringBuffer 字段中,遇到不相同的直接跳出循坏结束。

public static String longestCommonPrefix(String[] strs) {
StringBuffer frontFull = new StringBuffer("");
int strNums = strs.length;
int minStrLength = strs[0].length();
for (int i = 0; i < strNums; i++) {
if (minStrLength > strs[i].length()) {
minStrLength = strs[i].length();
}
}
for (int i = 0; i < minStrLength; i++) {
boolean same = true;
for (int j = 0; j < strNums - 1; j++) {
char[] char1 = strs[j].toCharArray();
char[] char2 = strs[j + 1].toCharArray();
if (char1[i] != char2[i]) {
same = false;
}
}
if (same) {
frontFull.append(strs[0].toCharArray()[i]);
} else {
break;
}
}
return frontFull.toString();
}

有效括号
- 解题思路:
** 使用栈这种数据结构进行处理,首先将括号入栈一位,下一个括号入栈的时候,与栈中括号进行比较,符合有效括号的直接弹出栈,所有的括号遍历结束了,如果栈为空代表这,这个括号字符串符合有效括号,否则不符合有效括号。

public boolean isValid(String s) {
Stack stack = new Stack();
HashMap<Character,Character> characterHashMap=new HashMap<>();
characterHashMap.put('(',')');
characterHashMap.put('{','}');
characterHashMap.put('[',']');
boolean valid=true;
char [] par = s.toCharArray();
if(par.length%2!=0){
valid=false;
}else{
for(int i=0;i< par.length; i++){
if(stack.size()==0){
stack.push(par[i]);
}else{
if(characterHashMap.get(stack.peek())!=null&&characterHashMap.get(stack.peek()).charValue()==par[i]){
stack.pop();
}else{
stack.push(par[i]);
}
}
}
if(stack.size()!=0){
valid=false;
}
}
return valid;
}

合并两个有序列表
- 解题思路:
** 如果list1为空则返回list2,如果list2,为空则返回list1.如果list2的长度比list1小,两个指针相互交换一下,然后将list1最为外层循环,遍历list1中结点,然后与list2终结点进行比较(在比较过程中需要等一两个指针,一个指向,永远比另一个落后一步),遇到小于list2的将其插入即可。(困难地方修改指针状态)

class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1==null){
return list2;
}
if(list1==null){
return list1;
}
int list1L=0;
ListNode text1L=list1;
while(text1L!=null){
text1L=text1L.next;
list1L++;
}
int list2L=0;
ListNode text2L=list2;
while(text2L!=null){
text2L=text2L.next;
list2L++;
}
if(list2L<list1L){
ListNode tem=new ListNode();
tem=list2;
list2=list1;
list1=tem;
}
ListNode head=new ListNode();
head=list2;
ListNode parHead=new ListNode();
parHead.next=list2;
ListNode tem=parHead;
while(list1!=null){
boolean max=true;
while(head!=null){
if(list1.val <= head.val){
ListNode listNode=new ListNode(list1.val);
listNode.next=head;
parHead.next=listNode;
parHead=parHead.next;
head=parHead.next;
max=false;
break;
}
head=head.next;
parHead=parHead.next;
}
if(max==true){
ListNode listNode=new ListNode(list1.val);
parHead.next=listNode;
head=listNode;
}
list1=list1.next;
}
return tem.next;
}
}
