题目描述
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路
sb.toString()
去返回缓冲区的字符串代码
//!COPY
public class Solution {
public String intToRoman(int num) {
String[][] map={
{"","I","II","III","IV","V","VI","VII","VIII","IX"},
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"","M","MM","MMM"}
};
StringBuffer sb=new StringBuffer();
sb.append(map[3][num/1000%10]);
sb.append(map[2][num/100%10]);
sb.append(map[1][num/10%10]);
sb.append(map[0][num%10]);
return sb.toString();
}
}
题目描述
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
思路
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1==null){
return l2;
}
if(l2==null){
return l1;
}
ListNode head=new ListNode(0);
ListNode p=head;
int tem=0;
while(l1!=null||l2!=null||tem!=0){
if(l1!=null){
tem+=l1.val;
l1=l1.next;
}
if(l2!=null){
tem+=l2.val;
l2=l2.next;
}
p.next=new ListNode(tem%10);
p=p.next;
tem/=10;
}
return head.next;
}
}
题目描述
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
思路
代码1
import java.util.HashMap;
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap map=new HashMap<>();
int len=s.length();
if(s==null||len==0){
return 0;
}
int res=0;
int l=0;
for(int i=0;i
代码2
import java.util.HashMap;
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap map=new HashMap<>();
int len = s.length();
if(s==null||len==0){
return 0;
}
for(int i=0;i