public int[] twoSum(int[] nums, int target) { Map map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target – nums[i]; if (map.containsKey(complement)) { return new int[] { map.get(complement), i }; } map.put(nums[i], i); } throw new IllegalArgumentException(“No two sum solution”); }
/** * Created by zzh on 2018/12/30. */ public class Test { public static List<List<Character>> allSort(List<Character> chars){ if(chars.size() == 1) { List<List<Character>> list = new ArrayList<>(); list.add(chars); return list; }
List<List<Character>> list = new ArrayList<>();
List<Character> useList = new ArrayList<>(); List<Character> others = new ArrayList<>(chars);
// 当前的元素和剩下的元素的全排列,拼接下就可以了 for (List<Character> othersAllSortItem : othersAllSort) { List<Character> dest = new ArrayList<>(othersAllSortItem); dest.add(0, ch);
InitList(&L)
DestroyList(&L)
ClearList(&L)
ListEmpty(L)
ListLength(L)
GetElem(L, i, &e)
LocateElem(L, e, compare())
PriorElem(L, e, &pre)
NextElem(L, e, &next)
ListInsert(&L, i, e)
ListDelete(L, i, &e)
ListTraverse(L, visit())
线性表
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L){
L.elem = (ElemType*) malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
Status ListInsert_Sq(SqList &L, int i, ElemType e){
if(i < 1 || i > L.length + 1) return ERROR;
if(L.length >= L.listsize){
newbase = (ElemType*) realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
q = &(L.elem[i - 1];
for(p = &(L.elem[L.length - 1]; p >= q; —p){
*(p + 1) = *p;
}
*q = e;
++L.length;
return OK;
}
Status ListDelete_Sq(SqList &L, int i, ElemType &e){
if( i < 1 || i > L.length) return ERROR;
p = &(L.elem[i - 1];
e = *p;
q = L.elem + L.length - 1;
for(++p; p <= q; ++p){
*(p - 1) = *p;
}
—L.length;
return OK;
}